-
-
Save repodevs/64d0fd4bad8bb18aaf691cc17f5ddb84 to your computer and use it in GitHub Desktop.
Email notifications for chats received in Fonality HUD when away
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Quick and (very) dirty script to send emails when a user gets a message and is away from HUD | |
from base64 import b64decode | |
from hashlib import sha256 | |
import sqlite3 | |
import datetime | |
import itertools | |
import os | |
import sys | |
import xml.etree.ElementTree as ET | |
from secret_conf import SENDGRID_API_KEY | |
import sendgrid | |
# Some constants | |
log_dir = '/var/fonality/hud3.5.0/caches/users/' | |
u3_file = '/etc/asterisk/hud3.5/u3.xml' | |
sent_database = '/opt/flow/db/hudnotifications.db' | |
# Set up the database | |
conn = sqlite3.connect(sent_database) | |
try: | |
conn.execute('create table if not exists sent_notification(hash char(64));') | |
except sqlite3.DatabaseError: | |
# DB is corrupt somehow! | |
os.remove(sent_database) | |
conn = sqlite3.connect(sent_database) | |
conn.execute('create table if not exists sent_notification(hash char(64));') | |
# Parse the XML... | |
u3 = ET.parse(u3_file).getroot() | |
servers = list(u3) | |
users = dict([(user.attrib['username'], user) for user in list(itertools.chain( | |
*[list(server) for server in servers])) if user.attrib['inHUD'] == '1']) | |
#Iterate through each user | |
for username, user in users.iteritems(): | |
email = user.find('email').text | |
history_file = '%s/%s/history' % (log_dir, username) | |
if os.path.exists(history_file): | |
with open(history_file, 'r') as fh: | |
contents = fh.read() | |
history = ET.fromstring('<chats>%s</chats>' % contents) | |
for idx, chat in enumerate(history): | |
hash = sha256(ET.tostring(chat)).hexdigest() | |
rows = list(conn.execute('select * from sent_notification where hash=?', (hash,))) | |
if len(rows) != 0: | |
continue | |
to_username = '@'.join(chat.attrib['to'].split('@')[:-1]) | |
if to_username != username: | |
continue | |
if(idx == len(history) - 1): | |
continue | |
next_chat = history[idx + 1] | |
timestamp = datetime.datetime.fromtimestamp( | |
int(chat.attrib['ts']) / 1000.0) | |
next_timestamp = datetime.datetime.fromtimestamp( | |
int(next_chat.attrib['ts']) / 1000.0) | |
ago = datetime.datetime.now() - timestamp | |
if ago > datetime.timedelta(minutes=10): | |
continue | |
if (next_timestamp - timestamp) > datetime.timedelta(seconds=10): | |
continue | |
text = b64decode(chat[0].find('{u:m}p').text) | |
html = chat[0].find('{u:m}f').text | |
next_text = b64decode(next_chat[0].find('{u:m}p').text) | |
next_html = next_chat[0].find('{u:m}f').text | |
if text and not next_html: | |
from_user = "@".join(chat.get('f').split('@')[:-1]) | |
first_name = users[from_user].find('firstName').text if users[ | |
from_user].find('firstName') != None else '' | |
last_name = users[from_user].find('lastName').text if users[ | |
from_user].find('lastName') != None else '' | |
if first_name or last_name: | |
name = '%s %s' % (first_name, last_name) | |
else: | |
name = username | |
subject = "Missed HUD Message from %s" % name.strip() | |
body = "<%s> %s" % ( | |
timestamp.strftime('%Y/%b/%d %H:%M'), text) | |
from_email = users[from_user].find('email').text | |
sg = sendgrid.SendGridClient(SENDGRID_API_KEY) | |
msg = sendgrid.Mail() | |
msg.add_to(email) | |
msg.set_from('%s <%s>' % (name, from_email)) | |
msg.set_text(body) | |
msg.set_subject(subject) | |
status, result = sg.send(msg) | |
if status == 200: | |
cur = conn.cursor() | |
cur.execute('insert into sent_notification(hash) values(?)', (hash,)) | |
conn.commit() | |
cur.close() | |
else: | |
print result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SENDGRID_API_KEY='your_key_here' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment