-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifications.py
53 lines (44 loc) · 1.54 KB
/
notifications.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
import requests
import pytz
import logging
import datetime
import time
from pymongo import MongoClient
dbcli = MongoClient('127.0.0.1', 8080)
db = dbcli['scrader']
db.authenticate('scrader', 'scr@d3r')
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('notification_logs.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
def send_user_news(user):
logger.info("sending staff for user" + user.get('first_name'))
if not user.get('companies'):
return
url = 'https://api.chatfuel.com/bots/591189a0e4b0772d3373542b/' \
'users/{}/' \
'send?chatfuel_token=vnbqX6cpvXUXFcOKr5RHJ7psSpHDRzO1hXBY8dkvn50ZkZyWML3YdtoCnKH7FSjC' \
'&chatfuel_block_id=5b01b49fe4b03903064c0f64&last%20'.format(user.get('user_id'))
try:
requests.post(url)
except requests.exceptions.RequestException:
pass
if __name__ == '__main__':
while True:
utc = pytz.utc
utc_time = datetime.datetime.now(utc)
time_now = str(utc_time.now().time())
hour = str(int(time_now.split(':')[0]))
#if int(hour) < 10:
#hour = '0' + hour
minutes = time_now.split(':')[1]
formatted_time = hour + ":" + minutes
logger.info(formatted_time)
users_collection = db['users']
query = {'datetime': formatted_time}
users = users_collection.find(query)
for user in users:
send_user_news(user)
time.sleep(60)