-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.py
123 lines (100 loc) · 4.85 KB
/
interactive.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
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
from core import *
from hourly_checknalert import getPriceMsg
app = Flask(__name__)
from translation_and_language_sentiment import *
import requests
def detect_intent_texts(project_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversation."""
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print("Session path: {}\n".format(session))
for text in texts:
text_input = dialogflow.TextInput(text=text, language_code=language_code)
query_input = dialogflow.QueryInput(text=text_input)
response = session_client.detect_intent(
request={"session": session, "query_input": query_input}
)
print("=" * 20)
print("Query text: {}".format(response.query_result.query_text))
print(
"Detected intent: {} (confidence: {})\n".format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence,
)
)
print("Fulfillment text: {}\n".format(response.query_result.fulfillment_text))
@app.route("/sms", methods=['GET', 'POST'])
def sms_reply():
message_received, lang = translate_text("en", request.form['Body'])
phone_num_from = request.form["From"]
mra = message_received.split(' ');
if mra[0] == "Activate":
url = 'http://localhost:3000/activate'
post_data = {'phone_num': phone_num_from, 'code': str(mra[1])}
aresult = requests.post(url, data = post_data).json()["aresult"]
if (aresult == "true"):
print("successfully activated")
resp = MessagingResponse()
resp.message("You have successfully been activated!")
return str(resp)
return ""
url = "http://localhost:3000/get_user"
post_data = {'phone_num': phone_num_from}
user = requests.post(url, data = post_data).json()["user"]
if ("_fieldsProto" not in user) or ("activated" not in user["_fieldsProto"]) or (user["_fieldsProto"]["activated"]["stringValue"] != "y"):
print("User doesn't exist or not activated!")
return ""
message_to_send = ""
currency_name = mra[0];
news_response = getNews(currency_name, num=1)
news0 = news_response["articles"][0];
news0_message_to_convey = "1. " + news0["title"] + ".\n" + \
"per " + news0["source"]["name"] + ": " + news0["url"] + "\n"
if (currency_name == "Ethereum"):
news0_message_to_convey = "[High magnitude of sentiment--More likely to be important] " + news0_message_to_convey
reddit = praw.Reddit(client_id=os.getenv("REDDIT_CLIENT_ID"),
client_secret=os.getenv("REDDIT_CLIENT_SECRET"), password=os.getenv("REDDIT_PASSWORD"),
user_agent='PRAW', username=os.getenv("REDDIT_USER_NAME"))
reddit_message_to_convey = f"Hot discussion on {currency_name} since last hour:\n"
for post in reddit.subreddit(currency_name).top("day", limit=1):
reddit_message_to_convey += f"{post.title} {post.url} {post.ups} upvotes, {post.num_comments} comments.\n"
ai_advice_message_to_convey = "AI Advice: " + getAIAdvice(currency_name)["conclusion"] \
+ ".\n"
fname = user["_fieldsProto"]["fname"]["stringValue"]
if (len(mra) == 1):
lang = "en"
message_to_send += f"Hi {fname}!\n"
message_to_send += f"{currency_name}'s current price is {getCurrentPrice(currency_name)[0:7]} USD.\n\n"
message_to_send += f"News headlines on {currency_name}:\n{news0_message_to_convey}\n"
message_to_send += reddit_message_to_convey + "\n"
message_to_send += ai_advice_message_to_convey
message_to_send += "\n"
else:
for attr in mra:
if (attr == "price"):
message_to_send += getPriceMsg(currency_name)
message_to_send += "\n"
elif (attr == "news"):
message_to_send += f"News headlines on {currency_name}:\n{news0_message_to_convey}"
message_to_send += "\n"
elif (attr == "discussion"):
message_to_send += reddit_message_to_convey
message_to_send += "\n"
elif (attr == "advice"):
message_to_send += ai_advice_message_to_convey
message_to_send += "\n"
else:
pass
"""Respond to incoming calls with a simple text message."""
# Start our TwiML response
resp = MessagingResponse()
# Add a message
resp.message(translate_text(lang, message_to_send)[0])
return str(resp)
if __name__ == "__main__":
app.run(debug=True)