Down below you’ll find a bit of code I wrote to have Google Apps Script automatically get my latest tweet and include it in my Gmail signature. Is this necessary? No. Is it kind of cool and will it make your friends jealous? You tell me.
First, a shout-out to Wesley Chun (@wescpy) for the inspiration. And to Martin Hawksey (@mhawksey) for his wonderful TwtrService which made working with Twitter’s API a breeze. Let’s get started.
Getting Apps Script to talk to Twitter
The steps below will walk you through creating a Twitter app that can be called by TwtrService. In this example we’ll link these two pieces together to retrieve your latest tweet. But TwtrService is capable of a lot more.
- Create a new Twitter App at http://apps.twitter.com. This app is what our Google Apps Script project will use to handle calls to Twitter’s API.
IMPORTANT: Be sure to enter https://script.google.com/macros/ in the Callback URL field. - Grab your newly created Twitter App’s API Key and API Secret. You can find these beneath the “Keys and Access Tokens” heading. Enter them on THIS PAGE.
- Click through the prompts to authorize Apps Script to connect to an external service, then sign in with the Twitter account you used to create the app in step 1. Authorize the app to talk to Google Apps script.
- If all went as planned, you should see the below screen. Go ahead and close the tab.
The code
We’ll first need to create a new Apps Script project and enable the Gmail API. Next we’ll add the TwtrService library to said Apps Script project and paste in the code that does the heavy lifting. The getLatestTweet
function will retrieve your latest tweet and return it, along with some custom HTML, for use as your Gmail signature. The setSignature
function calls getLatestTweet
and sets whatever it gets back as your Gmail signature.
- Create a new project at http://script.google.com. Be sure to give it a title.
- Enable the Gmail API for this project under Resources–>Advanced Google services then click the link to enable the API in the Google Developers Console.
- Search for the Gmail API and click through to enable it. Close the API Manager tab.
- Return to your Apps Script project and click OK. Be sure that you’ve got the Gmail API enabled.
- Click on Resources–>Libraries…
- The key for the TwtrService library is
MarIlVOhstkJA6QjPgCWAHIq9hSqx7jwh
. Paste it in the proper field and click Select. - Select the latest version of the library (14 as of today) and click Save.
- Paste in the code below. Be sure to put your Twitter handle in the proper place on lines 3, 14, & 16 as well as your Gmail address on line 24. Lastly, please be sure to modify the HTML on lines 12-16 suit your needs.
Testing functionality and automating the script
Let’s check to make sure the script is running as expected. If it is, we’ll setup a time-based trigger so that your Gmail signature is always up to date with your latest tweet.
- Select the
getLatestTweet
function from the drop-down and click the Run button.
- Look for errors under View–>Execution transcript
- If all’s well, go ahead and repeat steps 1&2, but this time select the
setSignature
function before running. - Compose a Gmail message in a new tab to see your new Twitter-iffic signature.
- To automate this process, let’s set a time-driven trigger for the
setSignature
function under Resources–>Current project’s triggers.
Credits
Thanks again to Wesley Chun (@wescpy) and Martin Hawksey (@mhawksey). Click here to read up on some of the cool things you can do with your Apps Script super powers and TwtrService. Also, be sure to check out the Google Developers YouTube Channel for more inspiration.
6 Comments
Hello Matt,
Thanks for your post. It’s very interesting tool. I go step by step but get error: “TypeError: No se puede leer la propiedad “id_str” de undefined. I don’t have any idea. Any suggestion?
Thanks in advance
It’s hard to say what’s going wrong here. Can you please post the execution transcript here?
Hello Matt,
Thanksfor your reply. I get the error “Missing required scope “https://www.googleapis.com/auth/gmail.settings.sharing” for modifying non-primary SendAs (línea 24, archivo “Código”)”
when I check the “setSignture” function.
Any idea?. Thanks
It’s posible to config in a setSignture function an Email address other than gmail? I set in this function “info@tabletspc.es” and get that error: “Missing required scope “https://www.googleapis.com/auth/gmail.settings.sharing” for modifying non-primary SendAs (línea 24, archivo “Código”)”
and this is the code of that line: ” var rsp = Gmail.Users.Settings.SendAs.patch(data, ‘me’, ‘info@tabletspc.es’);”
That is the log:
[17-01-28 20:54:50:027 CET] Iniciando ejecución
[17-01-28 20:54:50:043 CET] CacheService.getUserCache() [0,001 segundos]
[17-01-28 20:54:50:063 CET] Cache.get([consumerKey]) [0,02 segundos]
[17-01-28 20:54:50:065 CET] CacheService.getUserCache() [0,001 segundos]
[17-01-28 20:54:50:084 CET] Cache.get([consumerSecret]) [0,019 segundos]
[17-01-28 20:54:50:085 CET] CacheService.getScriptCache() [0,001 segundos]
[17-01-28 20:54:50:104 CET] Cache.get([service_url]) [0,018 segundos]
[17-01-28 20:54:50:106 CET] CacheService.getScriptCache() [0,001 segundos]
[17-01-28 20:54:50:125 CET] Cache.get([hash]) [0,019 segundos]
[17-01-28 20:54:50:128 CET] CacheService.getUserCache() [0,001 segundos]
[17-01-28 20:54:50:148 CET] Cache.get([oauth_token]) [0,019 segundos]
[17-01-28 20:54:50:150 CET] CacheService.getUserCache() [0,001 segundos]
[17-01-28 20:54:50:171 CET] Cache.get([oauth_token_secret]) [0,021 segundos]
[17-01-28 20:54:50:352 CET] UrlFetchApp.fetch([https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=tabletspcceuta&count=1, {headers={Authorization=OAuth realm=””,oauth_consumer_key=”e7kUqpdbnAsLOB6DNqAcEavgg”,oauth_token=”2453066827-AAsd}, =, =}]…) [0,098 segundos]
[17-01-28 20:54:50:353 CET] HTTPResponse.getResponseCode() [0 segundos]
[17-01-28 20:54:50:353 CET] HTTPResponse.getContentText() [0 segundos]
[17-01-28 20:54:50:437 CET] UrlFetchApp.fetch([https://publish.twitter.com/oembed?url=https://twitter.com/Interior/status/787913605530411008]) [0,083 segundos]
[17-01-28 20:54:50:760 CET] Ejecución fallida: Missing required scope “https://www.googleapis.com/auth/gmail.settings.sharing” for modifying non-primary SendAs (línea 24, archivo “Código”) [0,63 segundos de tiempo de ejecución total]
Thanks
Sorry, Jorge. This only works for Gmail/Google Apps email addresses.
Hi Matt,
It’a all runnong rigth. Do tou know if there is any option to apply css style to text var?.
I’m try to apply around link rel but gmail don’t apply it.
Any idea where find it?.
Thanks for all.