from config import utelegram_config
from config import wifi_config

import utelegram
import network
import time

debug = True


def get_message(bot, message):
    bot.send(message['message']['chat']['id'], message['message']['text'].upper())

def reply_ping(bot, message):
    print(message)
    bot.send(message['message']['chat']['id'], 'pong')

def main():
    print("Connecting to WiFi")
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)
    sta_if.connect(wifi_config['ssid'], wifi_config['password'])

    if debug: print('Wait for network')
    while not sta_if.isconnected():
        print(".", end="")
        time.sleep(0.1)
    print(" Connected!")

    bot = utelegram.ubot(utelegram_config['token'])
    bot.register('/ping', reply_ping)
    bot.set_default_handler(get_message)

    print('BOT LISTENING')
    bot.listen()

if __name__ == "__main__":
    main()