-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduck.py
58 lines (43 loc) · 1.58 KB
/
duck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
import time
import schedule
import urllib3
token = ""
offset = 0
def pull_msg():
global offset
http = urllib3.PoolManager()
resp = http.request(method="GET",
headers={
"Content-Type": "application/json",
},
url="https://api.telegram.org/bot{0}/getUpdates?offset={1}".format(token, offset))
body = json.loads(resp.data.decode("utf-8"))
# print(body)
# process body for loop and update offset
for i in range(0, len(body["result"])):
print(body["result"][i])
# parse sender id and process text then respond(send_msg)
sender_id = body["result"][i]["message"]["from"]["id"]
text = body["result"][i]["message"]["text"]
send_msg(sender_id, text)
if i == len(body["result"]) - 1:
offset = body["result"][i]["update_id"] + 1
def send_msg(sender_id, text):
http = urllib3.PoolManager()
resp = http.request(method="POST",
headers={
"Content-Type": "application/json",
},
url="https://api.telegram.org/bot{0}/sendMessage".format(token),
body=json.dumps(
{
"chat_id": sender_id,
"text": text
})
)
if __name__ == "__main__":
schedule.every(3).seconds.do(pull_msg)
while True:
schedule.run_pending()
time.sleep(1)