-
Notifications
You must be signed in to change notification settings - Fork 1
/
messenger.py
83 lines (69 loc) · 2.4 KB
/
messenger.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
'''
It is the program to deploy Jarvis to heroku. Currently not working.
'''
import json
import os
import requests
from flask import Flask, request ,Response
import configparser
from scripts import req
#host=config['HOST']
#ACCESS_TOKEN = os.environ.get('ACCESS_TOKEN', config.ACCESS_TOKEN)
VERIFY_TOKEN = os.environ.get('VERIFY_TOKEN', 'xxxyyyzzz')
app = Flask(__name__)
@app.route('/')
def about():
intro = get_file('static/index.html')
return Response(intro,mimetype='text/html')
@app.route('/webhook/', methods=['GET', 'POST'])
def webhook():
if request.method == 'POST':
try:
data=request.get_json()
return json.dumps(data)
except:
return "Fucked Up here"
# 200 OK
elif request.method == 'GET':
if request.args.get('hub.verify_token') == VERIFY_TOKEN:
return request.args.get('hub.challenge')
else:
return 'Error, wrong validation token'
'''
@app.route('/privacy-policy/',methods=['GET'])
def privacy_policy():
content = get_file('static/privacy-policy.html')
return Response(content,mimetype='text/html')
'''
def get_file(file):
try:
src=os.path.join(root_dir(),file)
return open(src).read()
except IOError as exec:
return str(exec)
def root_dir():
return os.path.abspath(os.path.dirname(__file__))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=True)
'''
data = request.get_json(force=True)
messaging_events = data['entry'][0]['messaging']
for event in messaging_events:
sender = event['sender']['id']
message = None
if 'message' in event and 'text' in event['message']:
if 'quick_reply' in event['message'] and 'payload' in event['message']['quick_reply']:
quick_reply_payload = event['message']['quick_reply']['payload']
message = modules.search(quick_reply_payload, sender=sender, postback=True)
else:
text = event['message']['text']
message = modules.search(text, sender=sender)
if 'postback' in event and 'payload' in event['postback']:
postback_payload = event['postback']['payload']
message = modules.search(postback_payload, sender=sender, postback=True)
return
if request.args.get('hub.verify_token') == VERIFY_TOKEN:
return request.args.get('hub.challenge')
else:
return 'Error, wrong validation token'
'''