-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendHalp.py
221 lines (205 loc) · 12.1 KB
/
SendHalp.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import requests
import json
import random
token = "769472550:AAF4X8wuqE6nU0DJqQPXNRPtMIz6YHeTA-M"
url = "https://api.telegram.org/bot" + token + "/"
MAX_QNS = 5
def generateList(qns):
text = "<b>Send Halp Questions</b>"
count = 1
for qn in qns:
text += "\n\n" + str(count) + ". " + qn['question'] + "\n"
text += "[" + str(len(qn['answer'])) + " Ans]"
count = count + 1
return text
def main():
rdm = random.randint(1, 10)
questions = {}
# questions is a dictionary with chat_id keys. Each key has an array (list) of
# 5 qn dictionary, that contains a question string, user_id and an array (list)
# of answer strings
while True:
result = requests.get(url + "getUpdates").json()
if 'result' not in result:
continue
result = result['result']
if len(result) == 0:
continue # no results :(
for message in result:
if 'message' in message: # Message
chat_id = message['message']['chat']['id']
if chat_id not in questions:
questions[chat_id] = []
chat_qns = questions[chat_id]
if 'text' in message['message']: # Text
reply = message['message']['text']
args = reply.split(" ")
if args[0] == "/list":
if len(chat_qns) == 0:
text = "No questions to show! Ask one using /ask!"
message_out = {"chat_id": chat_id, "text": text}
else:
text = generateList(chat_qns)
buttons = [{"text": "1", "callback_data": "ans 1"}]
for i in range(2, len(chat_qns) + 1):
buttons.append({"text": str(i), "callback_data": "ans " + str(i)})
options = {"inline_keyboard": [buttons]}
message_out = {"chat_id": chat_id, "text": text, "parse_mode": "HTML", "reply_markup": options}
requests.post(url + "sendMessage", json = message_out)
elif args[0] == "/remove":
if len(args) == 1:
text = "Please choose a question number!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
continue
try: # test for non-integer input
qn_num = int(args[1])
except ValueError:
qn_num = 0
user_id = message['message']['from']['id']
if qn_num <= 0 or len(chat_qns) < qn_num:
text = "No such question to be removed! Please try again with another index!"
elif user_id != chat_qns[qn_num - 1]['user_id']:
text = "Sorry, you are not allowed to remove this question!"
else:
del chat_qns[qn_num - 1]
text = "Question " + args[1] + " has been removed!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
elif args[0] == "/resolve":
if len(args) == 1:
text = "Please choose a question number!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
continue
try: # test for non-integer input
qn_num = int(args[1])
except ValueError:
qn_num = 0
user_id = message['message']['from']['id']
if qn_num <= 0 or len(chat_qns) < qn_num:
text = "No such question to be resolved! Please try again with another index!"
elif user_id != chat_qns[qn_num - 1]['user_id']:
text = "Sorry, you are not allowed to resolve this question!"
else:
qn = chat_qns[qn_num - 1]
text = "<b>Question by " + qn['question'] + "</b>"
for ans in qn['answer']:
text += "\n\n" + ans
del chat_qns[qn_num - 1]
message_out = {"chat_id": chat_id, "text": text, "parse_mode": "HTML"}
requests.post(url + "sendMessage", json = message_out)
elif args[0] == "/help":
ask = "Type /ask (question) here. Eg: /ask Where shall we go for lunch today?"
answer = "Type /answer (index) (answer) here. Eg: /answer 1 Hawker Centre"
list = "Type /list to display all the questions. Eg: /list"
remove = "Type /remove (index). Eg: /remove 2 - removes 2nd question in list"
resolve = "Type /resolve (index). Eg: /resolve 2 - removes 2nd question in list, prints out list afterwards"
sendhelp = "Type /sendhelp - cleans up the spam it creates afterwards"
message_out = {"chat_id": chat_id, "text": ask + "\n" + answer + "\n" + list + "\n" +remove
+ "\n" + resolve + "\n" + sendhelp}
requests.post(url + "sendMessage", json=message_out)
elif args[0] == "/sendhelp":
if len(args) == 1:
counts = 5
else:
try: # test for non-integer input
counts = int(args[1])
if counts > 10:
counts = 10
except ValueError:
counts = 5
helpMessage = "Please help x " + str(counts) + "! :("
start = 1
end = counts + 1
message_out = {"chat_id": chat_id, "text": helpMessage}
deletelater = []
for i in range(start, end):
posted = requests.post(url + "sendMessage", json=message_out).content
parsed = json.loads(posted)
deletelater.append(parsed['result']['message_id'])
print(deletelater)
for i in range(start - 1, end - 1):
requests.post(url + "deleteMessage?" + "chat_id=" + str(chat_id) + "&message_id=" + str(
deletelater[i]))
elif args[0] == "/answer":
if len(args) == 1:
text = "Please write an answer!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
continue
try: # test for non-integer input
qn_num = int(args[1])
except ValueError:
qn_num = 0
if qn_num <= 0 or qn_num > len(chat_qns): # out of range
text = "Invalid question number. Please try again!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
elif len(args) == 2:
text = "Please write an answer!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
else:
newAns = " ".join(args[2:])
qn = chat_qns[qn_num - 1]
user_name = message['message']['from']['first_name']
qn['answer'].append(user_name + ": " + newAns)
text = "<b>Question by " + qn['question'] + "</b>"
for ans in qn['answer']:
text += "\n\n" + ans
message_out = {"chat_id": chat_id, "text": text, "parse_mode": "HTML"}
requests.post(url + "sendMessage", json = message_out)
elif args[0] == "/ping":
if rdm == 0:
rdm = random.randint(1, 10)
message_out = {"chat_id": chat_id, "text": "BANG BANG BANG"}
else:
rdm = rdm - 1
message_out = {"chat_id": chat_id, "text": "pong"}
requests.post(url + "sendMessage", json = message_out)
elif args[0] == "/ask":
if len(chat_qns) >= MAX_QNS:
text = "The maximum questions we can store is " + str(MAX_QNS) + ". Please /remove or /resolve the existing questions first."
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
elif len(args) == 1:
text = "Please write a question!"
message_out = {"chat_id": chat_id, "text": text}
requests.post(url + "sendMessage", json = message_out)
else:
newQn = message['message']['from']['first_name'] + ": " + " ".join(args[1:])
user_id = message['message']['from']['id']
chat_qns.append({"question": newQn, "answer": [], "user_id": user_id})
text = generateList(chat_qns)
buttons = [{"text": "1", "callback_data": "ans 1"}]
for i in range(2, len(chat_qns) + 1):
buttons.append({"text": str(i), "callback_data": "ans " + str(i)})
options = {"inline_keyboard": [buttons]}
message_out = {"chat_id": chat_id, "text": text, "parse_mode": "HTML", "reply_markup": options}
requests.post(url + "sendMessage", json = message_out)
elif 'callback_query' in message:
# print("Callback by " + callback_id)
callback_id = message['callback_query']['id']
data = message['callback_query']['data']
args = data.split(" ")
if args[0] == "ans": # show answers to a selected question
origin_msg = message['callback_query']['message']
chat_id = origin_msg['chat']['id']
chat_qns = questions[chat_id]
list_index = int(args[1]) - 1
if len(chat_qns) > list_index: # valid index
qn = chat_qns[int(args[1]) - 1]
text = "<b>Question by " + qn['question'] + "</b>"
for ans in qn['answer']:
text += "\n\n" + ans
else:
text = "Invalid question! Please try again."
pop_out = {"callback_query_id": callback_id}
message_out = {"chat_id": chat_id, "text": text, "parse_mode": "HTML"}
requests.post(url + "answerCallbackQuery", json = pop_out)
requests.post(url + "sendMessage", json = message_out)
offset = str(message['update_id'] + 1)
result = requests.get(url + "getUpdates?offset=" + offset + "&timeout=1").json()
if __name__ == "__main__":
main()