Skip to content

examples: updated timerbot to work with the latest version of the library #1119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Paul Larsen <https://github.com/PaulSonOfLars>`_
- `Pieter Schutz <https://github.com/eldinnie>`_
- `Rahiel Kasim <https://github.com/rahiel>`_
- `Rick van Schijndel <https://github.com/Mindavi>`_
- `Sascha <https://github.com/saschalalala>`_
- `Shelomentsev D <https://github.com/shelomentsevd>`_
- `Simon Schürrle <https://github.com/SitiSchu>`_
Expand Down
27 changes: 12 additions & 15 deletions examples/timerbot.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,44 @@

# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
def start(bot, update):
update.message.reply_text('Hi! Use /set <seconds> to set a timer')


def alarm(context):
def alarm(bot, job):
"""Send the alarm message."""
job = context.job
context.bot.send_message(job.context, text='Beep!')
bot.send_message(job.context, text='Beep!')


def set_timer(update, context):
def set_timer(bot, update, args, job_queue, chat_data):
"""Add a job to the queue."""
chat_id = update.message.chat_id
try:
# args[0] should contain the time for the timer in seconds
due = int(context.args[0])
due = int(args[0])
if due < 0:
update.message.reply_text('Sorry we can not go back to future!')
return

# Add job to queue
job = context.job_queue.run_once(alarm, due, context=chat_id)
context.chat_data['job'] = job
job = job_queue.run_once(alarm, due, context=chat_id)
chat_data['job'] = job

update.message.reply_text('Timer successfully set!')

except (IndexError, ValueError):
update.message.reply_text('Usage: /set <seconds>')


def unset(update, context):
def unset(bot, update, chat_data):
"""Remove the job if the user changed their mind."""
if 'job' not in context.chat_data:
if 'job' not in chat_data:
update.message.reply_text('You have no active timer')
return

job = context.chat_data['job']
job = chat_data['job']
job.schedule_removal()
del context.chat_data['job']
del chat_data['job']

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

Expand All @@ -83,9 +82,7 @@ def error(update, context):
def main():
"""Run bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater("TOKEN", use_context=True)
updater = Updater("TOKEN")

# Get the dispatcher to register handlers
dp = updater.dispatcher
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy