17
17
bot.
18
18
"""
19
19
20
- from telegram .ext import Updater , CommandHandler
20
+ from telegram .ext import Updater , CommandHandler , Job
21
21
import logging
22
22
23
23
# Enable logging
24
24
logging .basicConfig (format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,
25
- level = logging .INFO )
25
+ level = logging .DEBUG )
26
26
27
27
logger = logging .getLogger (__name__ )
28
- job_queue = None
28
+ timers = dict ()
29
29
30
30
31
31
# Define a few command handlers. These usually take the two arguments bot and
@@ -34,46 +34,58 @@ def start(bot, update):
34
34
bot .sendMessage (update .message .chat_id , text = 'Hi! Use /set <seconds> to ' 'set a timer' )
35
35
36
36
37
- def set (bot , update , args ):
38
- """ Adds a job to the queue """
37
+ def set (bot , update , args , job_queue ):
38
+ """Adds a job to the queue"""
39
39
chat_id = update .message .chat_id
40
40
try :
41
41
# args[0] should contain the time for the timer in seconds
42
42
due = int (args [0 ])
43
43
if due < 0 :
44
44
bot .sendMessage (chat_id , text = 'Sorry we can not go back to future!' )
45
45
46
- def alarm (bot ):
47
- """ Inner function to send the alarm message """
46
+ def alarm (bot , job ):
47
+ """Inner function to send the alarm message"""
48
48
bot .sendMessage (chat_id , text = 'Beep!' )
49
49
50
50
# Add job to queue
51
- job_queue .put (alarm , due , repeat = False )
51
+ job = Job (alarm , due , repeat = False )
52
+ timers [chat_id ] = job
53
+ job_queue .put (job )
54
+
52
55
bot .sendMessage (chat_id , text = 'Timer successfully set!' )
53
56
54
- except IndexError :
55
- bot .sendMessage (chat_id , text = 'Usage: /set <seconds>' )
56
- except ValueError :
57
+ except (IndexError , ValueError ):
57
58
bot .sendMessage (chat_id , text = 'Usage: /set <seconds>' )
58
59
59
60
61
+ def unset (bot , update ):
62
+ """Removes the job if the user changed their mind"""
63
+ chat_id = update .message .chat_id
64
+
65
+ if chat_id not in timers :
66
+ bot .sendMessage (chat_id , text = 'You have no active timer' )
67
+ return
68
+
69
+ job = timers [chat_id ]
70
+ job .schedule_removal ()
71
+ bot .sendMessage (chat_id , text = 'Timer successfully unset!' )
72
+
73
+
60
74
def error (bot , update , error ):
61
75
logger .warn ('Update "%s" caused error "%s"' % (update , error ))
62
76
63
77
64
78
def main ():
65
- global job_queue
66
-
67
79
updater = Updater ("TOKEN" )
68
- job_queue = updater .job_queue
69
80
70
81
# Get the dispatcher to register handlers
71
82
dp = updater .dispatcher
72
83
73
84
# on different commands - answer in Telegram
74
85
dp .add_handler (CommandHandler ("start" , start ))
75
86
dp .add_handler (CommandHandler ("help" , start ))
76
- dp .add_handler (CommandHandler ("set" , set , pass_args = True ))
87
+ dp .add_handler (CommandHandler ("set" , set , pass_args = True , pass_job_queue = True ))
88
+ dp .add_handler (CommandHandler ("unset" , unset ))
77
89
78
90
# log all errors
79
91
dp .add_error_handler (error )
0 commit comments