-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
173 lines (149 loc) · 5.47 KB
/
run.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
from flask import Flask, request, jsonify
from flask_mqtt import Mqtt
from twilio.twiml.messaging_response import MessagingResponse
import json
import twilio_messaging as messaging
import chatbot
import db
import connect
import visual_recognition as vr
import nlp
import os
import sparrow_handler as sparrow
from time import sleep
from threading import Thread
import paho.mqtt.publish as publish
with open('config.json') as config_file:
config = json.load(config_file)
app = Flask(__name__)
app.config['MQTT_BROKER_URL'] = config["mqtt"]["broker"]
app.config['MQTT_BROKER_PORT'] = config["mqtt"]["port"]
app.config['MQTT_REFRESH_TIME'] = 1.0 # refresh time in seconds
# app.config['MQTT_USERNAME'] = 'user'
# app.config['MQTT_PASSWORD'] = 'secret'
mqtt = Mqtt(app)
mqttPublishBroker=config["mqtt"]["broker"]
mqttPublishPort=config["mqtt"]["port"]
flag_connected = 0
cf_port = os.getenv("PORT")
@app.route('/')
def route():
return "chirrup chirrup....You have reached Sparrow API"
@app.route("/middleware/receive", methods=['GET', 'POST'])
def listen_input():
message = request.values.get('Body', None)
from_no = request.values.get('From', None)
print(message, from_no)
#Handling Media content
num_media = int(request.values.get("NumMedia"))
if num_media > 0:
media_url = request.values.get(f'MediaUrl0')
mime_type = request.values.get(f'MediaContentType0')
print(media_url, mime_type)
if num_media > 1:
messaging.send_message(from_no, "Multiple media cannot be sent. Sending only first media")
#Handling @sparrow commands
if sparrow.is_sparrow_request(message):
t = Thread(target=sparrow.handle_sparrow_request, args=(from_no, message,))
t.start()
# sparrow.handle_sparrow_request(from_no, message)
return str(MessagingResponse())
receiver = db.getReceiver(from_no)
if receiver == db.IBM_RECEIVER:
if sparrow.is_command(message):
t = Thread(target=sparrow.handle_command, args=(from_no, message))
t.start()
return str(MessagingResponse())
elif num_media > 0:
reply = "Sorry! Our Automated chatbot doesn't support Media at this point."
elif message == "":
reply = "Invalid format. Your message is empty!"
else:
replies = chatbot.handle_message(from_no, message)
if len(replies) > 1:
t = Thread(target=messaging.send_messages, args=(from_no, replies))
t.start()
return(str(MessagingResponse()))
else:
reply = replies[0]
resp = MessagingResponse()
resp.message(reply)
return str(resp)
else:
if num_media > 0:
messaging.send_message_with_media(from_no, receiver, message, media_url, mime_type)
elif message == "":
messaging.send_message(from_no, "Invalid message. Can't be sent")
else:
messaging.send_message(receiver, message)
return str(MessagingResponse())
@app.route("/middleware/connect_expert", methods=['GET', 'POST'])
def connectExpert():
sessionID = request.values.get('sessionID', None)
type = request.values.get('type', None)
userID = db.getUserID(sessionID)
if userID:
expert = connect.connect_expert(userID, type)
return str(expert)
return str("Invalid Session")
@app.route("/middleware/send_message", methods=['GET', 'POST'])
def sendMessage():
userID = request.values.get('userID', None)
message = request.values.get('message', None)
expert = messaging.send_message(userID, message)
return str("Success")
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
mqtt.unsubscribe_all()
mqtt.subscribe('sparrow_receive/+')
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
data = dict(
topic=message.topic,
payload=message.payload.decode()
)
# print(data)
message = data["payload"]
from_no = data["topic"].split("/")[1]
print(message, from_no)
if sparrow.is_sparrow_request(message):
sparrow.handle_sparrow_request(from_no, message)
return
receiver = db.getReceiver(from_no)
print(receiver)
if receiver == db.IBM_RECEIVER:
if sparrow.is_command(message):
sparrow.handle_command(from_no, message)
return
reply = chatbot.handle_message(from_no, message)
for message in reply:
mqtt.publish(data["topic"].replace("receive", "response"), message)
sleep(2)
return
else:
messaging.send_message(receiver, message)
return
@app.route("/visual_recognition/text", methods=['POST'])
def recognize_text():
image_url = request.values.get('image_url', None)
# print(image_url)
text = vr.get_text_from_image(image_url)
#return jsonify({'text':text})
return text
@app.route("/nlp/sentiment", methods=['POST'])
def sentiment_of_text():
text = request.values.get('text', None)
sentiment = nlp.get_sentiment_emotions(text)
#return jsonify({'text':text})
return jsonify(sentiment)
@app.route("/nlp/entities", methods=['POST'])
def entities_of_text():
text = request.values.get('text', None)
entities = nlp.get_entities(text)
#return jsonify({'text':text})
return jsonify(entities)
if __name__ == "__main__":
if cf_port is None:
app.run(host='0.0.0.0', port=5000, debug=False)
else:
app.run(host='0.0.0.0', port=int(cf_port), debug=False)