Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para...

62
a t a r e a o . e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

Transcript of Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para...

Page 1: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Bots para Telegramó como

automatizarhasta el infinito y más

allá.(sin morir en el intento)

Page 2: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Introducción

Page 3: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

¿Que es un bot?● Una aplicación informática,un script, una app

dentro de otra app● Una aplicación de terceros que funciona

dentro de Telegram

Page 4: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

¿Que podemos hacer con un bot?● Los usuarios pueden interactuar con los

bots– Mediante mensajes

– Comandos y botones

– Con llamadas en línea

Page 5: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

¿Que podemos hacer con un bot?● Enviar notificaciones y noticias● Integrar otros servicios (Gmail bot, Image bot, IMDB

bot, Music bot, Wiki bot)● Herramienas personalizadas (Markdown bot, Stiker

bot, Vote bot, Like bot)● Creación de juegos (GameBot)● Servicios (HotOrBot)

Page 6: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

¿Como funcionan los bots?● Las peticiones que el usuario envía al bot son

procesadas por los servidores de Telegram● ¿Como nos enteramos de estas peticiones?

Dos opciones excluyentes:– Le preguntamos a Telegram: getUpdates

– Telegram nos avisa: Webhooks

Page 7: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Crear un bot

Page 8: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Crear un bot● Para crear un bot utilizaremos un bot.

– BotFather

● /newbot– name: Linux Center

– username: linux_center_bot

● https://t.me/linux_center_bot● Token:

716851483:AAGaXrbms19lhCbaqoR7tdCxCwvUsJtTf7c

Page 9: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Page 10: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Personarlizar nuestro bot● /setname para cambiar el nombre al bot● /setdescription para cambiar la descripción de

nuestro bot● /setabouttext para cambiar la información que

aparece en el acerca de● /setuserpic para cambiar la imagen de perfil de

nuestro bot

Page 11: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Como interactuar con el bot● Llamadas a la API

– Agnóstico al lenguaje de programación

● getUpdates– Python – python-telegram-bot (framework)

● Webooks– Python

– PHP

Page 12: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Insomnia

Page 13: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Insomnia● Un cliente REST API sencillo y potente● Permite crear llamadas HTTP requests● Ver la respuesta de forma detalla● Sobre todo: ORGANIZACIÓN

Page 14: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Insomnia

Page 15: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Llamadas a la API

Page 16: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Llamadas a la API● https://core.telegram.org/bots/api#available-

methods● getMe

– https://api.telegram.org/bot<token>/getMe

● getUpdates– https://api.telegram.org/bot<token>/getUpdates

– Nada mas crear no hay nada

Page 17: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Crear un nuevo grupo● Creamos un nuevo grupo y añadimos a nuestro bot

– getUpdates se ha actualizado

● Enviemos un mensaje– https://api.telegram.org/bot <token>/sendMessage

● “chat_id”:”-123456789” / grupospublicos: @nombre– el bot tiene que ser miembro del grupo o canal.

● “text”:”Ejemplo”

Page 18: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Crear un nuevo grupo● Creamos un nuevo grupo y añadimos a nuestro bot

– getUpdates se ha actualizado

● Enviemos un mensaje– https://api.telegram.org/bot <token>/sendMessage

● “chat_id”:”-123456789” / grupospublicos: @nombre– el bot tiene que ser miembro del grupo o canal.

● “text”:”Ejemplo”

Page 19: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Hagamos llamadas a la API● sendMessage

– Opciones de formateado

● sendPhoto● sendAudio● sendDocument● SendVoice● Otros

Page 20: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Hasta 40 llamadas a la API● getMe● sendMessage● Formatting options● forwardMessage● sendPhoto● sendAudio● sendDocument● sendVideo● sendAnimation● SendVoice

● sendVideoNote● SendMediaGroup● sendLocation● editMessageLiveLocation● stopMessageLiveLocation● sendVenue● sendContact● sendChatAction● getUserProfilePhotos● getFile

● KickChatMember● unbanChatMember● restrictChatMember● promoteChatMember● ExportChatInviteLink● setChatPhoto● deleteChatPhoto● setChatTitle● setChatDescription● pinChatMessage

● unpinChatMessage● leaveChat● getChat● getChatAdministrators● getChatMembersCount● getChatMember● setChatStickerSet● deleteChatStickerSet● answerCallbackQuery● Inline mode methods

https://core.telegram.org/bots/api#available-methods

Page 21: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

Page 22: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdateshttps://github.com/python-telegram-bot/python-telegram-bot/

https://github.com/atareao/senderbot

Page 23: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

● Tres objetos clave– Bot. El bot �– Updater. Se encarga de interactuar con el bot

– Handler. Se encarga de interactuar con el humano

Page 24: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

El bot

Page 25: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

Los métodos

Page 26: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

Los métodos

Page 27: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

Los métodos

Page 28: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

Los métodos

Page 29: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

(1)

(2)

(3)

(4)

Page 30: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

getUpdates

https://github.com/atareao/senderbot

● Handlers– CommandHandler. Comandos

– MessageHandler. Mensajes

– ConversationHandler. Conversaciones

– CallbackQueryHandler. Respuestas

Page 31: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CommandHandler

Page 32: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CommandHandler

Page 33: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CommandHandler

Page 34: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CommandHandler

Page 35: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CommandHandler

Page 36: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

MessageHandler

● Parámetros– Filters

● Filters.text● Filters.audio● Filters.photo● Filters.document● Filters.command

– callback

Page 37: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

MessageHandler

Page 38: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

MessageHandler

Page 39: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

MessageHandler

Page 40: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

MessageHandler

Page 41: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CallbackQueryHandler

Page 42: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CallbackQueryHandler

Page 43: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CallbackQueryHandler

Page 44: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

esgetUpdates

https://github.com/atareao/senderbot

CallbackQueryHandler

Page 45: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks

Page 46: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks

https://github.com/atareao/senderbot

● Necesitas un dominio (o ddns)● Puerto abierto

– Telegram solo trabaja con 443, 80, 88, 8443

● Cifrado SSL/TTLS (puede ser autofirmado, también Let’s Encrypt)

Page 47: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks

https://github.com/atareao/senderbot

● Métodos para trabajar con tu webhook– setWebhook

– deleteWebhook

– getWebhookInfo

Page 48: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks

https://github.com/atareao/senderbot

Page 49: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Python

Page 50: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - Python

Page 51: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - Python

Page 52: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - Python

Page 53: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - Python

Page 54: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

PHP

Page 55: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - PHP2 funciones clave para las llamadas a la API

Page 56: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - PHP

Page 57: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - PHPEl web hook

(integrado con WordPress)

Page 58: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Webhooks - PHPMétodos

Page 59: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Recursos

Page 60: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Recursos

https://github.com/atareao/senderbot

● https://www.atareao.es/tutorial/crea-tu-propio-bot-para-telegram/

● https://github.com/atareao/senderbot

● https://github.com/python-telegram-bot/python-telegram-bot/

Page 61: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Agradecimientos

Page 62: Bots para Telegram ó como automatizar hasta el infinito y ... · a t a r e a o. e s Bots para Telegram ó como automatizar hasta el infinito y más allá. (sin morir en el intento)

atar

eao.

es

Agradecimientos