-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
53 lines (48 loc) · 1.73 KB
/
code.py
File metadata and controls
53 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import telepot
import os
import pprint
from telepot.loop import MessageLoop
from pipeline import Pipeline
from grammar.parser import SyntacticError
from grammar.executor import SemanticError
from utils import WHITELIST, help_message
import time
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
mssg_id = msg['message_id']
try:
_ = msg['reply_to_message']
return
except:
pass
try:
_ = msg['edit_date']
return
except:
pass
pprint.pprint(msg)
if chat_id not in WHITELIST or time.time() - 120 > msg['date']:
return
if command[:2] == '/d':
print('query: ' + command)
verbose = False
try:
pipeline = Pipeline(command[2:], verbose)
ans = pipeline.getString() + 'total = ' + str(pipeline.getResult())
print(ans)
bot.sendMessage(chat_id, ans, parse_mode = 'HTML', reply_to_message_id=mssg_id)
except SyntacticError as e:
bot.sendMessage(chat_id, 'Invalid query, ' + str(e) + '\n\nType /aiuda if you need help', reply_to_message_id=mssg_id)
except SemanticError as e:
bot.sendMessage(chat_id, 'Invalid query, ' + str(e), reply_to_message_id=mssg_id)
except Exception as err:
bot.sendMessage(chat_id, 'whoops, something went wrong, check the /aiuda command if you need help', reply_to_message_id=mssg_id)
print(err)
elif command[:6] == '/aiuda':
bot.sendMessage(chat_id, help_message, parse_mode='HTML', reply_to_message_id=mssg_id)
else:
return
bot.sendMessage(chat_id, '*bold*', parse_mode = 'Markdown')
bot = telepot.Bot(os.environ['BOT_TOKEN'])
MessageLoop(bot, handle).run_forever()