-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiodic.py
50 lines (42 loc) · 1.38 KB
/
periodic.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
import sched, time
from app import db
from kavenegar import *
from config import Config
from models import User, Audite
def send_notif(audite, users_phone):
msg = f''''
{audite.CompanyName} {audite.Symbol} \n
{audite.Title} \n
{audite.Url} \n\n
[کدالر]
'''
try:
params = {
'receptor': users_phone,
'message': msg
}
response = api.sms_send(params)
return True
except Exception as e:
print(str(e))
return False
def periodic_alert(scheduler):
scheduler.enter(60, 1, periodic_alert, (scheduler,))
with db.session() as session:
users = session.query(User).all()
audites = session.query(Audite).filter(Audite.send.is_(None)).offset(50).all()
for audite in audites:
users_phone = ''
for user in users:
if audite.Symbol in user.symbols:
users_phone += f'0{user.phone},'
if len(users_phone) > 0:
status = send_notif(audite, users_phone=users_phone)
if status:
audite.send = True
session.commit()
if __name__ == '__main__':
api = KavenegarAPI(Config.SMS_TOKEN)
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enter(60, 1, periodic_alert, (scheduler,))
scheduler.run()