This repository was archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path#09-polls.py
More file actions
92 lines (67 loc) · 2.39 KB
/
Copy path#09-polls.py
File metadata and controls
92 lines (67 loc) · 2.39 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
from time import sleep
import json
global OFFSET
OFFSET = 0
botToken = ""
requestURL = "https://api.telegram.org/bot" + botToken + "/getUpdates"
sendURL = "https://api.telegram.org/bot" + botToken + "/sendMessage"
sendPollURL = "https://api.telegram.org/bot" + botToken + "/sendPoll"
def update (url):
global OFFSET
try:
update_raw = requests.get(url + "?offset=" + str(OFFSET))
update = update_raw.json()
result = extract_result(update)
if result != False:
OFFSET = result['update_id'] + 1
return result
else:
return False
except requests.exceptions.ConnectionError:
pass
def extract_result (dict):
result_array = dict['result']
if result_array == []:
return False
else:
result_dic = result_array[0]
return result_dic
def send_message (chatId, message):
data = {
'chat_id': chatId,
'text': message
}
requests.post(sendURL, data = data)
def send_poll (chatId, question: str, options: list, is_anonymous=True, poll_type="regular",
multiple_answers=False, correct_option=False, explanation="", open_period=0):
data = {
'chat_id': chatId,
'question': question,
'options': options,
'is_anonymous': is_anonymous,
'type': poll_type,
'allows_multiple_answers': multiple_answers,
'correct_option_id': correct_option,
'explanation': explanation
}
if open_period > 5:
data['open_period'] = open_period
requests.post(sendPollURL, data=data)
while True:
newmessage = update (requestURL)
try:
if newmessage != False:
userchatid = newmessage['message']['chat']['id']
usertext = newmessage['message']['text']
username = newmessage['message']['chat']['first_name']
if usertext.lower() == "hello":
send_message(userchatid, "Hi " + username)
elif usertext.lower() == "poll":
send_poll(userchatid, "This is a question", json.dumps(["Option 1", "Option 2"]))
else:
send_message(userchatid, "You said: " + usertext)
except Exception as e:
# print (newmessage)
print ("Error:", e)
sleep (1)