-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
46 lines (35 loc) · 1.05 KB
/
main.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
from flask import Flask
from flask import request
from flask import Response
import requests
TOKEN = "6313165116:AAHEGDqbg25BvDD1vouT34jTYbmH2Dc6Vts"
app = Flask(__name__)
def parse_message(message):
print("message-->", message)
chat_id = message['message']['chat']['id']
txt = message['message']['text']
print("chat_id-->", chat_id)
print("txt-->", txt)
return chat_id, txt
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, payload)
return r
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
msg = request.get_json()
chat_id, txt = parse_message(msg)
if txt == "hi":
tel_send_message(chat_id, "Hello!!")
else:
tel_send_message(chat_id, "from webhook")
return Response('ok', status=200)
else:
return "<h1>Welcome1!</h1>"
if __name__== '__main__':
app.run(threaded = True)