-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (41 loc) · 1.2 KB
/
app.py
File metadata and controls
54 lines (41 loc) · 1.2 KB
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
from flask import Flask
from flask import request
from twilio.rest import Client
import os
from marketstack import get_stock_price
app = Flask(__name__)
ACCOUNT_ID = os.environ.get('TWILIO_ACCOUNT')
TWILIO_TOKEN = os.environ.get('TWILIO_TOKEN')
client = Client(ACCOUNT_ID,TWILIO_TOKEN)
TWILIO_NUMBER = 'whatsapp:+14155238886'
def send_msg(msg,recipient):
client.messages.create(
from_= TWILIO_NUMBER,
body=msg,
to=recipient
)
def process_msg(msg):
response = ""
if (msg == "hi"):
response = "Hello, Welcome to the stock market bot"
response += "Type sym:<stock_symbol to know the price of the stock>"
elif 'sym' in msg:
data = msg.split(":")
stock_symbol = data[1]
stock_price = get_stock_price(stock_symbol)
last_price = stock_price['last_price']
last_price_str = str(last_price)
response = "The stock price of " + stock_symbol + " is $" + last_price_str
else:
response = "Please type Hi to get started."
return response
@app.route("/webhook", methods=["POST"])
def webhook():
f = request.form
msg = f['Body']
sender = f['From']
response = process_msg(msg)
send_msg(response,sender)
return "OK",200
if __name__ == "__main__":
app.run(host="localhost", port=5000, debug=True)