Skip to content

Commit d548643

Browse files
committed
use job context for timerbot example
1 parent c9ec436 commit d548643

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

examples/timerbot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def start(bot, update):
3434
bot.sendMessage(update.message.chat_id, text='Hi! Use /set <seconds> to ' 'set a timer')
3535

3636

37+
def alarm(bot, job):
38+
"""Function to send the alarm message"""
39+
bot.sendMessage(job.context, text='Beep!')
40+
41+
3742
def set(bot, update, args, job_queue):
3843
"""Adds a job to the queue"""
3944
chat_id = update.message.chat_id
@@ -43,12 +48,8 @@ def set(bot, update, args, job_queue):
4348
if due < 0:
4449
bot.sendMessage(chat_id, text='Sorry we can not go back to future!')
4550

46-
def alarm(bot, job):
47-
"""Inner function to send the alarm message"""
48-
bot.sendMessage(chat_id, text='Beep!')
49-
5051
# Add job to queue
51-
job = Job(alarm, due, repeat=False)
52+
job = Job(alarm, due, repeat=False, context=chat_id)
5253
timers[chat_id] = job
5354
job_queue.put(job)
5455

@@ -68,6 +69,8 @@ def unset(bot, update):
6869

6970
job = timers[chat_id]
7071
job.schedule_removal()
72+
del timers[chat_id]
73+
7174
bot.sendMessage(chat_id, text='Timer successfully unset!')
7275

7376

0 commit comments

Comments
 (0)