# Envío de mensajes a Telegram

En la anterior página, PicoBricks hacía de servidor, alojaba una página web y desde el exterior, se llamaba a su página web para encender y apagar un led.

¿y al revés? es decir, la llamada de PicoBricks a una web externa, por ejemplo la api de Telegram y así poder enviar temperatura, datos, etc.. de forma muy fácil :

- Primero [creando un bot de Telegram y consiguiendo su Token ](https://libros.catedu.es/books/arduino-alvik/page/crear-bot-en-telegram)
- Segundo [identificar nuestro ID de usuario a donde enviar el mensaje](https://libros.catedu.es/books/arduino-alvik/page/encontrar-tu-id-en-telegram)
- Tercero utilizar la instrucción urequest.get(laurl) de la librería urequests

<p class="callout info">Tienes que poner en la línea 11 los datos de tu wifi</p>

<p class="callout info">Tienes que poner en la url de la línea 16:  
- Donde pone **PONTUBOT** sustitúyelo por el token del bot que has conseguido en [reando un bot de Telegram y consiguiendo su Token ](https://libros.catedu.es/books/arduino-alvik/page/crear-bot-en-telegram)  
- Donde pone **PONTUID** sustitúyelo por el ID de tu usuario a donde hay que enviar el mensaje ver [identificar nuestro ID de usuario a donde enviar el mensaje](https://libros.catedu.es/books/arduino-alvik/page/encontrar-tu-id-en-telegram)</p>

```
## extraido del proyecto action-reaction https://github.com/Robotistan/PicoBricks/tree/main/Software/Activities/Action-Reaction
from machine import Pin#to acces the hardware picobricks 
led = Pin(7,Pin.OUT)                         #### initialize digital pin as an output for led
push_button = Pin(10,Pin.IN,Pin.PULL_DOWN)   ### initialize digital pin 10 as an input
        
##### extraido de página 21 de https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf
##### Connecttonetwork
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('PONTUWIFI', 'PONTUCONTRASEÑAWIFI')

# Make GET request
import urequests
def mandarmensaje():
    r = urequests.get("http://api.telegram.org/botPONTUBOT/sendMessage?chat_id=PONTUID&text=APRETADO")
    print(r.status_code) # redirectsto https
    #print(r.content)
    r.close() 

#### while loop #######################################    
while True:  
    logic_state = push_button.value();#button on&off status
    if logic_state == True:#check the button and if it is on
        led.value(1)#turn on the led
        mandarmensaje()
    else:
        led.value(0)#turn off the led
#### end while loop ###################################    

        
```

Hay que dejar apretado unos segundos el botón para que funcione:<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" frameborder="0" height="815" src="https://www.youtube.com/embed/cimDJiuisag" title="envio a telegram con picobricks y python" width="458"></iframe>