Created
November 1, 2019 15:27
-
-
Save dwl285/a4e192d4112c2612635a173a4ceb5dd8 to your computer and use it in GitHub Desktop.
Sending data from BigQuery to Intercom using Google Cloud Functions
This file contains 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
import requests | |
from google.cloud import bigquery | |
# Intercom API access header | |
headers = { | |
'Authorization': 'Bearer access-token', | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
} | |
# prepare for querying BQ | |
client = bigquery.Client() | |
def send_data(event, context): | |
# query the data you'd like to send to Intercom | |
query = ( | |
"SELECT email, field_to_send FROM `project-name.dataset_name.table_name` " | |
) | |
query_job = client.query( | |
query, | |
location="US", | |
) | |
# for each row in the data, make a http post request | |
for row in query_job: | |
email = row.email | |
field_to_send = row.field_to_send | |
values = f""" | |
{{ | |
"email": "{email}", | |
"custom_attributes": {{ | |
"field_to_send": "{field_to_send}" | |
}} | |
}} | |
""" | |
response = requests.post('https://api.intercom.io/users', headers=headers, data=values) | |
print(response.json()) |
This file contains 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
# Function dependencies, for example: | |
# package>=version | |
requests | |
google-cloud-bigquery |
This file contains 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
resource.type="bigquery_resource" | |
protoPayload.methodName="jobservice.jobcompleted" | |
protoPayload.serviceData.jobCompletedEvent.job.jobConfiguration.query.destinationTable.datasetId="dataset_name" | |
protoPayload.serviceData.jobCompletedEvent.job.jobConfiguration.query.destinationTable.tableId="table_name" | |
severity="INFO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment