forked from rahulmistry33/ChatApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
89 lines (73 loc) · 2.58 KB
/
index.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
from flask import Flask, request, jsonify, render_template
import os
import dialogflow
import requests
import json
import pusher
app = Flask(__name__)
# initialize Pusher
'''pusher_client = pusher.Pusher(
app_id=os.getenv('PUSHER_APP_ID'),
key=os.getenv('PUSHER_KEY'),
secret=os.getenv('PUSHER_SECRET'),
cluster=os.getenv('PUSHER_CLUSTER'),
ssl=True)'''
@app.route('/')
def index():
return render_template('index.html')
@app.route('/get_movie_detail', methods=['POST'])
def get_movie_detail():
data = request.get_json(silent=True)
print("inside get movie details")
try:
ticker = data['queryResult']['parameters']['tickers']
'''api_key = os.getenv('OMDB_API_KEY')
movie_detail = requests.post('http://www.omdbapi.com/?t={0}&apikey={1}'.format(movie, api_key)).content
movie_detail = json.loads(movie_detail)'''
'''response = """
Title : {0}
Released: {1}
Actors: {2}
Plot: {3}
""".format(movie_detail['Title'], movie_detail['Released'], movie_detail['Actors'], movie_detail['Plot'])'''
response = """
"""
except:
response = "Could not get movie detail at the moment, please try again"
reply = { "fulfillmentText": response }
return jsonify(reply)
def detect_intent_texts(project_id, session_id, text, language_code):
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
if text:
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
print("intent matched")
return response.query_result.fulfillment_text
@app.route('/send_message', methods=['POST'])
def send_message():
try:
socketId = request.form['socketId']
except KeyError:
socketId = ''
message = request.form['message']
project_id = os.getenv('DIALOGFLOW_PROJECT_ID')
fulfillment_text = detect_intent_texts(project_id, "unique", message, 'en')
response_text = { "message": fulfillment_text }
'''pusher_client.trigger(
'movie_bot',
'new_message',
{
'human_message': message,
'bot_message': fulfillment_text,
},
socketId
)'''
print(response_text)
return jsonify(response_text)
# run Flask app
if __name__ == "__main__":
app.run()