Skip to content

Commit 328cad9

Browse files
committed
Reverted
1 parent b2d61ed commit 328cad9

File tree

2 files changed

+42
-81
lines changed

2 files changed

+42
-81
lines changed

app.py

+42-80
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from werkzeug.utils import secure_filename
55
import os
66
import json
7-
import time
87

98
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
109
UPLOAD_FOLDER = APP_ROOT + "/src/chats"
@@ -66,8 +65,7 @@ def verify():
6665
return "Verification token mismatch", 403
6766
return request.args["hub.challenge"], 200
6867

69-
return "<h1>Hello, this is fb-text-classifier.</h1>\n" \
70-
" <p>Made by Siim Raudsepp and Kristjan Puusepp</p>", 200
68+
return "<h1>Hello, this is fb-text-classifier.</h1>\n<p>Made by Siim Raudsepp and Kristjan Puusepp</p>", 200
7169

7270

7371
@app.route('/', methods=['POST'])
@@ -81,88 +79,52 @@ def webhook():
8179
if data["object"] == "page":
8280

8381
for entry in data["entry"]:
84-
print(int(time.time()))
85-
print(int(((str(entry["time"]))[:-3])))
86-
# if int(time.time()) <= int(((str(entry["time"]))[:-3])) + 10:
8782
for messaging_event in entry["messaging"]:
88-
# if messaging_event.get("read"):
89-
# print("passing")
90-
# pass
91-
# if messaging_event.get("delivery"):
92-
# print("passing")
93-
# pass
94-
if messaging_event.get("message"):
95-
sender_id = messaging_event["sender"]["id"]
96-
97-
for type_of_message in messaging_event["message"]:
98-
99-
if type_of_message == "attachments":
100-
for i in messaging_event["message"]["attachments"]:
101-
print(i["payload"]["url"])
102-
r = requests.get(i["payload"]["url"])
103-
send_message(sender_id, "Learning from the file...")
104-
clf = MyTextClassifier(r.content)
105-
send_message(sender_id, "Learning finished.")
106-
continue
107-
if type_of_message == "text":
83+
if messaging_event.get("read"):
84+
pass
85+
else:
86+
try:
87+
if messaging_event.get("message"): # someone sent us a message
88+
89+
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
90+
print("sender_id: " + sender_id)
91+
92+
try:
93+
for i in messaging_event["message"]["attachments"]:
94+
print("Checking if message contains a file...")
95+
if i["type"] == "file":
96+
print("User sent a file. Downloading it...")
97+
r = requests.get(i["payload"]["url"])
98+
send_message(sender_id, "Learning from the file...")
99+
clf = MyTextClassifier(r.content)
100+
send_message(sender_id, "Learning finished.")
101+
continue
102+
else:
103+
print("Not a file")
104+
break
105+
except Exception:
106+
print("Something went wrong, could not download the file")
107+
108+
recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID
109+
message_text = messaging_event["message"]["text"] # the message's text
110+
108111
if clf:
109-
if messaging_event["message"]["text"].split()[0] == "!ennusta":
110-
send_message(sender_id, clf.predictAuthor([messaging_event["message"]["text"]][9:])[0] + " is the author of that text.")
111-
else:
112-
send_message(sender_id,
113-
"If you wish I made a prediction, write !ennusta 'your text here'")
114-
#if not clf:
112+
send_message(sender_id, clf.predictAuthor([message_text])[0] + " is the author of that text.")
115113
else:
116114
noclassifier = "You have not uploaded your chat history yet. Please rename the .html file to .txt and attach it to this chat."
117115
send_message(sender_id, noclassifier)
118-
#else:
119-
# send_message(sender_id, "If you wish I made a prediction, write !ennusta 'your text here'")
120-
121-
# else:
122-
# pass
123-
# else:
124-
# try:
125-
# if messaging_event.get("message"): # someone sent us a message
126-
#
127-
# sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
128-
# print("sender_id: " + sender_id)
129-
#
130-
# try:
131-
# for i in messaging_event["message"]["attachments"]:
132-
# print("Checking if message contains a file...")
133-
# if i["type"] == "file":
134-
# print("User sent a file. Downloading it...")
135-
# r = requests.get(i["payload"]["url"])
136-
# send_message(sender_id, "Learning from the file...")
137-
# clf = MyTextClassifier(r.content)
138-
# send_message(sender_id, "Learning finished.")
139-
# continue
140-
# else:
141-
# print("Not a file")
142-
# break
143-
# except Exception:
144-
# print("Something went wrong, could not download the file")
145-
#
146-
# recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID
147-
# message_text = messaging_event["message"]["text"] # the message's text
148-
#
149-
# if clf:
150-
# send_message(sender_id, clf.predictAuthor([message_text])[0] + " is the author of that text.")
151-
# else:
152-
# noclassifier = "You have not uploaded your chat history yet. Please rename the .html file to .txt and attach it to this chat."
153-
# send_message(sender_id, noclassifier)
154-
155-
# if messaging_event.get("delivery"): # delivery confirmation
156-
# pass
157-
#
158-
# if messaging_event.get("optin"): # optin confirmation
159-
# pass
160-
#
161-
# if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message
162-
# pass
163-
#
164-
# except KeyError:
165-
# return "oops", 200
116+
117+
if messaging_event.get("delivery"): # delivery confirmation
118+
pass
119+
120+
if messaging_event.get("optin"): # optin confirmation
121+
pass
122+
123+
if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message
124+
pass
125+
126+
except KeyError:
127+
return "oops", 200
166128

167129
print("sending response: ok, 200")
168130
return "ok", 200

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ MarkupSafe==1.0
3939
mistune==0.7.4
4040
nbconvert==5.3.1
4141
nbformat==4.4.0
42-
nltk==3.2.5
4342
notebook==5.2.0
4443
numpy==1.13.3
4544
oauthlib==2.0.6

0 commit comments

Comments
 (0)