Sending Charts to Twitter
Tutorial on how to use Zapier to integrate with Stencil
Stencil's Zapier app is coming soon. For now, we can use Stencil's webhook to do the integration.

Automating Charts Generation Workflow

In this demo we will demonstrate a real use case of workflow automation using Zapier, AWS Lambda and Twitter.
The workflow goal is to pull up data from external source, populate our template with the data, generate the image and post it to Twitter.
Despite having no Zapier app (yet, WIP), integration with Zapier is still possible. In fact, this illustrates how integration with other services can be achieved just by utilizing webhook functionality.

Requirements

If you're following along, you will need
  1. 1.
    Premium Zapier account
  2. 2.
    AWS Account
  3. 3.
    Twitter account
  4. 4.
    and of course Stencil
Premium Zapier account is required due to the Zaps that we are going to use

Overview

Here's how everything connects to each other.
  1. 1.
    We write Lambda function to pull the data we need and build modifications to send to Stencil.
  2. 2.
    We use Zapier to schedule a Zap that calls AWS Lambda function once a day.
  3. 3.
    Our lambda function then fetches the data, transforms it into modifications that can be applied to the template and then they are sent to Stencil to generate image.
  4. 4.
    We also specify Zapier webhook URL that Stencil will call once the image is generated.
  5. 5.
    The webhook is linked to Zapier Webhook Zap that in turns run another zap to parse the payload and tweet to Twitter.

Guides

1. Designing our template

The final image that will be tweeted
Our template consists of two bar graphs, three texts, one image and one rectangle that acts as a background.
We will leave the designing part as an exercise to the reader πŸ˜„
For both bar graphs, you can use the following JSON to create a default chart that we will override from the API.
1
{
2
"labels": [
3
"Johor",
4
"Kedah",
5
"Kelantan",
6
"Melaka",
7
"Negeri Sembilan",
8
"Pahang",
9
"Perak",
10
"Perlis",
11
"Pulau Pinang",
12
"Sabah",
13
"Sarawak",
14
"Selangor",
15
"Terengganu",
16
"W.P. Kuala Lumpur",
17
"W.P. Labuan",
18
"W.P. Putrajaya"
19
],
20
"datasets": [
21
{
22
"label": "Dose #1",
23
"data": [
24
100,
25
5,
26
15,
27
100,
28
0,
29
0,
30
0,
31
0,
32
0,
33
0,
34
0,
35
0,
36
0,
37
0,
38
0,
39
0
40
],
41
"backgroundColor": "#ffcc00"
42
},
43
{
44
"label": "Dose #2",
45
"data": [
46
10,
47
100,
48
150,
49
200,
50
0,
51
0,
52
0,
53
0,
54
0,
55
0,
56
0,
57
0,
58
0,
59
0,
60
0,
61
0
62
],
63
"backgroundColor": "rgba(187, 153, 224, 1)"
64
}
65
]
66
}
Copied!
Paste the above JSON into your Dataset Editor (switch to JSON View).
If you need help with Charts, please see Chart's documentation​

2. Preparing the data with AWS Lambda

We are going to use Malaysia's National Covid-​19 Immunisation Programme Open Data that can be found here, https://github.com/CITF-Malaysia/citf-public.
We are mostly interested in vaccination count by state that is referenced here, https://github.com/CITF-Malaysia/citf-public/blob/main/vaccination/vax_state.csv.
Lambda Function
1
from datetime import datetime, timedelta
2
import pytz
3
import pandas as pd
4
import requests as req
5
import json
6
​
7
def make_modification(name, states, dose1, dose2):
8
datasets = [ { "data": dose1, "label": "Dose #1" }, { "data": dose2, "label": "Dose #2" } ]
9
​
10
modification = {
11
"name": name,
12
"labels": states,
13
"datasets": datasets
14
}
15
​
16
return modification
17
​
18
def run():
19
url = "https://raw.githubusercontent.com/CITF-Malaysia/citf-public/main/vaccination/vax_state.csv"
20
21
# Get yesterday's date because data is only available up to yesterday
22
kl_timezone = pytz.timezone('Asia/Kuala_Lumpur')
23
kl_time_now = datetime.now(kl_timezone)
24
kl_time_yesterday = kl_time_now - timedelta(days=1)
25
yesterday = kl_time_yesterday.strftime("%Y-%m-%d")
26
​
27
# read the CSV from the URL
28
df = pd.read_csv(url)
29
30
# filter the data only for the date we are interested in
31
rows = df.loc[df["date"] == yesterday]
32
​
33
if not rows.empty:
34
# Only get the data that we want to trend
35
states = rows["state"].tolist()
36
daily_dose1 = rows["dose1_daily"].tolist()
37
daily_dose2 = rows["dose2_daily"].tolist()
38
​
39
cumul_dose1 = rows["dose1_cumul"].tolist()
40
cumul_dose2 = rows["dose2_cumul"].tolist()
41
​
42
# Generate modificatiosn that fit Stencil's API format
43
daily_graph = make_modification("bar_graph_daily", states, daily_dose1, daily_dose2)
44
cumul_graph = make_modification("bar_graph_cumul", states, cumul_dose1, cumul_dose2)
45
​
46
text_date = {
47
"name": "text_date",
48
"text": yesterday
49
}
50
modifications = [text_date, daily_graph, cumul_graph]
51
52
# Set our Zapier webhook to call once image is generatd
53
payload = {
54
"template": "3bc487ef-33ad-46b3-a18c-0d57c3825723",
55
"modifications": modifications,
56
"webhook_url": "your-zapier-webhook"
57
}
58
​
59
headers = {
60
"Authorization": "Bearer your-project-api-key",
61
"Content-Type": "application/json"
62
}
63
​
64
# Call Stencil's API to generate the image asynchronously
65
req.post("https://api.usestencil.com/v1/images", json=payload, headers=headers)
66
​
67
​
68
def lambda_handler(event, context):
69
run()
70
71
return {
72
'statusCode': 200,
73
'body': json.dumps('Request sent')
74
}
Copied!
Although the code may look daunting for some users, it is actually pretty straight forward. You could also use any other no-code or low-code tool out there to do this, but in this case a simple Python script should handle the job just fine.
We can now deploy our lambda function and set HTTP trigger for the lambda function. We will need this trigger URL in the next step.

3. Schedule Zap to run lambda function

The rest of the steps are straight forward and mostly filling in the details with proper information.
We are going to trigger the lambda function everyday at 7AM including weekends.
We provide the Lambda function trigger URL from previous step

4. Create Zap to listen for webhook callback and send a tweet

Our lambda function will call Stencil's API to generate the image. We also setup our payload to include Zapier webhook below. Once image is generated, it will call this webhoook.
This is the Zapier's Webhook that we set in our payload in step 2 (Lambda function)
We add another step to parse our JSON payload and assign our output variable to include the link to the image and the date of the data.
Zapier's output variable must be a dictionary
We parse the response we get from Stencil's API to send it to the next step (Twitter)
We parse the response we get from Stencil's API to send it to the next step (Twitter)
We add date to our message so the message is always unique.
You're done! Here is sample of the tweet.
Now do something productive with the extra time you've gained. This workflow is fully automated.

Debugging issues

Sometimes, problems can arise and you need to know where to look. You can view image requests by either clicking on Requests tab or by accessing your template's Console page.
You should be able to see your generated image in the list of requests. If you don't, that means Stencil does not receive your request. Please check your Zap or AWS Lambda function.
You can also see the webhook response in the log.
Twitter Zap can be disabled automatically by Zapier if they think you're sending a duplicate tweet. It is against Twitter's ToC.
Now try do the same workflow but with Integromat.