-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushNotifications.py
More file actions
45 lines (29 loc) · 1.57 KB
/
pushNotifications.py
File metadata and controls
45 lines (29 loc) · 1.57 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
#!/usr/bin/env python
import telegram.ext
from telegram.ext import Updater
from tips import generic_tip
registered_users = {}
def messageCallback (context, user_id) :
job = context.job
if user_id in registered_users :
reply_message = "Ciao, ecco qui un suggerimento su cosa potresti fare!\n\n" + generic_tip.pick_tip ()
context.bot.send_message (chat_id=user_id, text=reply_message)
else:
job.schedule_removal()
context.bot.send_message (chat_id=user_id, text="Notifica rimossa.")
def add_notification_handler (updater, context) :
user_id = updater.message.from_user.id
if user_id in registered_users :
context.bot.send_message (chat_id=user_id, text="Ops, qualcosa è andato storto: è stato già registrato un promemoria!")
else :
lambda_message_callback = lambda ctxt : messageCallback (ctxt, user_id)
registered_users[user_id] = context.job_queue.run_repeating (lambda_message_callback, interval=86400, first=5)
context.bot.send_message (chat_id=user_id, text="Promemoria impostato!")
def remove_notification_handler (updater, context) :
user_id = updater.message.from_user.id
if user_id in registered_users :
registered_users[user_id].schedule_removal ()
del registered_users[user_id]
context.bot.send_message (chat_id=user_id, text="Hai eliminato il promemoria giornaliero.")
else:
context.bot.send_message (chat_id=user_id, text="Ops, qualcosa è andato storto: nessun promemoria attualmente registrato.")