Skip to content

Instantly share code, notes, and snippets.

@dwl285
Created November 1, 2019 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwl285/a4e192d4112c2612635a173a4ceb5dd8 to your computer and use it in GitHub Desktop.
Save dwl285/a4e192d4112c2612635a173a4ceb5dd8 to your computer and use it in GitHub Desktop.
Sending data from BigQuery to Intercom using Google Cloud Functions
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())
# Function dependencies, for example:
# package>=version
requests
google-cloud-bigquery
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