def main(event, context):
    db_url = get_db_url()
    db_engine = create_engine(db_url, echo=False)
    session = sessionmaker(bind=db_engine)()

    payload_json = event

    if payload_json and 'from' in payload_json and 'data' in payload_json:
        if payload_json['from'] == 'director-function':
            details = payload_json['data']
            with db_engine.connect() as conn:
                data_sets = [{
                    'type':
                    'connection',
                    'data':
                    conn.execute("call new_connections('{}');".format(
                        details['clientid']))
                }, {
                    'type':
                    'message',
                    'data':
                    conn.execute("call new_messages('{}');".format(
                        details['clientid']))
                }]
            try:
                email_body = session.query(Daily_tasks_email).filter(
                    Daily_tasks_email.id ==
                    details['daily_tasks_email_id']).first().body
                email_subject = session.query(Daily_tasks_email).filter(
                    Daily_tasks_email.id ==
                    details['daily_tasks_email_id']).first().subject
            except Exception as err:
                print(
                    "There was an error while querying the DB for email_body and subject. Error: {}"
                    .format(err))

            for data_set in data_sets:
                email_body = populate_table(email_body, data_set)

            email_body = tailor_email(email_body, details['client_ulincid'],
                                      details['client_firstname'])
            send_email(email_body, email_subject, details)
        else:
            print(
                "The pubsub message was not sent from the director-function Function"
            )
    else:
        print("Pubsub message payload is missing the from or data key")
Esempio n. 2
0
def main(event, context):
    demoji.download_codes()
    db_url = get_db_url()
    db_engine = create_engine(db_url, echo=False)
    session = sessionmaker(bind=db_engine)()

    # pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    payload_json = event
    # print(payload_json)
    # return payload_json

    if payload_json and 'from' in payload_json and 'data' in payload_json:
        if payload_json['from'] == 'director-function':
            webhooks = payload_json['data']['webhooks']
            client = session.query(Client).filter(
                Client.id == payload_json['data']['clientid']).first()

            wh_res_id_list = []
            for wh in webhooks:
                wh_res = poll_webhook(wh['url'])
                print(wh_res)
                if len(wh_res) > 0:
                    wh_res_id = save_webhook_res(
                        client.id, wh_res, wh['type'], session
                    )  #  Save the webhook response into the mysql instance
                    wh_res_id_list.append({
                        'type': wh['type'],
                        'id': wh_res_id
                    })
                else:
                    print('{} webhook response was empty for client {} {}'.
                          format(wh['type'], client.firstname,
                                 client.lastname))
            if len(wh_res_id_list) > 0:
                for item_dict in wh_res_id_list:
                    handle_jdata(
                        client, item_dict, session
                    )  #  Handle the jdata from the webhook response by parsing and inserting into the contacts and activity tables
            print('Saved and handled all webhook responses')
        else:
            print(
                "The pubsub message was not sent from the director-function function"
            )
    else:
        print(
            "Pubsub message payload is missing the from, webhooks, or clientid keys"
        )
Esempio n. 3
0
def main():
    db_url = get_db_url()
    db_engine = create_engine(db_url, echo=False)
    session = sessionmaker(bind=db_engine)()

    contact_info = {
        'id': '1234',
        'first_name': 'Alex',
        'last_name': 'White',
        'title': 'Whatever',
        'company': 'Whatever',
    }
    campaignid = 'a4fc093e-1551-11eb-9daa-42010a8002ff'
    clientid = '63bf6eca-1d2b-11eb-9daa-42010a8002ff'
    wh_id = str(uuid.uuid4())
    wh_type = 'connection'

    new_contact = create_new_contact(contact_info, campaignid, clientid, wh_id, wh_type)
    new_activity = create_new_activity(new_contact.id, None, 1, None, None)
    # new_activity = create_new_activity('00f0be86-5a98-4323-8a68-9da074bb49a1', None, 1, None, None)
    for x in ['23ea57d4-f0c6-4322-a9be-619c3aadfca5']:
        new_activity = create_new_activity(x, datetime.now(), 3, 'Hello there there there there', None)
        session.add(new_activity)
        session.commit()
            for data_set in data_sets:
                email_body = populate_table(email_body, data_set)

            email_body = tailor_email(email_body, details['client_ulincid'],
                                      details['client_firstname'])
            send_email(email_body, email_subject, details)
        else:
            print(
                "The pubsub message was not sent from the director-function Function"
            )
    else:
        print("Pubsub message payload is missing the from or data key")


if __name__ == '__main__':
    db_url = get_db_url()
    db_engine = create_engine(db_url, echo=False)
    session = sessionmaker(bind=db_engine)()

    # client = session.query(Client).filter(Client.isactive == 1).filter(Client.id == '879dcc21-3335-11eb-865a-42010a3d0004').first() # shelley
    client = session.query(Client).filter(Client.isactive == 1).filter(
        Client.id ==
        '6486f2e3-333b-11eb-865a-42010a3d0004').first()  # David Lewis
    client = session.query(Client).filter(Client.isactive == 1).filter(
        Client.id ==
        'f250e0e4-334a-11eb-8c70-42010a3d0004').first()  # Lesa Skipper
    clientmanager = session.query(Client_manager).filter(
        Client_manager.id == client.clientmanager).first()

    sdf_payload = {
        "trigger-type": "function",