diff --git a/README.md b/README.md index c0b4d5e..a66680e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # telegramapi +```pipenv sync``` + +```cp config.json.sample config.json``` + +Create a Telegram account if you don't have one + +https://telegram.org/ + +Search users for ```BotFather``` +Click start + +Type ```/newbot``` and follow the prompts + +Take the token under ```Use this token to access the HTTP API:``` and put it into config.json + +Search users for ```userinfobot @userinfobot``` +Put you id in config.json + +Run +```pipenv run prod``` diff --git a/main.py b/main.py new file mode 100644 index 0000000..8e105ef --- /dev/null +++ b/main.py @@ -0,0 +1,34 @@ +try: + import urequests as requests +except : + import requests +import json + +with open("config.json", 'r') as f: + config = json.load(f) + +offset = "0" +while True: + querry = "limit=1&offset={}&timeout=10".format(offset) + #long poll for response + 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) + #send acknowledgement + 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() + + + + + +