telegramapi/main.py

35 lines
921 B
Python

try:
import urequests as requests #For MicroPython
except :
import requests
import json
with open("config.json", 'r') as f:
config = json.load(f)
offset = "0"
while True:
#long poll for response
querry = f'limit=1&offset={offset}&timeout=10'
try:
resp = requests.get(url=f'https://api.telegram.org/bot{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)
#echo message
querry = f'chat_id={config["recipient"]}&text={text}'
requests.get(url=f'https://api.telegram.org/bot{config["token"]}/{"sendMessage"}?{querry}')
except Exception as e:
print(e)
resp.close()