From fa638d8f2d185847bb2a64d31e4b72cec373f3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alberto=20Orejuela=20Garc=C3=ADa?= Date: Mon, 19 Aug 2019 20:36:25 +0200 Subject: [PATCH] Add usage of basic functions --- telegram_bot.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/telegram_bot.py b/telegram_bot.py index 1d213df..0e3a141 100644 --- a/telegram_bot.py +++ b/telegram_bot.py @@ -22,16 +22,61 @@ def read_config(section, key): config.read('config.cfg') return config[section][key] -def start(update, context): - context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!") - token = read_config('Telegram', 'token') updater = telegram.ext.Updater(token=token, use_context=True) dispatcher = updater.dispatcher logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) +def send_link(update: telegram.Update, context: telegram.ext.CallbackContext): + update.message.reply_text('https://www.youtube.com/embed/aiFD_LBx2nM') + +send_link_handler = telegram.ext.CommandHandler('link', send_link) +dispatcher.add_handler(send_link_handler) + +def image(update: telegram.Update, context: telegram.ext.CallbackContext): + update.message.reply_photo(photo='https://apod.nasa.gov/apod/image/1908/PerseidsPloughCow.jpg', caption='This is a title.') + +image_handler = telegram.ext.CommandHandler('image', image) +dispatcher.add_handler(image_handler) + +def start(update, context): + context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!") + start_handler = telegram.ext.CommandHandler('start', start) dispatcher.add_handler(start_handler) +def echo(update, context): + update.message.reply_text(update.message.text) + +echo_handler = telegram.ext.MessageHandler(telegram.ext.Filters.text, echo) +dispatcher.add_handler(echo_handler) + +def caps(update, context): + print(context.args) + text_caps = ' '.join(context.args).upper() + context.bot.send_message(chat_id=update.message.chat_id, text=text_caps) + +caps_handler = telegram.ext.CommandHandler('caps', caps) +dispatcher.add_handler(caps_handler) + +def callback_alarm(context: telegram.ext.CallbackContext): + context.bot.send_message(chat_id=context.job.context, text='BEEP') + +def callback_timer(update: telegram.Update, context: telegram.ext.CallbackContext): + context.bot.send_message(chat_id=update.message.chat_id, + text='Setting a timer for 1 minute!') + + context.job_queue.run_once(callback_alarm, 60, context=update.message.chat_id) + +timer_handler = telegram.ext.CommandHandler('timer', callback_timer) +dispatcher.add_handler(timer_handler) + +def unknown(update, context): + context.bot.send_message(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command.") + +unknown_handler = telegram.ext.MessageHandler(telegram.ext.Filters.command, unknown) +dispatcher.add_handler(unknown_handler) + updater.start_polling() +updater.idle() -- 2.45.2