informatica:telegram:bot
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
informatica:telegram:bot [2021/12/01 14:08] – jose | informatica:telegram:bot [2023/03/20 14:13] (current) – jose | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Fuentes: | ||
+ | |||
+ | https:// | ||
+ | |||
====== Crear el bot ====== | ====== Crear el bot ====== | ||
Buscamos @botfather en Telegram | Buscamos @botfather en Telegram | ||
Line 4: | Line 8: | ||
Escribimos | Escribimos | ||
/newbot | /newbot | ||
- | Nos contesta que ahora le tenemos que poner un nombre | + | Nos contesta que ahora le tenemos que poner un nombre. El nombre puede ser largo y tener espacios: |
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. | ||
- | Le ponemos: | + | 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 token del bot que debemos guardar. | Nos dice que está creado y nos da el token del bot que debemos guardar. | ||
Line 23: | 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 66: | Line 70: | ||
</ | </ | ||
- | ====== | + | ====== |
Podemos ver las actualizaciones del bot con la API getUpdates como hemos visto antes: | Podemos ver las actualizaciones del bot con la API getUpdates como hemos visto antes: | ||
curl -X GET https:// | curl -X GET https:// | ||
Line 160: | Line 164: | ||
====== Menus con botfather ====== | ====== Menus con botfather ====== | ||
Desde el bot @botfather podemos añadir los menús para que aparezcan los comandos. | 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.1638367688.txt.gz · Last modified: 2021/12/01 14:08 by jose