def __init__(self, dbhost, dbusername, dbpass, dbname, sinkaddr, skey, ivkey): self.key = skey self.iv = ivkey self.context = zmq.Context() self.receiver = self.context.socket(zmq.PULL) self.receiver.bind(sinkaddr) self.syslog = SystemLog() self.db = DatabaseConnection(dbhost, dbusername, dbpass, dbname) self.inbox = Inbox(self.db) self.outbox = Outbox(self.db)
def handle(self, *args, **options): def on_message(to, sender, subject, body): # fn = "mymsg_{}.txt".format(get_random_string(5)) # with open(fn, "wb") as f: # f.write(body) # print "written {} bytes to {}.".format(len(body), fn) text, html, images = parse_message(body) msg = add_email(sender, to, subject, text, html, images) # TODO: send confirmation email # TODO: send email to managers inbox = Inbox(on_message, settings.TURTLES_SMTP_PORT, settings.TURTLES_SMTP_HOST) inbox.serve()
def __init__(self): self.syncDB = DatabaseConnection(env.DB_HOST, env.DB_UNAME, env.DB_PASSWORD, env.DB_NAME) # self.statusDB = DatabaseConnection( # env.DB_HOST, env.DB_UNAME, env.DB_PASSWORD, env.DB_NAME) self.limitRow = env.LIMIT_PROC_ROW self.outbox = Outbox(self.syncDB) self.systemlog = SystemLog() self.inbox = Inbox(self.syncDB) self.outbox = Outbox(self.syncDB) self.clientIdStartFrom = 10 self.updateToZeroHistory = set([]) self.PKFileName = 'pk' self.nextPriToProcess = dict() self.PRIFileName = 'pri'
def main(): inbox = Inbox() parser = Parser() telegram = TelegramAPI() # Async callback when email is received @inbox.collate def handle(to, sender, body): try: str_body = quopri.decodestring(body) str_body = str_body.decode("ascii", "ignore") except Exception as e: log.plog(e) return # Caltex Singapore's receipt email body for CaltexGO if ("Thank You - Successful Payment (" not in str_body): # Ignore emails if conditions not met # e.g. using a very specific toaddr like [email protected] print(str_body) return try: # Parses the fixed-format email to extract date, refill, cost per litre ddmmyy, refilled, costperlitre = parser.extract_info(str_body) except ParsingException as e: log.plog("{} (sender: {})".format(e, sender)) return try: # Uses Gspread to get previous mileage sheets_api = SheetsAPI() prev_mileage = sheets_api.get_prev_mileage() # Uses Telegram bot to prompt user for current mileage mileage = telegram.prompt_for_mileage(prev_mileage) except ParsingException as e: print(e) return # Uses Gspread to access Google Sheets API to update cells sheets_api = SheetsAPI() sheets_api.update_row(ddmmyy, mileage, refilled, costperlitre) log.plog("update_row: {} {} {} {}".format(ddmmyy, mileage, refilled, costperlitre)) inbox.serve(address='0.0.0.0', port=4467)
from re import match from inbox import Inbox from crm.settings import ATTACHMENTS_DIR, STATIC_URL_PATH, SENDGRID_API_KEY, SUPPORT_EMAIL from crm.mailer import sendemail, parse_email_body from crm.db import RootModel, db from crm import app from crm.apps.user.models import User from crm.apps.contact.models import Contact from crm.apps.message.models import Message, MessageState from crm.apps.link.models import Link import click PATTERN_TO_ROOTOBJ = r'(?P<objid>\w{5})_(?P<rootobjtype>\w+)@(?P<domain>.+)' PATTERN_SUPPORT_EMAIL = r'support@(?P<domain>.+)' inbox = Inbox() @inbox.collate def handle_mail(to, sender, subject, body): """ Fired on every new email received @param to [str]: receivers list. [should be in format $uid_roottypeobj@$domain]. @param sender str: sender email. [should be in CRM database users/contacts emails] @param subject str: subject @param body email.Message: email message object. If sender is not in recognized senders (contacts/users emails) an email will be sent back to him to contact support. If sender is in recognized senders: we get the correct object receiving the message and attach the email text body to its messages. If receiever is SUPPORT_EMAIL: an email will be sent to it using sendgrid.