Paso 6: Código de Python
Telepot es un python API para trabajar con los bots del telegrama. Para más detalles, algunos códigos acerca de la API y documentación revisa el GitHub aquí.
Vamos a empezar con un Hola mundo del mundo del bot. Construiremos un bot que responderá al mensaje "/ Hola".
#!/usr/bin/python import telepot, time def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) if (content_type == 'text'): command = msg['text'] print ('Got command: %s' % command) if '/hello' in command: bot.sendMessage(chat_id, "Hello, do you have any commands for today?") # Creates a bot using the token provided by BotFather bot = telepot.Bot('16843XXXX:AAGGq99MLWOknqCx66V5s2XXXXXXXXXXXXX') # Add the handle function to be called every new received message bot.message_loop(handle) # Wait for new messages while 1: time.sleep(20)
Tenga en cuenta que en su propio código tienes que cambiar el símbolo en la línea 15 por el token suministrado por botFather. Guarde el archivo el nombre assistant.py.
Ejecutar el código aunque su terminal usando
$ python3 assistant.py
Una vez que el código lo patea es posible probar nuestros enviando un mensaje a través de cualquier dispositivo a nuestro bot (le puede encontrar con el nombre de usuario que se proporcionó a botFather antes).