~josealberto4444/apodnasabot

fa638d8f2d185847bb2a64d31e4b72cec373f3b9 — José Alberto Orejuela García 2 years ago 41b0f00
Add usage of basic functions
1 files changed, 48 insertions(+), 3 deletions(-)

M telegram_bot.py
M telegram_bot.py => telegram_bot.py +48 -3
@@ 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()