2016-01-04 01:56:22 +01:00
|
|
|
#!/usr/bin/env python
|
2021-03-13 16:21:03 +01:00
|
|
|
# pylint: disable=C0116
|
2016-01-04 01:56:22 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2019-02-13 13:38:07 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Simple Bot to send timed Telegram messages.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
This Bot uses the Updater class to handle the bot and the JobQueue to send
|
|
|
|
timed messages.
|
|
|
|
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
|
|
|
the Dispatcher and registered at their respective places.
|
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Basic Alarm Bot example, sends a message after a set time.
|
|
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
|
|
bot.
|
|
|
|
"""
|
|
|
|
|
2018-05-21 15:00:47 +02:00
|
|
|
import logging
|
2018-05-21 15:00:47 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import Update
|
|
|
|
from telegram.ext import Updater, CommandHandler, CallbackContext
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
|
|
|
)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# Define a few command handlers. These usually take the two arguments update and
|
|
|
|
# context. Error handlers also receive the raised TelegramError object in error.
|
2021-03-13 16:21:03 +01:00
|
|
|
def start(update: Update, _: CallbackContext) -> None:
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('Hi! Use /set <seconds> to set a timer')
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def alarm(context: CallbackContext) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Send the alarm message."""
|
2018-09-21 08:57:01 +02:00
|
|
|
job = context.job
|
|
|
|
context.bot.send_message(job.context, text='Beep!')
|
2016-07-15 01:46:27 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def remove_job_if_exists(name: str, context: CallbackContext) -> bool:
|
2020-10-15 18:48:12 +02:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def set_timer(update: Update, context: CallbackContext) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Add a job to the queue."""
|
2016-01-04 01:56:22 +01:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
try:
|
|
|
|
# args[0] should contain the time for the timer in seconds
|
2018-09-21 08:57:01 +02:00
|
|
|
due = int(context.args[0])
|
2016-01-05 14:36:28 +01:00
|
|
|
if due < 0:
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('Sorry we can not go back to future!')
|
2016-08-07 17:59:58 +02:00
|
|
|
return
|
2016-05-22 18:26:57 +02:00
|
|
|
|
2020-10-15 18:48:12 +02:00
|
|
|
job_removed = remove_job_if_exists(str(chat_id), context)
|
|
|
|
context.job_queue.run_once(alarm, due, context=chat_id, name=str(chat_id))
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2020-10-15 18:48:12 +02:00
|
|
|
text = 'Timer successfully set!'
|
|
|
|
if job_removed:
|
|
|
|
text += ' Old one was removed.'
|
|
|
|
update.message.reply_text(text)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
except (IndexError, ValueError):
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('Usage: /set <seconds>')
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def unset(update: Update, context: CallbackContext) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Remove the job if the user changed their mind."""
|
2020-10-15 18:48:12 +02:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
job_removed = remove_job_if_exists(str(chat_id), context)
|
|
|
|
text = 'Timer successfully cancelled!' if job_removed else 'You have no active timer.'
|
|
|
|
update.message.reply_text(text)
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Run bot."""
|
2018-09-21 08:57:01 +02:00
|
|
|
# Create the Updater and pass it your bot's token.
|
2021-02-01 17:59:39 +01:00
|
|
|
updater = Updater("TOKEN")
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
# Get the dispatcher to register handlers
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher = updater.dispatcher
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
# on different commands - answer in Telegram
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher.add_handler(CommandHandler("start", start))
|
|
|
|
dispatcher.add_handler(CommandHandler("help", start))
|
|
|
|
dispatcher.add_handler(CommandHandler("set", set_timer))
|
|
|
|
dispatcher.add_handler(CommandHandler("unset", unset))
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
# Start the Bot
|
|
|
|
updater.start_polling()
|
|
|
|
|
2017-02-18 16:03:50 +01:00
|
|
|
# Block until you press Ctrl-C or the process receives SIGINT, SIGTERM or
|
|
|
|
# SIGABRT. This should be used most of the time, since start_polling() is
|
|
|
|
# non-blocking and will stop the bot gracefully.
|
2016-01-04 01:56:22 +01:00
|
|
|
updater.idle()
|
|
|
|
|
2016-05-22 18:26:57 +02:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|