Ejemplo n.º 1
0
def processMatches(colnames, matches, currentProposal, isFromMatches):
    print(colnames)
    print(matches)
    try:
        index_action_type = colnames.index('action_type')
        index_action_subtype = colnames.index('action_subtype')
        index_action_data = colnames.index('action_data')
        index_trigger_data = colnames.index('trigger_data')
        index_trigger_action_id = colnames.index('trigger_action_id')
        index_unique_id = index_trigger_action_id
        try:
            index_unique_id = colnames.index('unique_id')
        except:
            print("No unique_id found.")
        for match in matches:
            print('match')
            print(match)
            emailAddress = match[index_action_data]['email']
            if (match[index_action_type] == 'notification') and \
                (match[index_action_subtype] == 'send_email'):
                print(f"Sending email to: {emailAddress}")

                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "to_name":
                    "",
                    "to_email":
                    emailAddress,
                    "subject":
                    coin_upper + " New Voting Phase Period",
                    "text_line":
                    coin_upper + " New Voting Phase Period",
                    "main_title":
                    coin_upper + " New Voting Phase Period: " +
                    str(currentProposal).capitalize(),
                    "trigger_text":
                    coin_upper + " New Voting Phase Period",
                    "action_text":
                    "send email to " + emailAddress,
                    "id_trigger_action":
                    match[index_unique_id]
                }
                print(payload)
                try:
                    r = requests.post(
                        'https://eqzuf5sfph.execute-api.us-west-1.amazonaws.com/default/general-email-action-test-1_hello',
                        data=json.dumps(payload))
                    print(r.status_code)
                    print(r.text)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'json'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger": match[colnames.index('trigger_subtype')],
                        "coin": coin_upper,
                        "chain": match[index_trigger_data]['chain'],
                        "proposal": str(currentProposal).capitalize()
                    }
                }
                print(payload)
                try:
                    params = {}
                    payload["dataJson"] = json.dumps(payload["dataJson"])
                    r = requests.post(url=webhookURL,
                                      params=params,
                                      data=payload)
                    print(r.json())
                    print(r.status_code)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'rpc'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger_data": {
                            "trigger":
                            match[colnames.index('trigger_subtype')],
                            "coin": coin_upper,
                            "chain": match[index_trigger_data]['chain'],
                            "proposal": str(currentProposal).capitalize()
                        }
                    }
                }
                print(payload)
                rpc_response = ""

                if match[index_action_data]["function"][
                        "name"] == 'forge_operation':
                    print("getting rpc response")
                    rpc_response = forgeOperation(
                        match[index_action_data]["function"]["data"]
                        ["sourceAddress"], match[index_action_data]["function"]
                        ["data"]["destinationAddress"],
                        match[index_action_data]["function"]["data"]["amount"])
                    payload["dataJson"]["rpc_response"] = rpc_response

                try:
                    params = {}
                    payload["dataJson"] = json.dumps(payload["dataJson"])
                    r = requests.post(url=webhookURL,
                                      params=params,
                                      data=payload)
                    print(r.json())
                    print(r.status_code)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'api_actions'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger_data": {
                            "trigger":
                            match[colnames.index('trigger_subtype')],
                            "coin": coin_upper,
                            "chain": match[index_trigger_data]['chain'],
                            "proposal": str(currentProposal).capitalize()
                        }
                    }
                }
                print(payload)
                api_response = {}

                print("getting api action response")
                try:
                    api_response = runApiFunction(
                        match[index_action_data]["api_function"]["url"],
                        match[index_action_data]["api_function"]["path"],
                        match[index_action_data]["api_function"]["method"],
                        match[index_action_data]["api_function"]["data"])
                    payload["dataJson"]["api_response"] = api_response
                except Exception as e:
                    print(e)

                try:
                    params = {}
                    payload["dataJson"] = json.dumps(payload["dataJson"])
                    r = requests.post(url=webhookURL,
                                      params=params,
                                      data=payload)
                    print(r.json())
                    print(r.status_code)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'google_sheets'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                email_to_send = ""
                if webhookURL == "":
                    email_to_send = match[index_action_data]['email']
                    print(f"Update: Sending email to: {email_to_send}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger": match[colnames.index('trigger_subtype')],
                        "coin": coin_upper,
                        "chain": match[index_trigger_data]['chain'],
                        "proposal": str(currentProposal).capitalize()
                    }
                }
                print(payload)
                api_response = {}

                print("getting google sheets response")
                try:
                    api_response = runGSheetsFunction(
                        "https://sheets.googleapis.com/v4/spreadsheets/" +
                        match[index_action_data]["sheets_data"]
                        ["spreadsheetId"] + "/values/" +
                        match[index_action_data]["sheets_data"]["sheetName"] +
                        "!" + match[index_action_data]["sheets_data"]["rows"] +
                        "?key=AIzaSyDKjM9lKCZTTA66_dyTkrSaIThSSBaWu1s", "",
                        "GET", {})
                    payload["dataJson"]["api_response"] = api_response
                except Exception as e:
                    print(e)

                if webhookURL != "":
                    try:
                        params = {}
                        payload["dataJson"] = json.dumps(payload["dataJson"])
                        r = requests.post(url=webhookURL,
                                          params=params,
                                          data=payload)
                        print(r.json())
                        print(r.status_code)
                    except Exception as e:
                        print(e)

                elif email_to_send != "":
                    email_payload = {
                        "to_name":
                        "",
                        "to_email":
                        emailAddress,
                        "subject":
                        coin_upper + " New Voting Phase Period",
                        "text_line":
                        coin_upper + " New Voting Phase Period",
                        "main_title":
                        coin_upper + " New Voting Phase Period: " +
                        str(currentProposal).capitalize() +
                        " Google Sheets Data: " + json.dumps(api_response),
                        "trigger_text":
                        coin_upper + " New Voting Phase Period",
                        "action_text":
                        "send email to " + emailAddress,
                        "id_trigger_action":
                        match[index_unique_id]
                    }
                    print(email_payload)
                    try:
                        r = requests.post(
                            'https://eqzuf5sfph.execute-api.us-west-1.amazonaws.com/default/general-email-action-test-1_hello',
                            data=json.dumps(email_payload))
                        print(r.status_code)
                        print(r.text)
                    except Exception as e:
                        print(e)

    except ValueError as err:
        print("Cannot process matches - actionXXX columns not found: ", err)
        return

    for match in matches:
        pass
