mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-18 15:20:42 +01:00
Update timerbot.py to v13.0 (#2149)
* Update timerbot example (#2144) * update timerbot example (suggestions from review) Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
This commit is contained in:
parent
3b4559dd95
commit
b554f1a85d
2 changed files with 23 additions and 20 deletions
|
@ -83,6 +83,7 @@ The following wonderful people contributed directly or indirectly to this projec
|
||||||
- `sooyhwang <https://github.com/sooyhwang>`_
|
- `sooyhwang <https://github.com/sooyhwang>`_
|
||||||
- `syntx <https://github.com/syntx>`_
|
- `syntx <https://github.com/syntx>`_
|
||||||
- `thodnev <https://github.com/thodnev>`_
|
- `thodnev <https://github.com/thodnev>`_
|
||||||
|
- `Timur Kushukov <https://github.com/timqsh>`_
|
||||||
- `Trainer Jono <https://github.com/Tr-Jono>`_
|
- `Trainer Jono <https://github.com/Tr-Jono>`_
|
||||||
- `Valentijn <https://github.com/Faalentijn>`_
|
- `Valentijn <https://github.com/Faalentijn>`_
|
||||||
- `voider1 <https://github.com/voider1>`_
|
- `voider1 <https://github.com/voider1>`_
|
||||||
|
|
|
@ -42,6 +42,16 @@ def alarm(context):
|
||||||
context.bot.send_message(job.context, text='Beep!')
|
context.bot.send_message(job.context, text='Beep!')
|
||||||
|
|
||||||
|
|
||||||
|
def remove_job_if_exists(name, context):
|
||||||
|
"""Remove job with given name. Returns whether job was removed."""
|
||||||
|
current_jobs = context.job_queue.get_jobs_by_name(name)
|
||||||
|
if not current_jobs:
|
||||||
|
return False
|
||||||
|
for job in current_jobs:
|
||||||
|
job.schedule_removal()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def set_timer(update, context):
|
def set_timer(update, context):
|
||||||
"""Add a job to the queue."""
|
"""Add a job to the queue."""
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
|
@ -52,14 +62,13 @@ def set_timer(update, context):
|
||||||
update.message.reply_text('Sorry we can not go back to future!')
|
update.message.reply_text('Sorry we can not go back to future!')
|
||||||
return
|
return
|
||||||
|
|
||||||
# Add job to queue and stop current one if there is a timer already
|
job_removed = remove_job_if_exists(str(chat_id), context)
|
||||||
if 'job' in context.chat_data:
|
context.job_queue.run_once(alarm, due, context=chat_id, name=str(chat_id))
|
||||||
old_job = context.chat_data['job']
|
|
||||||
old_job.schedule_removal()
|
|
||||||
new_job = context.job_queue.run_once(alarm, due, context=chat_id)
|
|
||||||
context.chat_data['job'] = new_job
|
|
||||||
|
|
||||||
update.message.reply_text('Timer successfully set!')
|
text = 'Timer successfully set!'
|
||||||
|
if job_removed:
|
||||||
|
text += ' Old one was removed.'
|
||||||
|
update.message.reply_text(text)
|
||||||
|
|
||||||
except (IndexError, ValueError):
|
except (IndexError, ValueError):
|
||||||
update.message.reply_text('Usage: /set <seconds>')
|
update.message.reply_text('Usage: /set <seconds>')
|
||||||
|
@ -67,15 +76,10 @@ def set_timer(update, context):
|
||||||
|
|
||||||
def unset(update, context):
|
def unset(update, context):
|
||||||
"""Remove the job if the user changed their mind."""
|
"""Remove the job if the user changed their mind."""
|
||||||
if 'job' not in context.chat_data:
|
chat_id = update.message.chat_id
|
||||||
update.message.reply_text('You have no active timer')
|
job_removed = remove_job_if_exists(str(chat_id), context)
|
||||||
return
|
text = 'Timer successfully cancelled!' if job_removed else 'You have no active timer.'
|
||||||
|
update.message.reply_text(text)
|
||||||
job = context.chat_data['job']
|
|
||||||
job.schedule_removal()
|
|
||||||
del context.chat_data['job']
|
|
||||||
|
|
||||||
update.message.reply_text('Timer successfully unset!')
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -91,10 +95,8 @@ def main():
|
||||||
# on different commands - answer in Telegram
|
# on different commands - answer in Telegram
|
||||||
dp.add_handler(CommandHandler("start", start))
|
dp.add_handler(CommandHandler("start", start))
|
||||||
dp.add_handler(CommandHandler("help", start))
|
dp.add_handler(CommandHandler("help", start))
|
||||||
dp.add_handler(
|
dp.add_handler(CommandHandler("set", set_timer))
|
||||||
CommandHandler("set", set_timer, pass_args=True, pass_job_queue=True, pass_chat_data=True)
|
dp.add_handler(CommandHandler("unset", unset))
|
||||||
)
|
|
||||||
dp.add_handler(CommandHandler("unset", unset, pass_chat_data=True))
|
|
||||||
|
|
||||||
# Start the Bot
|
# Start the Bot
|
||||||
updater.start_polling()
|
updater.start_polling()
|
||||||
|
|
Loading…
Reference in a new issue