-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtelegrambot.py
280 lines (232 loc) · 8.65 KB
/
telegrambot.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from flask import Flask, request, Response
import requests
import json
# token that we get from the BotFather
TOKEN = "<Your Bot Token>"
app = Flask(__name__)
# Reading the JSON message when the user send any type of file to the bot and extracting the chat id of the user and the file id for the file that user send to the bot
def tel_parse_get_message(message):
print("message-->", message)
try: # if the file is an image
g_chat_id = message['message']['chat']['id']
g_file_id = message['message']['photo'][0]['file_id']
print("g_chat_id-->", g_chat_id)
print("g_image_id-->", g_file_id)
return g_file_id
except:
try: # if the file is a video
g_chat_id = message['message']['chat']['id']
g_file_id = message['message']['video']['file_id']
print("g_chat_id-->", g_chat_id)
print("g_video_id-->", g_file_id)
return g_file_id
except:
try: # if the file is an audio
g_chat_id = message['message']['chat']['id']
g_file_id = message['message']['audio']['file_id']
print("g_chat_id-->", g_chat_id)
print("g_audio_id-->", g_file_id)
return g_file_id
except:
try: # if the file is a document
g_chat_id = message['message']['chat']['id']
g_file_id = message['message']['document']['file_id']
print("g_chat_id-->", g_chat_id)
print("g_file_id-->", g_file_id)
return g_file_id
except:
print("NO file found found-->>")
# Reading the JSON format when we send the text message and extracting the chat id of the user and the text that user send to the bot
def tel_parse_message(message):
print("message-->", message)
try:
chat_id = message['message']['chat']['id']
txt = message['message']['text']
print("chat_id-->", chat_id)
print("txt-->", txt)
return chat_id, txt
except:
print("NO text found-->>")
try:
cha_id = message['callback_query']['from']['id']
i_txt = message['callback_query']['data']
print("cha_id-->", cha_id)
print("i_txt-->", i_txt)
return cha_id, i_txt
except:
pass
# Get the Text message response from the bot
def tel_send_message(chat_id, text):
url = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
payload = {
'chat_id': chat_id,
'text': text
}
r = requests.post(url, json=payload)
return r
# Get the Image response from the bot by providing the image link
def tel_send_image(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendPhoto'
payload = {
'chat_id': chat_id,
'photo': "https://raw.githubusercontent.com/fbsamples/original-coast-clothing/main/public/styles/male-work.jpg"
}
r = requests.post(url, json=payload)
return r
# Get the Poll response from the bot
def tel_send_poll(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendPoll'
payload = {
'chat_id': chat_id,
"question": "In which direction does the sun rise?",
# options are provided in json format
"options": json.dumps(["North", "South", "East", "West"]),
"is_anonymous": False,
"type": "quiz",
# Here we are providing the index for the correct option(i.e. indexing starts from 0)
"correct_option_id": 2
}
r = requests.post(url, json=payload)
return r
# Get the Button response in the keyboard section
def tel_send_button(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
payload = {
'chat_id': chat_id,
'text': "What is this?", # button should be in the propper format as described
'reply_markup': {
'keyboard': [[
{
'text': 'supa'
},
{
'text': 'mario'
}
]]
}
}
r = requests.post(url, json=payload)
return r
# Get the Inline button response
def tel_send_inlinebutton(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
payload = {
'chat_id': chat_id,
'text': "What is this?",
'reply_markup': {
"inline_keyboard": [[
{
"text": "A",
"callback_data": "ic_A"
},
{
"text": "B",
"callback_data": "ic_B"
}]
]
}
}
r = requests.post(url, json=payload)
return r
# Get the Button response from the bot with the redirected URL
def tel_send_inlineurl(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
payload = {
'chat_id': chat_id,
'text': "Which link would you like to visit?",
'reply_markup': {
"inline_keyboard": [
[
{"text": "google", "url": "http://www.google.com/"},
{"text": "youtube", "url": "http://www.youtube.com/"}
]
]
}
}
r = requests.post(url, json=payload)
return r
# Get the Audio response from the bot by providing the URL for the audio
def tel_send_audio(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendAudio'
payload = {
'chat_id': chat_id,
"audio": "http://www.largesound.com/ashborytour/sound/brobob.mp3",
}
r = requests.post(url, json=payload)
return r
# Get the Document response from the bot by providing the URL for the Document
def tel_send_document(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendDocument'
payload = {
'chat_id': chat_id,
"document": "http://www.africau.edu/images/default/sample.pdf",
}
r = requests.post(url, json=payload)
return r
# Get the Video response from the bot by providing the URL for the Video
def tel_send_video(chat_id):
url = f'https://api.telegram.org/bot{TOKEN}/sendVideo'
payload = {
'chat_id': chat_id,
"video": "https://www.appsloveworld.com/wp-content/uploads/2018/10/640.mp4",
}
r = requests.post(url, json=payload)
return r
# Get the url for the file through the file id
def tel_upload_file(file_id):
# Getting the url for the file
url = f'https://api.telegram.org/bot{TOKEN}/getFile?file_id={file_id}'
a = requests.post(url)
json_resp = json.loads(a.content)
print("json_resp-->", json_resp)
file_pathh = json_resp['result']['file_path']
print("file_pathh-->", file_pathh)
# saving the file to our computer
url_1 = f'https://api.telegram.org/file/bot{TOKEN}/{file_pathh}'
b = requests.get(url_1)
file_content = b.content
with open(file_pathh, "wb") as f:
f.write(file_content)
# Reading the respomnse from the user and responding to it accordingly
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
msg = request.get_json()
try:
chat_id, txt = tel_parse_message(msg)
if txt == "hi":
tel_send_message(chat_id, "Hello, world!")
elif txt == "image":
tel_send_image(chat_id)
elif txt == "poll":
tel_send_poll(chat_id)
elif txt == "button":
tel_send_button(chat_id)
elif txt == "audio":
tel_send_audio(chat_id)
elif txt == "file":
tel_send_document(chat_id)
elif txt == "video":
tel_send_video(chat_id)
elif txt == "inline":
tel_send_inlinebutton(chat_id)
elif txt == "inlineurl":
tel_send_inlineurl(chat_id)
elif txt == "ic_A":
tel_send_message(chat_id, "You have clicked A")
elif txt == "ic_B":
tel_send_message(chat_id, "You have clicked B")
else:
tel_send_message(chat_id, 'from webhook')
except:
print("fromindex-->")
try:
file_id = tel_parse_get_message(msg)
tel_upload_file(file_id)
except:
print("No file from index-->")
return Response('ok', status=200)
else:
return "<h1>Welcome!</h1>"
if __name__ == '__main__':
app.run(threaded=True)