Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Forked from matthewrknoll/Code.gs
Last active November 17, 2019 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhawksey/a3450ae5ec0cb54582cb2de38b6345ba to your computer and use it in GitHub Desktop.
Save mhawksey/a3450ae5ec0cb54582cb2de38b6345ba to your computer and use it in GitHub Desktop.
Automatically include your latest favorited Tweet in your Gmail signature. More info https://knoll.tech/2017/01/24/automatically-include-your-latest-tweet-in-your-gmail-signature/
// Grabs my latest tweet favorited using the handy TwtrService wrapper by +MartinHawksey - https://goo.gl/2it7yb
function getLatestTweet() {
var data = TwtrService.get("statuses/user_timeline", {screen_name: /*"YourTwitterHandle(without the @)"*/, count: 1});
var id = false;
for (var i = 0; i < data.length; i++){
if (data[i].favorited) {
id = data[i].id_str;
break;
}
}
if (id) {
// Grabs embed code for my latest tweet. Would look much prettier if Gmail could render it properly.
var response = UrlFetchApp.fetch('https://publish.twitter.com/oembed?url=https://twitter.com/Interior/status/'+id);
var json = JSON.parse(response);
var text = json.html;
// Returns some custom HTML to be used as my Gmail signature.
var html = "<strong>Matt Knoll</strong><br>Technology Integration Specialist<br>Crystal Lake Central High School<br><br>\
<div><a href='https://edudirectory.withgoogle.com/en/profile/e26df0312f7c13cb2384e747141e2875'><img src='https://drive.google.com/uc?id=0B0DG-6PY52gfQUJ3Z2ZzOEdndDg'\
height='30' width='30' style='vertical-align: middle;'/></a> <a href='https://twitter.com/YourTwitterHandle'><img src='https://c1.staticflickr.com/9/8426/7749081714_9e35bdcdbd.jpg'\
width='20' height='20' style='vertical-align: middle;'/>\
<span style='vertical-align: middle;'></a><br><strong>Latest from <a href='https://twitter.com/YourTwitterHandle'>@knollmatt</a>:</strong></span></div>"+text;
return html;
} else {
return false;
}
};
// Sets my email signature to above HTML using the Gmail API. More on that here: https://goo.gl/R3pO11
function setSignture() {
var tweetHtml = getLatestTweet();
if (tweetHtml){
var data = {'signature': tweetHtml};
var rsp = Gmail.Users.Settings.SendAs.patch(data, 'me', /*'example@example.org'*/);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment