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.
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.
- 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.
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
getLatestTweetfunction 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
setSignaturefunction 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
setSignaturefunction under Resources–>Current project’s triggers.
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.