import network, time, urequests
from machine import Pin,ADC
from utime import sleep
from utelegram import Bot
TOKEN ="8173182210:AAGSl5RwCYOHXT55Y08v839FsxU_EDKQoq4"
bot = Bot(TOKEN)
azul= Pin(17, Pin.OUT)
morado= Pin(18, Pin.OUT)
def conectaWifi (red, password):
      global miRed
      miRed = network.WLAN(network.STA_IF)     
      if not miRed.isconnected():              #Si no está conectado…
          miRed.active(True)                   #activa la interface
          miRed.connect(red, password)         #Intenta conectar con la red
          print('Conectando a la red', red +"…")
          timeout = time.time ()
          while not miRed.isconnected():           #Mientras no se conecte..
              if (time.ticks_diff (time.time (), timeout) > 10):
                  return False
      return True
if conectaWifi ("Wokwi-GUEST", ""):
    print ("Conexión exitosa!")
    print('Datos de la red (IP/netmask/gw/DNS):', miRed.ifconfig())
    
    
    
    @bot.add_message_handler('Hola')
    def help(update):
      update.reply(''' Menu Principal
                      \n Para controlar los bombillos escriba:
                      \n 1: Encender led azul \U0001F600
                      \n 2: Encender led Morado \U0001F923
                      \n 3: Apagar led azul \U0001F972
                      \n 4: Apagar led Morado \U0001F911
                      \n 5: Ver estado del bombillo \U0001F911
                      ''')
    @bot.add_message_handler('1')
    def help(update):
        azul.on()
        update.reply('Led Azul Encendido')
    @bot.add_message_handler('2')
    def help(update):
        morado.on()
        update.reply('Led Morado Encendido')
    @bot.add_message_handler('3')
    def help(update):
        azul.off()
        update.reply('Led Azul Apagado')
    @bot.add_message_handler('4')
    def help(update):
        morado.off()
        update.reply('Led Azul Apagado')
    @bot.add_message_handler('5')
    def help(update):
        dato = azul.value()
        if dato ==1:
          update.reply('El bombillo azul esta encendido')
        else:
          update.reply('El bombillo azul esta apagado')
    bot.start_loop()
    
 
else:
       print ("Imposible conectar")
       miRed.active (False)