-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (49 loc) · 1.81 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
import random
from flask import Flask, request
from twilio.rest import Client
from bot import ask
from db import insert_message, insert_response, get_messages, get_responses
import os
app = Flask(__name__)
# Set the secret key to a random string to keep session data secure
app.config['SECRET_KEY'] = 'any-random-string'
# Set the Twilio account SID and auth token
account_sid = os.getenv('account_sid')
auth_token = os.getenv('auth_token')
client = Client(account_sid, auth_token)
# Set the Twilio phone number
twilio_phone_number = os.getenv('twilio_phone_number')
# Set the phone number to send SMS to
my_phone_number = os.getenv('my_phone_number')
@app.route('/', methods=['POST'])
def bot():
# Get the incoming message from the request values
incoming_msg = request.form['Body']
# Generate a random message ID
message_id = random.randint(1, 100000)
# Insert the new message into the "messages" table
insert_message(incoming_msg, message_id)
# Get the chatbot's response
answer = ask(incoming_msg, message_id)
# Insert the new chatbot response into the "chatbot_responses" table
insert_response(answer, message_id)
# Retrieve all messages and chatbot responses from the database
messages = get_messages()
chat_log = ""
for message in messages:
message_id = message[0]
body = message[1]
chat_log += f"\nPerson: {body}"
responses = get_responses(message_id)
for response in responses:
body = response[1]
chat_log += f"\nBot: {body}"
# Use Twilio to send the chatbot's response as a text message
message = client.messages.create(
body=answer,
from_=twilio_phone_number,
to=my_phone_number
)
return str(message.sid)
if __name__ == '__main__':
app.run(debug=True, port=8080)