def processMatches(colnames, matches, isFromMatches):
    print(colnames)
    print(matches)
    try:
        index_tx_proposals = colnames.index('tx_proposals')
        index_action_type = colnames.index('action_type')
        index_action_subtype = colnames.index('action_subtype')
        index_action_data = colnames.index('action_data')
        index_trigger_data = colnames.index('trigger_data')
        index_trigger_action_id = colnames.index('trigger_action_id')
        index_unique_id = index_trigger_action_id
        try:
            index_unique_id = colnames.index('unique_id')
        except:
            print("No unique_id found.")
        for match in matches:
            print('match')
            print(match)
            if (match[index_action_type] == 'notification') and \
                (match[index_action_subtype] == 'send_email'):
                emailAddress = match[index_action_data]['email']
                print(f"Sending email to: {emailAddress}")

                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "to_name":
                    "",
                    "to_email":
                    emailAddress,
                    "subject":
                    coin_upper + " New Governance Proposal",
                    "text_line":
                    coin_upper + " New Governance Proposal",
                    "main_title":
                    coin_upper + " New Governance Proposal: " +
                    str(match[index_tx_proposals]) + " at Operation Hash " +
                    match[colnames.index('tx_hash')],
                    "trigger_text":
                    coin_upper + " New Governance Proposal",
                    "action_text":
                    "send email to " + emailAddress,
                    "id_trigger_action":
                    match[index_unique_id]
                }
                print(payload)
                try:
                    # Create link to call Mailgun or other email provider
                    r = requests.post('EMAIL-URL', data=json.dumps(payload))
                    print(r.status_code)
                    print(r.text)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'json'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger": match[colnames.index('trigger_subtype')],
                        "coin": coin_upper,
                        "chain": match[index_trigger_data]['chain'],
                        "proposals": match[index_tx_proposals],
                        "txnHash": match[colnames.index('tx_hash')]
                    }
                }
                print(payload)
                try:
                    params = {}
                    payload["dataJson"] = json.dumps(payload["dataJson"])
                    r = requests.post(url=webhookURL,
                                      params=params,
                                      data=payload)
                    print(r.json())
                    print(r.status_code)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'rpc'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger_data": {
                            "trigger":
                            match[colnames.index('trigger_subtype')],
                            "coin": coin_upper,
                            "chain": match[index_trigger_data]['chain'],
                            "proposals": match[index_tx_proposals],
                            "txnHash": match[colnames.index('tx_hash')]
                        }
                    }
                }
                print(payload)
                rpc_response = ""

                if match[index_action_data]["function"][
                        "name"] == 'forge_operation':
                    print("getting rpc response")
                    rpc_response = forgeOperation(
                        match[index_action_data]["function"]["data"]
                        ["sourceAddress"], match[index_action_data]["function"]
                        ["data"]["destinationAddress"],
                        match[index_action_data]["function"]["data"]["amount"])
                    payload["dataJson"]["rpc_response"] = rpc_response

                try:
                    params = {}
                    payload["dataJson"] = json.dumps(payload["dataJson"])
                    r = requests.post(url=webhookURL,
                                      params=params,
                                      data=payload)
                    print(r.json())
                    print(r.status_code)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'api_actions'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger_data": {
                            "trigger":
                            match[colnames.index('trigger_subtype')],
                            "coin": coin_upper,
                            "chain": match[index_trigger_data]['chain'],
                            "proposals": match[index_tx_proposals],
                            "txnHash": match[colnames.index('tx_hash')]
                        }
                    }
                }
                print(payload)
                api_response = {}

                print("getting api action response")
                try:
                    api_response = runApiFunction(
                        match[index_action_data]["api_function"]["url"],
                        match[index_action_data]["api_function"]["path"],
                        match[index_action_data]["api_function"]["method"],
                        match[index_action_data]["api_function"]["data"])
                    payload["dataJson"]["api_response"] = api_response
                except Exception as e:
                    print(e)

                try:
                    params = {}
                    payload["dataJson"] = json.dumps(payload["dataJson"])
                    r = requests.post(url=webhookURL,
                                      params=params,
                                      data=payload)
                    print(r.json())
                    print(r.status_code)
                except Exception as e:
                    print(e)

            elif (match[index_action_type] == 'webhook') and \
                (match[index_action_subtype] == 'google_sheets'):
                webhookURL = match[index_action_data]['webhook']
                print(f"Sending webhook to: {webhookURL}")
                email_to_send = ""
                if webhookURL == "":
                    email_to_send = match[index_action_data]['email']
                    print(f"Update: Sending email to: {email_to_send}")
                coin_upper = match[index_trigger_data]['coin'].upper()
                payload = {
                    "dataJson": {
                        "trigger": match[colnames.index('trigger_subtype')],
                        "coin": coin_upper,
                        "chain": match[index_trigger_data]['chain'],
                        "proposals": match[index_tx_proposals],
                        "txnHash": match[colnames.index('tx_hash')]
                    }
                }
                print(payload)
                api_response = {}

                print("getting google sheets response")
                try:
                    api_response = runGSheetsFunction(
                        "https://sheets.googleapis.com/v4/spreadsheets/" +
                        match[index_action_data]["sheets_data"]
                        ["spreadsheetId"] + "/values/" +
                        match[index_action_data]["sheets_data"]["sheetName"] +
                        "!" + match[index_action_data]["sheets_data"]["rows"] +
                        "?key=GOOGLE-SHEETS-API-KEY", "", "GET", {})
                    payload["dataJson"]["api_response"] = api_response
                except Exception as e:
                    print(e)

                if webhookURL != "":
                    try:
                        params = {}
                        payload["dataJson"] = json.dumps(payload["dataJson"])
                        r = requests.post(url=webhookURL,
                                          params=params,
                                          data=payload)
                        print(r.json())
                        print(r.status_code)
                    except Exception as e:
                        print(e)

                elif email_to_send != "":
                    email_payload = {
                        "to_name":
                        "",
                        "to_email":
                        emailAddress,
                        "subject":
                        coin_upper + " New Governance Proposal",
                        "text_line":
                        coin_upper + " New Governance Proposal",
                        "main_title":
                        coin_upper + " New Governance Proposal: " +
                        str(match[index_tx_proposals]) +
                        " Google Sheets Data: " + json.dumps(api_response) +
                        " at Operation Hash " +
                        match[colnames.index('tx_hash')],
                        "trigger_text":
                        coin_upper + " New Governance Proposal",
                        "action_text":
                        "send email to " + emailAddress,
                        "id_trigger_action":
                        match[index_unique_id]
                    }
                    print(email_payload)
                    try:
                        r = requests.post('EMAIL-URL',
                                          data=json.dumps(email_payload))
                        print(r.status_code)
                        print(r.text)
                    except Exception as e:
                        print(e)

    except ValueError:
        print("Cannot process matches - actionXXX columns not found")
        return

    for match in matches:
        pass
