-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
133 lines (115 loc) · 5.05 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
# -*- coding: utf-8 -*-
import sys
import json
import pip._vendor.requests as requests
from flask import Flask, request, session
from dbhelper import DBHelper
from url_constants import UrlConstants
from url_constants import EnvConstants
from log_status import StatusType
from watson_analyser import WatsonAnalyzer
from mood import Mood
__author__ = 'Oyewale Ademola'
app = Flask(__name__)
@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") == EnvConstants.fb_page_verify_token:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello Bot!", 200
@app.route('/', methods=['POST'])
def webhook():
# endpoint for processing incoming messaging events
db = DBHelper()
mood = Mood()
data = request.get_json()
logger(data, status=StatusType.INFO)
mood_items = db.get_items()
current_mood = ''
if mood in mood_items:
current_mood = mood_items[0]
else:
db.add_item(mood.TONE_NEUTRAL)
current_mood = mood.TONE_NEUTRAL
default_reply = mood.CONST_DEFAULT_REPLY
if data["object"] == "page":
for entry in data["entry"]:
for messaging_event in entry["messaging"]:
if messaging_event.get("message"): # someone sent us a message
# the facebook ID of the person sending the bot the message
sender_id = messaging_event["sender"]["id"]
# the message sent by the user
if messaging_event["message"]["text"]:
message_text = messaging_event["message"]["text"]
# if can be an image or attachment ?
message_text = message_text.lower()
if message_text == mood.CONST_MOOD:
mood_items = db.get_items()
if mood in mood_items:
current_mood = mood
db.delete_item(mood)
else:
pass
send_message(sender_id, current_mood)
elif message_text in mood.default_messages:
send_message(sender_id, default_reply)
else:
send_response(mood=mood, message_text=message_text, sender_id=sender_id)
else:
logger("Can't process the message as it is not a text, it can either "
"be an attachment or something :) ", StatusType.INFO)
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
return "OK", 200
def send_response(mood, message_text, sender_id):
# analyse the text from fb with Watson to determine the mood
watson_analyze = WatsonAnalyzer()
results = watson_analyze.analyze_tone(message_text)
if results:
mood_tone = watson_analyze.get_emotion_tone(results) # show what it looks like
session["current_mood"] = mood_tone # update the current mood in session
# based on the mood, send the appropriate message
mood_level = mood.convert_tone_to_mood(mood_tone)
message_to_send = mood.get_reply_from_mood(mood_level)
send_message(sender_id, message_to_send)
else:
logger("Whoops, something went wrong while analyzing tone of the message", StatusType.ERROR)
def send_message(recipient_id, message_text):
logger("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text), StatusType.INFO)
params = {
"access_token": EnvConstants.fb_page_access_token
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
}
})
r = requests.post(UrlConstants.URL_FB_MESSAGES, params=params, headers=headers, data=data)
if r.status_code != 200:
logger(r.status_code, StatusType.ERROR)
logger(r.text, StatusType.ERROR)
def logger(message, status):
output = ""
if status == StatusType.ERROR:
output = "ERROR:" + str(message) + ": Status-> {type}".format(type=status)
elif status == StatusType.DEBUG:
output = "DEBUG:" + str(message) + ": Status-> {type}".format(type=status)
elif status == StatusType.INFO:
output = "INFO:" + str(message) + ": Status-> {type}".format(type=status)
print(output)
sys.stdout.flush()
if __name__ == '__main__':
app.run(debug=True)