4
4
from werkzeug .utils import secure_filename
5
5
import os
6
6
import json
7
- import time
8
7
9
8
APP_ROOT = os .path .dirname (os .path .abspath (__file__ ))
10
9
UPLOAD_FOLDER = APP_ROOT + "/src/chats"
@@ -66,8 +65,7 @@ def verify():
66
65
return "Verification token mismatch" , 403
67
66
return request .args ["hub.challenge" ], 200
68
67
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
71
69
72
70
73
71
@app .route ('/' , methods = ['POST' ])
@@ -81,88 +79,52 @@ def webhook():
81
79
if data ["object" ] == "page" :
82
80
83
81
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:
87
82
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
+
108
111
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." )
115
113
else :
116
114
noclassifier = "You have not uploaded your chat history yet. Please rename the .html file to .txt and attach it to this chat."
117
115
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
166
128
167
129
print ("sending response: ok, 200" )
168
130
return "ok" , 200
0 commit comments