-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
189 lines (150 loc) · 6.67 KB
/
app.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
import requests
from flask import Flask, request, redirect, url_for
from src.classifier import MyTextClassifier
from werkzeug.utils import secure_filename
import os
import json
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = APP_ROOT + "/src/chats"
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#clf = None
clfdict = {}
@app.route('/hi/<name>')
def say_hi(name):
return 'Hello, %s!' % name
@app.route('/findauthor/<text>/')
def findauthor(text):
print([text])
return clf.predictAuthor([text])[0]
# When using Postman:
# POST to https://fb-text-classifier.herokuapp.com/upload/
# Body: form-data
# Key - value:
# size (Text) - original
# file (File) - "Choose Files"
@app.route('/upload/', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file
if 'file' not in request.files:
return "No file selected"
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
return "No file selected"
if file:
filename = secure_filename(file.filename)
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(path)
global clf
clf = MyTextClassifier(path)
return "File upload success"
return "Done"
# https://github.com/hartleybrody/fb-messenger-bot
@app.route('/', methods=['GET'])
def verify():
# when the endpoint is registered as a webhook, it must echo back
# the 'hub.challenge' value it receives in the query arguments
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "<h1>Hello, this is fb-text-classifier.</h1>\n<p>Made by Siim Raudsepp and Kristjan Puusepp</p>", 200
@app.route('/', methods=['POST'])
def webhook():
global clfdict
# endpoint for processing incoming messaging events
data = request.get_json()
print(data)
if data["object"] == "page":
for entry in data["entry"]:
for messaging_event in entry["messaging"]:
if messaging_event.get("read"):
pass
else:
try:
if messaging_event.get("message"): # someone sent us a message
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
print("sender_id: " + sender_id)
try:
for i in messaging_event["message"]["attachments"]:
print("Checking if message contains a file...")
if i["type"] == "file":
print("User sent a file. Downloading it...")
r = requests.get(i["payload"]["url"])
send_message(sender_id, "Clearing old classifier...")
clfdict[sender_id] = None
dumpclean(clfdict)
send_message(sender_id, "Learning from the file...")
clfdict[sender_id] = MyTextClassifier(r.content)
print("saved to dictionary at [" + sender_id + "]")
send_message(sender_id, "Learning finished.")
break
else:
print("Not a file")
break
except Exception:
print("Something went wrong, could not download the file")
recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID
message_text = messaging_event["message"]["text"] # the message's text
clf = None
try:
clf = clfdict[sender_id]
print("Got classifier with id " + sender_id)
except Exception:
print("cannot get your classifier " + sender_id)
if clf:
send_message(sender_id, clf.predictAuthor([message_text])[0] + " is the author of that text.")
else:
noclassifier = "You have not uploaded your chat history yet. Please rename the .html file to .txt and attach it to this chat."
send_message(sender_id, noclassifier)
if messaging_event.get("delivery"): # delivery confirmation
pass
if messaging_event.get("optin"): # optin confirmation
pass
if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message
pass
except KeyError:
return "oops", 200
print("sending response: ok, 200")
return "ok", 200
def dumpclean(obj):
if type(obj) == dict:
for k, v in obj.items():
if hasattr(v, '__iter__'):
print(k)
dumpclean(v)
else:
print('%s : %s' % (k, v))
elif type(obj) == list:
for v in obj:
if hasattr(v, '__iter__'):
dumpclean(v)
else:
print(v)
else:
print(obj)
def send_message(recipient_id, message_text):
print("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"]
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
}
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
print(r.status_code)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(port=port, debug=True, threaded=True)