Skip to content

Commit a37add3

Browse files
committed
[ci skip] update timerbot.py to use chat_data
1 parent 71530f4 commit a37add3

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

examples/timerbot.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
# Enable logging
2424
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
25-
level=logging.DEBUG)
25+
level=logging.INFO)
2626

2727
logger = logging.getLogger(__name__)
28-
timers = dict()
2928

3029

3130
# Define a few command handlers. These usually take the two arguments bot and
@@ -39,7 +38,7 @@ def alarm(bot, job):
3938
bot.sendMessage(job.context, text='Beep!')
4039

4140

42-
def set(bot, update, args, job_queue):
41+
def set(bot, update, args, job_queue, chat_data):
4342
"""Adds a job to the queue"""
4443
chat_id = update.message.chat_id
4544
try:
@@ -51,7 +50,7 @@ def set(bot, update, args, job_queue):
5150

5251
# Add job to queue
5352
job = Job(alarm, due, repeat=False, context=chat_id)
54-
timers[chat_id] = job
53+
chat_data['job'] = job
5554
job_queue.put(job)
5655

5756
update.message.reply_text('Timer successfully set!')
@@ -60,23 +59,22 @@ def set(bot, update, args, job_queue):
6059
update.message.reply_text('Usage: /set <seconds>')
6160

6261

63-
def unset(bot, update):
62+
def unset(bot, update, chat_data):
6463
"""Removes the job if the user changed their mind"""
65-
chat_id = update.message.chat_id
6664

67-
if chat_id not in timers:
65+
if 'job' not in chat_data:
6866
update.message.reply_text('You have no active timer')
6967
return
7068

71-
job = timers[chat_id]
69+
job = chat_data['job']
7270
job.schedule_removal()
73-
del timers[chat_id]
71+
del chat_data['job']
7472

7573
update.message.reply_text('Timer successfully unset!')
7674

7775

7876
def error(bot, update, error):
79-
logger.warn('Update "%s" caused error "%s"' % (update, error))
77+
logger.warning('Update "%s" caused error "%s"' % (update, error))
8078

8179

8280
def main():
@@ -88,8 +86,11 @@ def main():
8886
# on different commands - answer in Telegram
8987
dp.add_handler(CommandHandler("start", start))
9088
dp.add_handler(CommandHandler("help", start))
91-
dp.add_handler(CommandHandler("set", set, pass_args=True, pass_job_queue=True))
92-
dp.add_handler(CommandHandler("unset", unset))
89+
dp.add_handler(CommandHandler("set", set,
90+
pass_args=True,
91+
pass_job_queue=True,
92+
pass_chat_data=True))
93+
dp.add_handler(CommandHandler("unset", unset, pass_chat_data=True))
9394

9495
# log all errors
9596
dp.add_error_handler(error)

0 commit comments

Comments
 (0)