import network, time, urequests
from machine import Pin
from utime import sleep, sleep_ms
import ujson
from utelegram import Bot
from neopixel import NeoPixel 



TOKEN = '5031163680:AAG45iFduorhunFrZs6GsGGBc7QUaegYGhE'

bot = Bot(TOKEN)
np= NeoPixel(Pin(13), 1)


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('Inicio')
    def help(update):
        update.reply('''
                        \n Menu
                        \n Rojo : Red  \U0001F600
                        \n Verde : Green  \U0001F643
                        \n Azul : Blue  \U0001F607
                        
                        ''')

    @bot.add_message_handler('Red')
    def help(update):
        np[0]=(255,0,0)
        np.write()
        update.reply('''Encendiendo Rojo''')

    @bot.add_message_handler('Green')
    def help(update):
        np[0]=(0,255,0)
        np.write()
        update.reply('''Encendiendo Verde''')

    @bot.add_message_handler('Blue')
    def help(update):
        np[0]=(0,0,255)
        np.write()
        update.reply('''Encendiendo Azul''')
  
   
    bot.start_loop()
    
 
else:
       print ("Imposible conectar")
       miRed.active (False)