def send_webhook(match, index_action_type, index_action_subtype,
                 index_action_data, dataJson):

    if (match[index_action_type] == 'webhook') and \
        (match[index_action_subtype] == 'json'):
        webhookURL = match[index_action_data]['webhook']
        print(f"Sending webhook to: {webhookURL}")
        payload = {"dataJson": dataJson}
        print(payload)
        try:
            params = {}
            payload["dataJson"] = json.dumps(payload["dataJson"])
            r = requests.post(url=webhookURL, params=params, data=payload)
            print(r.json())
            print(r.status_code)
        except Exception as e:
            print(e)

    elif (match[index_action_type] == 'webhook') and \
        (match[index_action_subtype] == 'rpc'):
        webhookURL = match[index_action_data]['webhook']
        print(f"Sending webhook to: {webhookURL}")
        payload = {"dataJson": dataJson}
        print(payload)
        rpc_response = ""

        if match[index_action_data]["function"]["name"] == 'forge_operation':
            print("getting rpc response")
            rpc_response = forgeOperation(
                match[index_action_data]["function"]["data"]["sourceAddress"],
                match[index_action_data]["function"]["data"]
                ["destinationAddress"],
                match[index_action_data]["function"]["data"]["amount"])
            payload["dataJson"]["rpc_response"] = rpc_response

        try:
            params = {}
            payload["dataJson"] = json.dumps(payload["dataJson"])
            r = requests.post(url=webhookURL, params=params, data=payload)
            print(r.json())
            print(r.status_code)
        except Exception as e:
            print(e)

    elif (match[index_action_type] == 'webhook') and \
        (match[index_action_subtype] == 'api_actions'):
        webhookURL = match[index_action_data]['webhook']
        print(f"Sending webhook to: {webhookURL}")
        payload = {"dataJson": dataJson}
        print(payload)
        api_response = {}

        print("getting api action response")
        try:
            api_response = runApiFunction(
                match[index_action_data]["api_function"]["url"],
                match[index_action_data]["api_function"]["path"],
                match[index_action_data]["api_function"]["method"],
                match[index_action_data]["api_function"]["data"])
            payload["dataJson"]["api_response"] = api_response
        except Exception as e:
            print(e)

        try:
            params = {}
            payload["dataJson"] = json.dumps(payload["dataJson"])
            r = requests.post(url=webhookURL, params=params, data=payload)
            print(r.json())
            print(r.status_code)
        except Exception as e:
            print(e)

    elif (match[index_action_type] == 'webhook') and \
        (match[index_action_subtype] == 'google_sheets'):
        webhookURL = match[index_action_data]['webhook']
        print(f"Sending webhook to: {webhookURL}")
        email_to_send = ""
        if webhookURL == "":
            email_to_send = match[index_action_data]['email']
            print(f"Update: Sending email to: {email_to_send}")

        payload = {"dataJson": dataJson}
        print(payload)
        api_response = {}

        print("getting google sheets response")
        try:
            api_response = runGSheetsFunction(
                "https://sheets.googleapis.com/v4/spreadsheets/" +
                match[index_action_data]["sheets_data"]["spreadsheetId"] +
                "/values/" +
                match[index_action_data]["sheets_data"]["sheetName"] + "!" +
                match[index_action_data]["sheets_data"]["rows"] +
                "?key=GOOGLE-SHEETS-API-KEY", "", "GET", {})
            payload["dataJson"]["api_response"] = api_response
        except Exception as e:
            print(e)

        if webhookURL != "":
            try:
                params = {}
                payload["dataJson"] = json.dumps(payload["dataJson"])
                r = requests.post(url=webhookURL, params=params, data=payload)
                print(r.json())
                print(r.status_code)
            except Exception as e:
                print(e)

        elif email_to_send != "":
            try:
                coin_upper = match[colnames.index(
                    'trigger_data')]['coin'].upper()
                trigger_subtype_str = match[colnames.index('trigger_subtype')]
                if trigger_subtype_str == 'pricing':
                    trigger_subtype_str = 'price'
                payload = {
                    "to_name":
                    "",
                    "to_email":
                    email_to_send,
                    "subject":
                    coin_upper +
                    " " + match[colnames.index('trigger_subtype')].replace(
                        '_', ' ').capitalize() + " is " + match[colnames.index(
                            'trigger_data')]['direction'].capitalize() + " $" +
                    str('{0:,.2f}'.format(
                        float(match[colnames.index('trigger_data')]
                              [trigger_subtype_str]))),
                    "text_line":
                    coin_upper +
                    " " + match[colnames.index('trigger_subtype')].replace(
                        '_', ' ').capitalize() + " is " + match[colnames.index(
                            'trigger_data')]['direction'].capitalize() + " $" +
                    str("{0:,.2f}".format(
                        float(match[colnames.index('trigger_data')]
                              [trigger_subtype_str]))),
                    "main_title":
                    coin_upper +
                    " " + match[colnames.index('trigger_subtype')].replace(
                        '_', ' ').capitalize() + " is " + match[colnames.index(
                            'trigger_data')]['direction'].capitalize() + " $" +
                    str("{0:,.2f}".format(
                        float(match[colnames.index('trigger_data')]
                              [trigger_subtype_str]))) +
                    " Google Sheets Data: " + json.dumps(api_response),
                    "trigger_text":
                    coin_upper +
                    " " + match[colnames.index('trigger_subtype')].replace(
                        '_', ' ').capitalize() + " is " + match[colnames.index(
                            'trigger_data')]['direction'].capitalize() + " $" +
                    str("{0:,.2f}".format(
                        float(match[colnames.index('trigger_data')]
                              [trigger_subtype_str]))),
                    "action_text":
                    "send email to " + emailAddress,
                    "id_trigger_action":
                    match[index_trigger_action_id]
                }
                print(payload)
                try:
                    r = requests.post('EMAIL-URL', data=json.dumps(payload))
                    print(r.status_code)
                    print(r.text)
                except Exception as e:
                    print(e)
                return
            except Exception as e:
                print(e)