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 13:57] – 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 105: | Line 109: | ||
< | < | ||
<?php | <?php | ||
- | define(' | + | define(' |
define(' | define(' | ||
Line 112: | Line 116: | ||
$update | $update | ||
$chatID | $chatID | ||
- | // | ||
$message | $message | ||
Line 119: | Line 122: | ||
switch ($message) { | switch ($message) { | ||
case "/ | case "/ | ||
- | $reply = urlencode(" | + | $reply = urlencode(" |
break; | break; | ||
case "/ | case "/ | ||
- | $reply = urlencode(" | + | $reply = urlencode(" |
break; | break; | ||
- | case "/internet": | + | case "/consulta1": |
- | $reply = trim(file_get_contents(" | + | $reply = " |
break; | break; | ||
- | case "/baterias": | + | case "/consulta2": |
- | $reply = "Carga baterias: " | + | $reply = "Fichero 2: " |
- | break; | + | |
- | case "/ | + | |
- | $reply = " | + | |
- | break; | + | |
- | case "/ | + | |
- | $reply = "El consumo total es de: " | + | |
break; | break; | ||
default: | default: | ||
Line 159: | Line 156: | ||
Cositas de PHP: | Cositas de PHP: | ||
- | | + | |
+ | * 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.1638367038.txt.gz · Last modified: 2021/12/01 13:57 by jose