├── README.md └── micropython_demo_bot.py /README.md: -------------------------------------------------------------------------------- 1 | # micropython_demo_bot 2 | Little example of how to create a bot for Telegram. 3 | 4 | Este es un sencillo ejemplo de como crear un bot de Telegram, utilizando la libreria python-telegram-bot (https://github.com/python-telegram-bot/python-telegram-bot). 5 | 6 | El bot esta funcionando, pueden encontrarlo en Telegram como micropython_demo_bot 7 | 8 | [![Video](https://img.youtube.com/vi/73coNZ-5hWM/0.jpg)](https://www.youtube.com/watch?v=73coNZ-5hWM) -------------------------------------------------------------------------------- /micropython_demo_bot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # This program is dedicated to the public domain under the CC0 license. 4 | # 5 | # THIS EXAMPLE HAS BEEN UPDATED TO WORK WITH THE BETA VERSION 12 OF PYTHON-TELEGRAM-BOT. 6 | # If you're still using version 11.1.0, please see the examples at 7 | # https://github.com/python-telegram-bot/python-telegram-bot/tree/v11.1.0/examples 8 | 9 | """ 10 | Simple Bot to reply to Telegram messages. 11 | 12 | First, a few handler functions are defined. Then, those functions are passed to 13 | the Dispatcher and registered at their respective places. 14 | Then, the bot is started and runs until we press Ctrl-C on the command line. 15 | 16 | Usage: 17 | Basic Echobot example, repeats messages. 18 | Press Ctrl-C on the command line or send a signal to the process to stop the 19 | bot. 20 | """ 21 | 22 | import logging 23 | 24 | from telegram.ext import Updater, CommandHandler, MessageHandler, Filters 25 | 26 | # Enable logging 27 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 28 | level=logging.INFO) 29 | 30 | logger = logging.getLogger(__name__) 31 | 32 | 33 | # Define a few command handlers. These usually take the two arguments bot and 34 | # update. Error handlers also receive the raised TelegramError object in error. 35 | def start(update, context): 36 | """Send a message when the command /start is issued.""" 37 | update.message.reply_text('Hi!') 38 | 39 | 40 | def help(update, context): 41 | """Send a message when the command /help is issued.""" 42 | update.message.reply_text('Help!') 43 | 44 | 45 | def echo(update, context): 46 | """Echo the user message.""" 47 | update.message.reply_text(update.message.text) 48 | 49 | 50 | def error(update, context): 51 | """Log Errors caused by Updates.""" 52 | logger.warning('Update "%s" caused error "%s"', update, context.error) 53 | 54 | def pizza(update, context): 55 | if(update.message.text.upper().find("MANZANAS VERDES") > 0): 56 | update.message.reply_text("Prefiero comer pizza") 57 | 58 | def sumar(update,context): 59 | try: 60 | numero1 = int(context.args[0]) 61 | numero2 = int(context.args[1]) 62 | 63 | suma = numero1 + numero2 64 | 65 | update.message.reply_text("La suma es "+str(suma)) 66 | 67 | except (ValueError): 68 | update.message.reply_text("por favor utilice dos numeros") 69 | 70 | def main(): 71 | """Start the bot.""" 72 | # Create the Updater and pass it your bot's token. 73 | # Make sure to set use_context=True to use the new context based callbacks 74 | # Post version 12 this will no longer be necessary 75 | updater = Updater("848331878:AAFVT1JWTGHIJ9_oaOtHLtCdBPA68CTtCcA", use_context=True) 76 | 77 | # Get the dispatcher to register handlers 78 | dp = updater.dispatcher 79 | 80 | # on different commands - answer in Telegram 81 | dp.add_handler(CommandHandler("start", start)) 82 | dp.add_handler(CommandHandler("help", help)) 83 | dp.add_handler(CommandHandler("sumar", sumar)) 84 | 85 | # on noncommand i.e message - echo the message on Telegram 86 | dp.add_handler(MessageHandler(Filters.text, pizza)) 87 | 88 | # log all errors 89 | dp.add_error_handler(error) 90 | 91 | # Start the Bot 92 | updater.start_polling() 93 | 94 | # Run the bot until you press Ctrl-C or the process receives SIGINT, 95 | # SIGTERM or SIGABRT. This should be used most of the time, since 96 | # start_polling() is non-blocking and will stop the bot gracefully. 97 | updater.idle() 98 | 99 | 100 | if __name__ == '__main__': 101 | main() --------------------------------------------------------------------------------