From 23a8f4e5fdb50feccac468af3acc92bf7884358a Mon Sep 17 00:00:00 2001 From: jmsingh6871 <43915113+jmsingh6871@users.noreply.github.com> Date: Tue, 15 Oct 2019 02:20:58 +0530 Subject: [PATCH] Create my first pr.py --- my first pr.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 my first pr.py diff --git a/my first pr.py b/my first pr.py new file mode 100644 index 0000000..4bb9a18 --- /dev/null +++ b/my first pr.py @@ -0,0 +1,47 @@ +import json +import requests + +TOKEN = "Your Token" +URL = "https://api.telegram.org/bot{}/".format(TOKEN) + + +def get_url(url): + response = requests.get(url) + content = response.content.decode("utf8") + return content + + +def get_json_from_url(url): + content = get_url(url) + js = json.loads(content) + return js + + +def get_updates(): + url = URL + "getUpdates" + js = get_json_from_url(url) + return js + + +def get_last_chat_id_and_text(updates): + num_updates = len(updates["result"]) + last_update = num_updates - 1 + text = updates["result"][last_update]["message"]["text"] + chat_id = updates["result"][last_update]["message"]["chat"]["id"] + return (text, chat_id) + + +#def send_message(text, chat_id): + #url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id) + #get_url(url) + +def send_message(text, chat_id): + fmssg="welcome to Script" + if text=="start": + url = URL + "sendMessage?text={}&chat_id={}".format(fmssg, chat_id) + get_url(url) + +global text +global chat +text, chat = get_last_chat_id_and_text(get_updates()) +send_message(text, chat)