-
Notifications
You must be signed in to change notification settings - Fork 27
/
server.py
37 lines (27 loc) · 896 Bytes
/
server.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
from flask import Flask, request
import requests
app = Flask(__name__)
ACCESS_TOKEN = ""
VERIFY_TOKEN = ""
def reply(user_id, msg):
data = {
"recipient": {"id": user_id},
"message": {"text": msg}
}
resp = requests.post("https://graph.facebook.com/v2.6/me/messages?access_token=" + ACCESS_TOKEN, json=data)
print(resp.content)
@app.route('/', methods=['GET'])
def handle_verification():
if request.args['hub.verify_token'] == VERIFY_TOKEN:
return request.args['hub.challenge']
else:
return "Invalid verification token"
@app.route('/', methods=['POST'])
def handle_incoming_messages():
data = request.json
sender = data['entry'][0]['messaging'][0]['sender']['id']
message = data['entry'][0]['messaging'][0]['message']['text']
reply(sender, message)
return "ok"
if __name__ == '__main__':
app.run(debug=True)