mirror of
https://github.com/house-of-vanity/conf_bot.git
synced 2025-07-06 22:34:07 +00:00
Add sql query support
This commit is contained in:
15
database.py
15
database.py
@ -125,6 +125,21 @@ class DataBase:
|
||||
result = self.execute(sql)
|
||||
return(result)
|
||||
|
||||
def command(self, sql):
|
||||
if 'DELETE' in sql.upper() \
|
||||
or 'INSERT' in sql.upper() \
|
||||
or 'UPDATE' in sql.upper() \
|
||||
or 'CREATE' in sql.upper() \
|
||||
or 'ALTER' in sql.upper():
|
||||
return('gtfo')
|
||||
try:
|
||||
if 'LIMIT' in sql.upper()[-9:]:
|
||||
result = self.execute(sql)
|
||||
else:
|
||||
result = self.execute(sql + ' limit 20')
|
||||
except Exception as err:
|
||||
result = err
|
||||
return(result)
|
||||
|
||||
def close(self):
|
||||
self.conn.close()
|
||||
|
20
worker.py
20
worker.py
@ -45,6 +45,25 @@ class MessageWorker:
|
||||
user_id=user_id)
|
||||
return True
|
||||
|
||||
if msg['message']['text'][:4] == '/sql':
|
||||
conf_id = msg['message']['chat']['id']
|
||||
user_id = msg['message']['from']['id']
|
||||
chat_title = msg['message']['chat']['title']
|
||||
self.db.add_conf(conf_id, chat_title)
|
||||
sql = msg['message']['text'][5:]
|
||||
|
||||
res = self.db.command(sql)
|
||||
try:
|
||||
msg = '```\n'
|
||||
for z in res:
|
||||
for i in z:
|
||||
msg = msg + str(i) + '\t'
|
||||
msg = msg + '\n'
|
||||
except:
|
||||
msg = res
|
||||
self.send(id=conf_id, msg=msg + ' ```')
|
||||
return True
|
||||
|
||||
if msg['message']['text'] == '@here':
|
||||
conf_id = msg['message']['chat']['id']
|
||||
user_id = msg['message']['from']['id']
|
||||
@ -123,6 +142,7 @@ class MessageWorker:
|
||||
return collection
|
||||
|
||||
def send(self, id, msg):
|
||||
print(msg)
|
||||
url = settings.parser.get('bot', 'telegram_api') + \
|
||||
'bot'+ settings.parser.get('bot', 'telegram_key') \
|
||||
+ '/sendMessage'
|
||||
|
Reference in New Issue
Block a user