informatica:telegram:bot
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
informatica:telegram:bot [2021/11/28 21:18] – created jose | informatica:telegram:bot [2023/03/20 14:13] (current) – jose | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Fuentes: | ||
+ | |||
+ | https:// | ||
+ | |||
+ | ====== Crear el bot ====== | ||
Buscamos @botfather en Telegram | Buscamos @botfather en Telegram | ||
Escribimos | Escribimos | ||
/newbot | /newbot | ||
- | Nos contesta: | + | Nos contesta |
Alright, a new bot. How are we going to call it? Please choose a name for your bot. | Alright, a new bot. How are we going to call it? Please choose a name for your bot. | ||
- | Ponemos el nombre | + | Ponemos el nombre: |
- | | + | |
Nos contesta: | Nos contesta: | ||
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. | Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. | ||
+ | El username no puede tener espacios y tiene que acaba con la palabra **bot**:Le ponemos: | ||
jurjurbot | jurjurbot | ||
- | Nos dice que está creado y nos da el API del bot | + | Nos dice que está creado y nos da el token del bot que debemos guardar. |
< | < | ||
Done! Congratulations on your new bot. You will find it at t.me/ | Done! Congratulations on your new bot. You will find it at t.me/ | ||
Line 21: | Line 27: | ||
</ | </ | ||
- | Metemos usuario en el grupo y sacamos el nombre del grupo con el comando: | + | Metemos usuario |
curl -X GET https:// | curl -X GET https:// | ||
Por ejemplo: | Por ejemplo: | ||
Line 45: | Line 51: | ||
</ | </ | ||
- | El nombre IDl grupo es: | + | El ID del grupo es: |
- | -648234144 | + | < |
+ | -648234144 | ||
+ | </ | ||
Puedo enviar un mensaje con el comando: | Puedo enviar un mensaje con el comando: | ||
curl -s -X POST https:// | curl -s -X POST https:// | ||
Line 60: | Line 68: | ||
URL=" | URL=" | ||
curl -s -X POST $URL -d chat_id=$CHAT_ID -d text=" | curl -s -X POST $URL -d chat_id=$CHAT_ID -d text=" | ||
+ | </ | ||
+ | |||
+ | ====== Interactuar BOT ====== | ||
+ | Podemos ver las actualizaciones del bot con la API getUpdates como hemos visto antes: | ||
+ | curl -X GET https:// | ||
+ | Pero vamos a añadir un webhook para que que haga PUSH en vez de PULL, así cada vez que escribamos algo, lo leerá y no tenemos que estar constantemente leyendo los Updates. | ||
+ | |||
+ | El webhook tiene que apuntar a un fichero que esté en un servidor con https. En este caso usaremos php. Para añadir el webhook: | ||
+ | curl -X GET https:// | ||
+ | Por ejemplo: | ||
+ | curl -X GET https:// | ||
+ | Nos responderá esto: | ||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Para comprobar que esté bien lanzamos este comando. Suele fallar por el certificado. | ||
+ | curl -X GET https:// | ||
+ | | ||
+ | Si está bien, aunque el fichero esté vacio, luego lo creamos bien, dará esto: | ||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Creamos el fichero bot.php así. Cambiamos el TOKEN por el nuestro: | ||
+ | < | ||
+ | <?php | ||
+ | define(' | ||
+ | define(' | ||
+ | |||
+ | // read incoming info and grab the chatID | ||
+ | $content | ||
+ | $update | ||
+ | $chatID | ||
+ | $message | ||
+ | |||
+ | // compose reply | ||
+ | $reply =""; | ||
+ | switch ($message) { | ||
+ | case "/ | ||
+ | $reply = urlencode(" | ||
+ | break; | ||
+ | case "/ | ||
+ | $reply = urlencode(" | ||
+ | break; | ||
+ | case "/ | ||
+ | $reply = " | ||
+ | break; | ||
+ | case "/ | ||
+ | $reply = " | ||
+ | break; | ||
+ | default: | ||
+ | $reply = " | ||
+ | } | ||
+ | |||
+ | // send reply | ||
+ | $sendto =API_URL." | ||
+ | file_get_contents($sendto); | ||
+ | |||
+ | // Create a debug log.txt to check the response/ | ||
+ | // You can disable it by commenting checkJSON. | ||
+ | checkJSON($chatID, | ||
+ | function checkJSON($chatID, | ||
+ | |||
+ | $myFile = " | ||
+ | $updateArray = print_r($update, | ||
+ | $fh = fopen($myFile, | ||
+ | fwrite($fh, $chatID ." | ||
+ | fwrite($fh, $updateArray." | ||
+ | fclose($fh); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Cositas de PHP: | ||
+ | * Ponemos urlencode para poder poner \n y escribir un salto de línea | ||
+ | * Usamos trim para quitar una _ que aparece al leer una url como si fuera un salto de línea | ||
+ | < | ||
+ | trim(file_get_contents(" | ||
+ | </ | ||
+ | |||
+ | ====== Menus con botfather ====== | ||
+ | Desde el bot @botfather podemos añadir los menús para que aparezcan los comandos. | ||
+ | |||
+ | ====== API en PHP ====== | ||
+ | El webhook de telegram lanza peticiones a la API de PHP de este tipo con todas las acciones que pasan. Metiendo el bot @RawDataBot se pueden sacar de cada grupo. Va bien para saber el chatID por ejemplo: | ||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | A nosotros solo nos interesa uno como este | ||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Si lo lanzamos a la API de PHP haría el comando **/ | ||
+ | curl -X POST -d@peticion.json " | ||
+ | | ||
+ | ====== ERRORES ====== | ||
+ | El bot no da respuesta y en nginx da 499 | ||
+ | curl -s -X GET https:// | ||
+ | |||
+ | Da error "Read timeout expired", | ||
+ | |||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Era un problema de DNS pero para vaciar los mensajes pendientes he puesto en el fichero bot.php | ||
+ | < | ||
+ | $update = json_decode(file_get_contents(' | ||
+ | | ||
+ | if(time()-((@$update-> | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
</ | </ |
informatica/telegram/bot.1638134309.txt.gz · Last modified: 2021/11/28 21:18 by jose