cronjob/scripts/dinner_notifier.py
# -*- coding: utf-8 -*-
import requests
import os
import time
import pycurl
import logging
from urllib.parse import urlencode
from pymongo import MongoClient
from .utils import get_telegram_users
# If you want to use your own bot to development add the bot token as
# second parameters
TELEGRAM_ACCESS_TOKEN = os.getenv('TELEGRAM_ACCESS_TOKEN', '')
FACEBOOK_ACCESS_TOKEN = os.getenv('FACEBOOK_ACCESS_TOKEN', '')
PSID = os.getenv('PSID', '')
URI_TELEGRAM = os.getenv('URI_TELEGRAM', '')
URI_FACEBOOK = os.getenv('URI_FACEBOOK', '')
def get_facebook_users(notification_type):
client = MongoClient(URI_FACEBOOK)
db = client['lino_facebook']
users = db.users.find(
{
"notification": {
"description": notification_type,
"value": True
}
},
{
'_id': 0,
'sender_id': 1
}
)
return users
def get_daily_menu():
day = time.strftime('%A', time.localtime())
if day in build_valid_days():
# Change the url if you have your own webcrawler server
try:
response = requests.get(
'http://webcrawler-ru.botlino.com.br/cardapio/{}/Jantar'
.format(day)
).json()
except ValueError:
logging.warning('Decoding JSON has failed')
response = None
else:
response = None
return response
def build_valid_days():
return [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday'
]
def parse_daily_notification_to_json(menu):
messages = []
messages.append('Na janta de hoje, teremos:')
text = ''
for item in menu:
prev = item
item = item.replace(':', '')
text += '*{}*'.format(item) + ':' + ' ' + menu[prev] + '\n'
messages.append(text)
return messages
def notify_daily_meal_to_telegram(messages, telegram_users):
chats = telegram_users
for chat in chats:
for message in messages:
requests.get(
'https://api.telegram.org/bot{}\
/sendChatAction?chat_id={}&action=typing'
.format(TELEGRAM_ACCESS_TOKEN, chat['sender_id'])).json()
time.sleep(1)
requests.get(
'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}'
.format(TELEGRAM_ACCESS_TOKEN, chat['sender_id'], message) +
'&parse_mode=Markdown'
).json()
def notify_daily_meal_to_facebook(messages, facebook_users):
chats = facebook_users
for chat in chats:
for message in messages:
builded_message = build_facebook_message(
chat['sender_id'],
message + '\n'
)
postfields = urlencode(builded_message)
url = get_url_facebook_parameter()
curl = pycurl.Curl()
curl.setopt(curl.URL, url)
curl.setopt(curl.POSTFIELDS, postfields)
curl.perform()
curl.close()
def build_facebook_message(sender_id, message):
return {
'recipient': {
'id': sender_id
},
'message': {
'text': message
}
}
def get_url_facebook_parameter():
return ('https://graph.facebook.com/v2.6/{}/messages?access_token={}'
.format(PSID, FACEBOOK_ACCESS_TOKEN))
dinner_menu = get_daily_menu()
telegram_users = get_telegram_users('lunch meal')
facebook_users = get_facebook_users('daily meal')
if dinner_menu:
messages = parse_daily_notification_to_json(dinner_menu)
notify_daily_meal_to_telegram(messages, telegram_users)
notify_daily_meal_to_facebook(messages, facebook_users)
else:
messages = []
messages.append('Não consegui pegar o cardápio pra você hoje... :(')
messages.append('Parece que teve algum problema com o site do RU')
notify_daily_meal_to_telegram(messages, telegram_users)
notify_daily_meal_to_facebook(messages, facebook_users)