-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminders.py
67 lines (49 loc) · 1.9 KB
/
reminders.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
import schedule
import time
from twilio.rest import Client
import os
import datetime
from server import app
from model import connect_to_db, db, User, Project, Status, Image
def send_text(text, recipient = os.environ['MY_PHONE']): # pragma: no cover
# Your Account SID from twilio.com/console
account_sid = os.environ['TWILIO_SID']
# Your Auth Token frpm twilio.com/console
auth_token = os.environ['TWILIO_TOKEN']
client = Client(account_sid, auth_token)
message = client.messages.create(
to=recipient,
from_=os.environ['TWILIO_NUM'],
body=text)
print(message.sid)
def message():
""" Create mesage from database info """
now = datetime.datetime.now()
users = db.session.query(User.user_id, User.update_time, User.phone_num).filter(User.subscribed).all()
for user in users:
user_id, freqency, phone = user
update_count = db.session.query(Project).filter(Project.status_id == 1,
Project.user_id == user_id,
Project.updated_at < (
now - datetime.timedelta(days=freqency))).count()
body = format_message(update_count, freqency)
print body
# return body
# send_text(body, phone)
def format_message(count, freqency):
""" write the text for each user who is subsribed
>>> mess = format_message(7, 14)
>>> mess == "7 projects haven't been updated in 14 days!"
True
"""
text = "%s projects haven't been updated in %s days!" % (count, freqency)
return text
# schedule.every().day.at("10:30").do(send_message)
schedule.every(1).minutes.do(message)
if __name__ == "__main__":
# # while True:
connect_to_db(app)
schedule.run_all()
while True:
schedule.run_pending()
time.sleep(1)