-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_listener.py
559 lines (437 loc) · 21 KB
/
telegram_listener.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
'''Listener for Telegram messages sent to bot'''
# pylint: disable=wrong-import-position
import os
import sys
import datetime
import time
from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove, KeyboardButton
from telegram.error import NetworkError
from telegram.ext import (filters, ApplicationBuilder, CommandHandler, PicklePersistence,
MessageHandler, ConversationHandler, ContextTypes, PersistenceInput)
import random_number_generator
import weather
import news
import nasa
import todos
import jokes
# change working directory for run through cron
os.chdir(os.path.dirname(os.path.abspath(__file__)))
from Utilities import telegram_utils
from Utilities import wait_for_internet
from Utilities import key_manager
from Utilities import user_manager
from Utilities import general
from Utilities import timer_utils
def help_msg():
'''Returns help message with list of commands'''
commands = ['<b><u>Supported commands:</u></b>',
'/help - Available commands',
'/todo - Canvas todos',
'/joke - Random joke',
'/weather - Local weather info',
'/news - Top 10 US news headlines',
'/nasa - NASA astronomy pic of the day',
'/random - Random number generator',
'/timer - Custom timers']
return commands
def handle_update(update, text=True):
'''Returns user for received message'''
telegram_id = update.effective_chat.id
msg = update.message.text.lower() if text else 'non_text_msg'
print('--------------------------')
print(datetime.datetime.now())
print()
print('Message received')
print(f'Chat id: {telegram_id}')
print(f'Message: {msg}')
print()
user = _user_manager.get_user_from_telegram_id(telegram_id)
if user is None:
print('Unapproved sender')
print()
return False
print(f'Approved sender: {user.name}')
print()
return True
MODE, REMOVE, NAME, UNIT, DURATION = range(5)
async def timer_init(_):
'''Initializes job queue with existing timers'''
if not app.bot_data.get('timers'):
app.bot_data['timers'] = {}
return
for chat_id in app.bot_data['timers']:
for timer in app.bot_data['timers'][chat_id].values():
if timer.remaining_seconds() < 0:
app.job_queue.run_once(timer_alarm, 0, chat_id=chat_id,
data={'timer':timer, 'expired':True})
else:
app.job_queue.run_once(timer_alarm, timer.duration, chat_id=chat_id,
data={'timer':timer,})
async def timer_start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Initial function in /timer command pipeline'''
context.user_data['timer_start'] = time.time()
if not handle_update(update): # only need to check approval in 1st message of pipeline
await telegram_utils.send_message(bot, update.effective_chat.id, ['Unauthorized'])
return
reply_markup = ReplyKeyboardMarkup([["Add", "Remove", "List"]], one_time_keyboard=True)
await telegram_utils.send_message(bot, update.effective_chat.id, ['Enter mode or /cancel'],
reply_markup=reply_markup)
return MODE
async def timer_add(update: Update, _) -> int:
'''Gets new timer name'''
handle_update(update)
await telegram_utils.send_message(bot, update.effective_chat.id,
['Enter a name for the timer or /cancel'],
reply_markup=ReplyKeyboardRemove())
return NAME
async def timer_name(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Gets new timer time unit after saving name'''
handle_update(update)
context.user_data['name'] = update.message.text
reply_markup = ReplyKeyboardMarkup([["Days", "Hours", "Minutes", "Seconds"]],
one_time_keyboard=True)
await telegram_utils.send_message(bot, update.effective_chat.id,
['Choose a time unit or /cancel'],
reply_markup=reply_markup)
return UNIT
async def timer_unit(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Gets new timer duration after saving time unit'''
handle_update(update)
context.user_data['unit'] = update.message.text[0]
await telegram_utils.send_message(bot, update.effective_chat.id,
['Enter an integer duration or /cancel'],
reply_markup=ReplyKeyboardRemove())
return DURATION
async def timer_duration(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Creates timer after saving duration'''
handle_update(update)
name = context.user_data['name']
unit = context.user_data['unit']
duration = int(update.message.text)
chat_id = update.effective_chat.id
if unit == 'D':
duration *= 60 * 60 * 24
elif unit == 'H':
duration *= 60 * 60
elif unit == 'M':
duration *= 60
curr_time = int(time.time())
timer = timer_utils.Timer(name, chat_id, curr_time, duration)
if not app.bot_data['timers'].get(chat_id):
app.bot_data['timers'][chat_id] = {}
app.bot_data['timers'][chat_id][curr_time] = timer
context.job_queue.run_once(timer_alarm, duration, chat_id=chat_id,
data={'timer': timer})
await telegram_utils.send_message(bot, chat_id, ['Timer successfully created'],
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
async def timer_remove(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Gets timer to remove'''
handle_update(update)
if len(context.job_queue.jobs()) == 0:
await telegram_utils.send_message(bot, update.effective_chat.id,
['No active timers'],
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
messages = ['<b><u>Make a selection:</u></b>']
for i, job in enumerate(context.job_queue.jobs()):
timer = job.data['timer']
messages.append(f'{i} - {timer}')
await telegram_utils.send_message(bot, update.effective_chat.id, messages,
reply_markup=ReplyKeyboardRemove())
return REMOVE
async def timer_remove_core(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Removes timer from existing'''
handle_update(update)
selection = int(update.message.text)
if selection < 0 or selection >= len(context.job_queue.jobs()):
await telegram_utils.send_message(bot, update.effective_chat.id,
['Invalid selection, try again or /cancel'],
reply_markup=ReplyKeyboardRemove())
return REMOVE
timer = context.job_queue.jobs()[selection].data['timer']
chat_id = timer.chat_id
context.job_queue.jobs()[selection].schedule_removal()
app.bot_data['timers'][chat_id].pop(timer.start)
await telegram_utils.send_message(bot, update.effective_chat.id,
['Timer removed'],
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
async def timer_list(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Lists existing timers'''
handle_update(update)
if len(context.job_queue.jobs()) == 0:
await telegram_utils.send_message(bot, update.effective_chat.id,
['No active timers'],
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
messages = ['<b><u>Active timers:</u></b>']
for job in context.job_queue.jobs():
timer = job.data['timer']
messages.append(f'{timer}')
await telegram_utils.send_message(bot, update.effective_chat.id, messages,
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
async def timer_alarm(context: ContextTypes.DEFAULT_TYPE):
'''Displays message when timer expires'''
job = context.job
timer = job.data['timer']
chat_id = timer.chat_id
app.bot_data['timers'][chat_id].pop(timer.start)
if job.data.get('expired'):
await telegram_utils.send_message(bot, job.chat_id,
["<b><u>Alarm rang while bot was down!</u></b> -" +
f"{timer.name}"])
else:
await telegram_utils.send_message(bot, job.chat_id,
[f"<b><u>Timer alarm!</u></b> - {timer.name}"])
async def timer_cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''/cancel entered during /timer pipeline'''
handle_update(update)
await telegram_utils.send_message(bot, update.effective_chat.id,
["Timer command canceled"],
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
async def timer_timeout(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
'''Timeout during /timer pipeline'''
await telegram_utils.send_message(bot, update.effective_chat.id,
["Timer command timeout"],
reply_markup=ReplyKeyboardRemove())
return end_timer_pipeline(context)
def end_timer_pipeline(context):
'''Ends timer conversation actions'''
context.user_data.pop('name', None)
context.user_data.pop('unit', None)
print(general.total_time(context.user_data.pop('timer_start')))
return ConversationHandler.END
LOCATION = 0
async def weather_start(update: Update, context) -> int:
'''Initial function in /weather command pipeline'''
context.user_data['weather_start'] = time.time()
if not handle_update(update): # only need to check approval in 1st message of pipeline
await telegram_utils.send_message(bot, update.effective_chat.id, ['Unauthorized'])
return
# don't use 1 time keyboard
# location not sent after clicking button on desktop
reply_markup=ReplyKeyboardMarkup(
[[KeyboardButton(
text="Send Location",
request_location=True)
]]
)
await telegram_utils.send_message(bot, update.effective_chat.id, ['Send location or /cancel'],
reply_markup=reply_markup)
return LOCATION
async def weather_main(update: Update, context) -> int:
'''Gets weather info after location is passed'''
handle_update(update, text=False)
loc = update.message.location
lat, lng = loc['latitude'], loc['longitude']
out = weather.main(lat, lng, _key_manager.get_weather_key())
await telegram_utils.send_message(bot, update.effective_chat.id, out,
reply_markup=ReplyKeyboardRemove())
return end_weather_pipeline(context)
async def weather_cancel(update: Update, context) -> int:
'''/cancel command passed during /weather pipeline'''
handle_update(update)
await telegram_utils.send_message(bot, update.effective_chat.id, ["Weather command canceled"],
reply_markup=ReplyKeyboardRemove())
return end_weather_pipeline(context)
async def weather_timeout(update, context):
'''/weather command timeout'''
await telegram_utils.send_message(bot, update.effective_chat.id, ["Weather command timeout"],
reply_markup=ReplyKeyboardRemove())
return end_weather_pipeline(context)
def end_weather_pipeline(context):
'''End weather conversation actions'''
print(general.total_time(context.user_data.pop('weather_start')))
return ConversationHandler.END
LOWER, UPPER, NUMS = range(3)
async def rng_start(update: Update, context):
'''Initial function in /random command pipeline, asks for lower bound'''
context.user_data['rng_start'] = time.time()
if not handle_update(update): # only need to check approval in 1st message of pipeline
await telegram_utils.send_message(bot, update.effective_chat.id, ['Unauthorized'])
return
await telegram_utils.send_message(bot, update.effective_chat.id,
['Enter lower bound or /cancel'])
return LOWER
async def rng_lower(update: Update, context):
'''Stores lower bound and asks for upper bound'''
context.user_data['lower'] = update.message.text
handle_update(update)
await telegram_utils.send_message(bot, update.effective_chat.id,
['Enter upper bound or /cancel'])
return UPPER
async def rng_upper(update: Update, context):
'''Stores upper bound and asks for nums'''
context.user_data['upper'] = update.message.text
handle_update(update)
await telegram_utils.send_message(bot, update.effective_chat.id,
['Enter number of values or /cancel'])
return NUMS
async def rng_nums(update: Update, context):
'''Calls main code to generate random numbers, clears stored data'''
handle_update(update)
user_data = context.user_data
lower = user_data['lower']
upper = user_data['upper']
nums = update.message.text
out = random_number_generator.main([lower,upper,nums])
await telegram_utils.send_message(bot, update.effective_chat.id, out)
return end_rng_pipeline(context)
async def rng_cancel(update: Update, context):
'''/cancel command passed during /random pipeline'''
handle_update(update)
await telegram_utils.send_message(bot, update.effective_chat.id,
["RNG command canceled"])
return end_rng_pipeline(context)
async def rng_timeout(update: Update, context):
'''/random command timeout'''
await telegram_utils.send_message(bot, update.effective_chat.id,
["RNG command timeout"])
return end_rng_pipeline(context)
def end_rng_pipeline(context):
'''Ends rng conversation actions'''
user_data = context.user_data
user_data.pop('lower', None)
user_data.pop('upper', None)
print(general.total_time(user_data.pop('rng_start')))
return ConversationHandler.END
async def received_command(update:Update, _):
'''General function for all received commands'''
# pylint: disable=too-many-branches
start = time.time()
approved = handle_update(update)
cmd = update.message.text.lower().split()[0][1:]
out = []
if cmd == 'start':
out = ['<b>Welcome!</b>']
if not approved:
out.append('Contact admin for approval!')
out.append(f'Provide id # {update.effective_chat.id}')
else:
out += help_msg()
else:
if approved:
if cmd == 'help':
out = help_msg()
elif cmd == 'status':
out = ['Online']
elif cmd == 'todo':
user = _user_manager.get_user_from_telegram_id(update.effective_chat.id)
if user.canvas_status == 'inactive':
await telegram_utils.send_message(bot, update.effective_chat.id,
['Not registered for Canvas todos. Contact admin!'])
else:
messages = todos.main('all', user.canvas_key, user.canvas_url)
await telegram_utils.send_message(bot, update.effective_chat.id, messages)
elif cmd == 'news':
out = news.main(_key_manager.get_news_key())
elif cmd == 'nasa':
filename, title = nasa.main(_key_manager.get_nasa_key())
if filename is None:
await telegram_utils.send_message(bot, update.effective_chat.id,
['No image today!'])
else:
with open(filename, 'rb') as image:
await telegram_utils.send_photo(bot, update.effective_chat.id, title, image)
elif cmd == 'joke':
out = jokes.main()
else:
print(f'unknown cmd provided - {cmd} - check code')
else:
out = ['Unauthorized']
if cmd != 'nasa':
await telegram_utils.send_message(bot, update.effective_chat.id, out,
disable_web_page_preview=True
if cmd=='news' else None)
print(general.total_time(start))
async def unknown_command(update: Update, _):
'''Received unrecognized command'''
start = time.time()
if handle_update(update):
await telegram_utils.send_message(bot, update.effective_chat.id,
['Command not recognized'] + help_msg())
else:
await telegram_utils.send_message(bot, update.effective_chat.id, ['Unauthorized'])
print(general.total_time(start))
async def message(update: Update, _):
'''Received message, not command'''
start = time.time()
if handle_update(update):
await telegram_utils.send_message(bot, update.effective_chat.id,
[r'Please enter a command (starting with /)']
+ help_msg())
else:
await telegram_utils.send_message(bot, update.effective_chat.id, ['Unauthorized'])
print(general.total_time(start))
CHAT_TIMEOUT = 30
INTEGERS_REGEX = r'^-?\d+$'
if __name__ == '__main__':
print(datetime.datetime.now())
wait_for_internet.main()
_user_manager = user_manager.UserManager(os.path.join('resources', 'user_info.json'))
_key_manager = key_manager.KeyManager(os.path.join('resources', 'config.ini'))
persistence = PicklePersistence(store_data=PersistenceInput(bot_data=True),
filepath=os.path.join('resources', 'telegram_bot.pickle'))
app = ApplicationBuilder().token(_key_manager.get_telegram_key()).post_init(timer_init)\
.persistence(persistence).build()
bot = app.bot
timer_handler = ConversationHandler(
entry_points=[CommandHandler("timer", timer_start)],
states={
MODE: [MessageHandler(filters.TEXT & (~ filters.COMMAND) & filters.Regex("^Add$"),
timer_add),
MessageHandler(filters.TEXT & (~ filters.COMMAND) & filters.Regex("^Remove$"),
timer_remove),
MessageHandler(filters.TEXT & (~ filters.COMMAND) & filters.Regex("^List$"),
timer_list)],
NAME: [MessageHandler(filters.TEXT & (~ filters.COMMAND), timer_name)],
UNIT: [MessageHandler(filters.Regex("^Days$") | filters.Regex("^Hours$") |
filters.Regex("^Minutes$") | filters.Regex("^Seconds$"),
timer_unit)],
DURATION: [MessageHandler(filters.Regex(INTEGERS_REGEX), timer_duration)],
REMOVE: [MessageHandler(filters.Regex(INTEGERS_REGEX), timer_remove_core)],
ConversationHandler.TIMEOUT: [MessageHandler(None, timer_timeout)]
},
fallbacks=[CommandHandler("cancel", timer_cancel)],
conversation_timeout=CHAT_TIMEOUT,
)
app.add_handler(timer_handler)
rng_handler = ConversationHandler(
entry_points=[CommandHandler("random", rng_start)],
states={
LOWER: [MessageHandler(filters.Regex(INTEGERS_REGEX), rng_lower)],
UPPER: [MessageHandler(filters.Regex(INTEGERS_REGEX), rng_upper)],
NUMS: [MessageHandler(filters.Regex(INTEGERS_REGEX), rng_nums)],
ConversationHandler.TIMEOUT: [MessageHandler(None, rng_timeout)]
},
fallbacks=[CommandHandler("cancel", rng_cancel)],
conversation_timeout=CHAT_TIMEOUT
)
app.add_handler(rng_handler)
weather_handler = ConversationHandler(
entry_points=[CommandHandler("weather", weather_start)],
states={
LOCATION: [MessageHandler(filters.LOCATION, weather_main)],
ConversationHandler.TIMEOUT: [MessageHandler(None, weather_timeout)]
},
fallbacks=[CommandHandler("cancel", weather_cancel)],
conversation_timeout=CHAT_TIMEOUT
)
app.add_handler(weather_handler)
app.add_handler(CommandHandler("start", received_command))
app.add_handler(CommandHandler("help", received_command))
app.add_handler(CommandHandler("status", received_command))
app.add_handler(CommandHandler("todo", received_command))
app.add_handler(CommandHandler("news", received_command))
app.add_handler(CommandHandler("nasa", received_command))
app.add_handler(CommandHandler("joke", received_command))
app.add_handler(MessageHandler(filters.COMMAND, unknown_command))
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), message))
try:
app.run_polling()
except NetworkError:
print('network error - exiting')
sys.exit()