From cb3a7f90366934109f67f7db48bffe728e26ce64 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Thu, 8 Jul 2021 23:03:36 +1200 Subject: [PATCH] Add MicroPython specific version --- up.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 up.py diff --git a/up.py b/up.py new file mode 100644 index 0000000..aa70675 --- /dev/null +++ b/up.py @@ -0,0 +1,54 @@ +try: + import urequests as requests #For MicroPython +except : + import requests +import json +import gc +from machine import WDT +from machine import Pin + +wdt = WDT(timeout=30000) +wdt.feed() +gc.enable() + +led = Pin(2, Pin.OUT) +led.on() + +with open("config.json", 'r') as f: + config = json.load(f) + +offset = "0" +while True: + gc.collect() + wdt.feed() + #long poll for response + querry = 'limit=1&offset={}&timeout=5'.format(offset) + try: + resp = requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "getUpdates", querry)) + json = resp.json() + resp.close() + if json.get("result"): + #get latest message + offset = json.get("result")[0].get("update_id") + 1 + text = json["result"][0]["message"]["text"] + print(text) + if text=="/on": + led.on() + if text=="/off": + led.off() + #echo message + querry = 'chat_id={}&text={}'.format(config["recipient"], text) + requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage", querry)) + + except Exception as e: + print(e) + resp.close() + + + + + + + + +