-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_messages.py
executable file
·52 lines (41 loc) · 1.6 KB
/
send_messages.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
from datetime import datetime
import crud
from model import connect_to_db
import os
from twilio.rest import Client
from flask_crontab import Crontab
from server import app
import weather
crontab = Crontab(app)
def send_message(message):
"""Send messages using twilio api"""
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
message = client.messages.create(
body=f"{message}",
from_= os.environ['FROM_PHONE_NUMBER'],
to= os.environ['TO_PHONE_NUMBER']
)
print(message.sid)
@crontab.job()
def message_sender():
"""Determine if there are any messages that need sending and what an appropriate message would be to send"""
connect_to_db(app)
now = datetime.now()
now = datetime.strftime(now, "%I:%M %p")
now = datetime.strptime(now, "%I:%M %p")
events = crud.get_events_by_time(now)
for event in events:
if event.reminder:
if event.event_type == "walk":
user = event.user
walking = weather.get_walking_weather(user.zipcode, user)
if not walking["walking"]:
activity = crud.get_random_activity(user)
message = "It's not walking weather. Try: " + activity.name
send_message(message)
else:
send_message(event.description)
else:
send_message(event.description)