2016-01-04 01:56:22 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
from telegram.ext import Updater, CommandHandler
|
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
# Enable logging
|
2016-05-22 18:26:57 +02:00
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
2016-12-13 21:57:37 +01:00
|
|
|
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.
|
2018-09-21 08:57:01 +02:00
|
|
|
def start(update, context):
|
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
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def alarm(context):
|
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
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def set_timer(update, context):
|
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
|
|
|
|
2019-08-23 21:09:46 +02:00
|
|
|
# Add job to queue and stop current one if there is a timer already
|
|
|
|
if 'job' in context.chat_data:
|
|
|
|
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
|
2016-05-25 22:51:13 +02:00
|
|
|
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('Timer successfully set!')
|
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
|
|
|
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
def unset(update, context):
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Remove the job if the user changed their mind."""
|
2018-09-21 08:57:01 +02:00
|
|
|
if 'job' not in context.chat_data:
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('You have no active timer')
|
2016-05-25 22:51:13 +02:00
|
|
|
return
|
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
job = context.chat_data['job']
|
2016-05-25 22:51:13 +02:00
|
|
|
job.schedule_removal()
|
2018-09-21 08:57:01 +02:00
|
|
|
del context.chat_data['job']
|
2016-07-15 01:46:27 +02:00
|
|
|
|
2016-09-24 15:32:22 +02:00
|
|
|
update.message.reply_text('Timer successfully unset!')
|
2016-05-25 22:51:13 +02:00
|
|
|
|
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
def main():
|
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.
|
|
|
|
# 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)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
# Get the dispatcher to register handlers
|
|
|
|
dp = updater.dispatcher
|
|
|
|
|
|
|
|
# on different commands - answer in Telegram
|
2016-04-28 17:29:34 +02:00
|
|
|
dp.add_handler(CommandHandler("start", start))
|
|
|
|
dp.add_handler(CommandHandler("help", start))
|
2017-10-20 20:24:00 +02:00
|
|
|
dp.add_handler(CommandHandler("set", set_timer,
|
2016-12-13 21:57:37 +01:00
|
|
|
pass_args=True,
|
|
|
|
pass_job_queue=True,
|
|
|
|
pass_chat_data=True))
|
|
|
|
dp.add_handler(CommandHandler("unset", unset, pass_chat_data=True))
|
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()
|