How to create a Medium post using Zapier


var mediumIntegrationToken = "REPLACE_WITH_YOUR_MEDIUM_INTEGRATION_TOKEN";
// That's it. No need to edit anything else below.
// split the comma-separated tags string into an array then remove any whitespace
var tagsArray = input.medium_tags.split(',');
for (var i = 0; i < tagsArray.length; i++) {
tagsArray[i] = tagsArray[i].trim();
}
var data = JSON.stringify({
"title": input.medium_title,
"contentFormat": "html",
"content": input.medium_content,
"canonicalUrl": input.medium_canonical_url,
"tags": tagsArray
});
// Look up the author id
fetch('https://api.medium.com/v1/me&#39;, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + mediumIntegrationToken
}
})
.then(function(res) {
return res.json();
})
.then(function(body) {
return body.data.id;
})
.then(function(authorId) {
// Now create the medium post
var apiUrl = 'https://api.medium.com/v1/users/&#39; + authorId + '/posts';
fetch(apiUrl, {
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + mediumIntegrationToken
}
})
.then(function(res) {
return res.json();
})
.then(function(body) {
var output = body.data;
callback(null, output);
})
.catch(callback);
});

How to add GitHub commits to FreshBooks time tracking using Zapier


/*
This code assumes that you have included a "freshbooksTimeEntryObject" somewhere in a previous Zap step. You can see the format of a "freshbooksTimeEntryObject" below.
freshbooksTimeEntryObject = {
"project_id": "25479",
"task_id": "302086",
"staff_id": "1",
"hours": "4",
"notes": "This is a note",
"date": "2016-01-31"
}
*/
function extract_piece_of_data(entire_string_of_text, characters_before_desired_data, characters_after_desired_data) {
var desired_data_with_stuff_after_it = entire_string_of_text.substr(entire_string_of_text.indexOf(characters_before_desired_data) + characters_before_desired_data.length);
var desired_data = desired_data_with_stuff_after_it.substr(0, desired_data_with_stuff_after_it.indexOf(characters_after_desired_data));
return desired_data;
}
var freshbooksTimeEntryObject = JSON.parse(extract_piece_of_data(input.commit_message, "freshbooksTimeEntryObject = ", "}") + "}");
output = {project_id: freshbooksTimeEntryObject.project_id, task_id: freshbooksTimeEntryObject.task_id, staff_id: freshbooksTimeEntryObject.staff_id, hours: freshbooksTimeEntryObject.hours, notes: freshbooksTimeEntryObject.notes, date: freshbooksTimeEntryObject.date};

How to Like a Tweet in Zapier using the “Code” app


// This code requires that you set the ID of the tweet that you want to Like as an input variable called "tweet_id". Learn more at https://zapier.com/help/code/#data-variables
// INSTUCTIONS FOR SETTING THESE REQUIRED VARIABLES: After you create a new app at https://apps.twitter.com/, click on the name of the app to open it. Then, select the "Keys and Access Tokens" tab.
var twitterApplicationConsumerKey = ''; // Located in the "Application Settings" section
var twitterApplicationConsumerSecret = ''; // Located in the "Application Settings" section
var twitterApplicationAccessToken = ''; // Located in the "Your Access Token" section
var twitterApplicationAccessTokenSecret = ''; // Located in the "Your Access Token" section
// That's it. No need to edit anything below.
/* OAuthSimple
* A simpler version of OAuth
*
* author: jr conlin
* mail: src@anticipatr.com
* copyright: unitedHeroes.net
* version: 1.0
* url: http://unitedHeroes.net/OAuthSimple
*
* Copyright (c) 2009, unitedHeroes.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the unitedHeroes.net nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY UNITEDHEROES.NET ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UNITEDHEROES.NET BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Computes a HMAC-SHA1 code.
*
* @param {string} k Secret key.
* @param {string} d Data to be hashed.
* @return {string} The hashed string.
*/
function b64_hmac_sha1(k,d,_p,_z){
if(!_p){_p='=';}if(!_z){_z=8;}function _f(t,b,c,d){if(t<20){return(b&c)|((~b)&d);}if(t<40){return b^c^d;}if(t<60){return(b&c)|(b&d)|(c&d);}return b^c^d;}function _k(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514;}function _s(x,y){var l=(x&0xFFFF)+(y&0xFFFF),m=(x>>16)+(y>>16)+(l>>16);return(m<<16)|(l&0xFFFF);}function _r(n,c){return(n<<c)|(n>>>(32-c));}function _c(x,l){x[l>>5]|=0x80<<(24-l%32);x[((l+64>>9)<<4)+15]=l;var w=[80],a=1732584193,b=-271733879,c=-1732584194,d=271733878,e=-1009589776;for(var i=0;i<x.length;i+=16){var o=a,p=b,q=c,r=d,s=e;for(var j=0;j<80;j++){if(j<16){w[j]=x[i+j];}else{w[j]=_r(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);}var t=_s(_s(_r(a,5),_f(j,b,c,d)),_s(_s(e,w[j]),_k(j)));e=d;d=c;c=_r(b,30);b=a;a=t;}a=_s(a,o);b=_s(b,p);c=_s(c,q);d=_s(d,r);e=_s(e,s);}return[a,b,c,d,e];}function _b(s){var b=[],m=(1<<_z)-1;for(var i=0;i<s.length*_z;i+=_z){b[i>>5]|=(s.charCodeAt(i/8)&m)<<(32-_z-i%32);}return b;}function _h(k,d){var b=_b(k);if(b.length>16){b=_c(b,k.length*_z);}var p=[16],o=[16];for(var i=0;i<16;i++){p[i]=b[i]^0x36363636;o[i]=b[i]^0x5C5C5C5C;}var h=_c(p.concat(_b(d)),512+d.length*_z);return _c(o.concat(h),512+160);}function _n(b){var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",s='';for(var i=0;i<b.length*4;i+=3){var r=(((b[i>>2]>>8*(3-i%4))&0xFF)<<16)|(((b[i+1>>2]>>8*(3-(i+1)%4))&0xFF)<<8)|((b[i+2>>2]>>8*(3-(i+2)%4))&0xFF);for(var j=0;j<4;j++){if(i*8+j*6>b.length*32){s+=_p;}else{s+=t.charAt((r>>6*(3-j))&0x3F);}}}return s;}function _x(k,d){return _n(_h(k,d));}return _x(k,d);
}
var tweetId = input.tweet_id;
//create nonce
function generateRandomString(desiredLengthOfRandomString) {
var result = '';
var possibleCharactersForRandomString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < desiredLengthOfRandomString; i++ )
result += possibleCharactersForRandomString.charAt(Math.floor(Math.random() * possibleCharactersForRandomString.length));
return result;
}
var randomString = generateRandomString(32);
var nonce = new Buffer(randomString).toString('base64');
//create timestamp
var timestamp = Math.floor(new Date() / 1000);
//create the signature
var signatureParameterString = 'id=' + tweetId + '&oauth_consumer_key=' + twitterApplicationConsumerKey + '&oauth_nonce=' + encodeURIComponent(nonce) + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + timestamp + '&oauth_token=' + twitterApplicationAccessToken + '&oauth_version=1.0';
var signatureBaseString = 'POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Ffavorites%2Fcreate.json&' + encodeURIComponent(signatureParameterString);
var signingKey = encodeURIComponent(twitterApplicationConsumerSecret) + '&' + encodeURIComponent(twitterApplicationAccessTokenSecret);
var signature = b64_hmac_sha1(signingKey, signatureBaseString);
var apiUrl = 'https://api.twitter.com/1.1/favorites/create.json?id=&#39; + tweetId;
var oauthString = 'OAuth oauth_consumer_key="' + twitterApplicationConsumerKey + '", oauth_nonce="' + encodeURIComponent(nonce) + '", oauth_signature="' + encodeURIComponent(signature) + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + timestamp + '", oauth_token="' + twitterApplicationAccessToken + '", oauth_version="1.0"';
fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': oauthString
}
})
.then(function(res) {
return res.json();
})
.then(function(body) {
var output = body;
callback(null, output);
})
.catch(callback);

How to use SHA-1 in Zapier’s “Code” app


function sha1 (str) {
// discuss at: http://phpjs.org/functions/sha1/
// original by: Webtoolkit.info (http://www.webtoolkit.info/)
// improved by: Michael White (http://getsprink.com)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// input by: Brett Zamir (http://brett-zamir.me)
// example 1: sha1('Kevin van Zonneveld');
// returns 1: '54916d2e62f65b3afa6e192e6a601cdbe5cb5897'
var rotate_left = function (n, s) {
var t4 = (n << s) | (n >>> (32 – s))
return t4
}
/* var lsb_hex = function (val) {
// Not in use; needed?
var str="";
var i;
var vh;
var vl;
for ( i=0; i<=6; i+=2 ) {
vh = (val>>>(i*4+4))&0x0f;
vl = (val>>>(i*4))&0x0f;
str += vh.toString(16) + vl.toString(16);
}
return str;
};*/
var cvt_hex = function (val) {
var str = ''
var i
var v
for (i = 7; i >= 0; i–) {
v = (val >>> (i * 4)) & 0x0f
str += v.toString(16)
}
return str
}
var blockstart
var i, j
var W = new Array(80)
var H0 = 0x67452301
var H1 = 0xEFCDAB89
var H2 = 0x98BADCFE
var H3 = 0x10325476
var H4 = 0xC3D2E1F0
var A, B, C, D, E
var temp
// utf8_encode
str = unescape(encodeURIComponent(str))
var str_len = str.length
var word_array = []
for (i = 0; i < str_len – 3; i += 4) {
j = str.charCodeAt(i) << 24 | str.charCodeAt(i + 1) << 16 | str.charCodeAt(i + 2) << 8 | str.charCodeAt(i + 3)
word_array.push(j)
}
switch (str_len % 4) {
case 0:
i = 0x080000000
break
case 1:
i = str.charCodeAt(str_len – 1) << 24 | 0x0800000
break
case 2:
i = str.charCodeAt(str_len – 2) << 24 | str.charCodeAt(str_len – 1) << 16 | 0x08000
break
case 3:
i = str.charCodeAt(str_len – 3) << 24 | str.charCodeAt(str_len – 2) << 16 | str.charCodeAt(str_len – 1) <<
8 | 0x80
break
}
word_array.push(i)
while ((word_array.length % 16) != 14) {
word_array.push(0)
}
word_array.push(str_len >>> 29)
word_array.push((str_len << 3) & 0x0ffffffff)
for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
for (i = 0; i < 16; i++) {
W[i] = word_array[blockstart + i]
}
for (i = 16; i <= 79; i++) {
W[i] = rotate_left(W[i – 3] ^ W[i – 8] ^ W[i – 14] ^ W[i – 16], 1)
}
A = H0
B = H1
C = H2
D = H3
E = H4
for (i = 0; i <= 19; i++) {
temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff
E = D
D = C
C = rotate_left(B, 30)
B = A
A = temp
}
for (i = 20; i <= 39; i++) {
temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff
E = D
D = C
C = rotate_left(B, 30)
B = A
A = temp
}
for (i = 40; i <= 59; i++) {
temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff
E = D
D = C
C = rotate_left(B, 30)
B = A
A = temp
}
for (i = 60; i <= 79; i++) {
temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff
E = D
D = C
C = rotate_left(B, 30)
B = A
A = temp
}
H0 = (H0 + A) & 0x0ffffffff
H1 = (H1 + B) & 0x0ffffffff
H2 = (H2 + C) & 0x0ffffffff
H3 = (H3 + D) & 0x0ffffffff
H4 = (H4 + E) & 0x0ffffffff
}
temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4)
return temp.toLowerCase()
}
var string = "This string is about to be encoded with sha1";
var encodedString = sha1(string);

How to generate a random string or ID in Zapier’s “Code” app

Sometimes, you might find it useful to generate a random string of letters, numbers, or a combination of both. I found myself using this function when I needed to create a nonce when I was authorizing a Twitter API request inside of Zapier’s Code app.


// adapted from http://stackoverflow.com/a/1349426/5573841
function generateRandomString(desiredLengthOfRandomString) {
var result = '';
var possibleCharactersForRandomString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < desiredLengthOfRandomString; i++ )
result += possibleCharactersForRandomString.charAt(Math.floor(Math.random() * possibleCharactersForRandomString.length));
return result;
}
var randomString = generateRandomString(32);
var output = {'randomString': randomString};

How to use Zapier to post Dribbble Likes to Twitter and include Author’s Twitter handle if available


var twitterHandle = "";
var bestAvailableImage = "";
if(input.user_twitter_link){
var indexOfLastSlash = input.user_twitter_link.lastIndexOf('/');
twitterHandle = input.user_twitter_link.substring(indexOfLastSlash + 1);
twitterHandle = '(@' + twitterHandle + ')';
}
if(input.hidpi_image){
bestAvailableImage = input.hidpi_image;
}
else{
bestAvailableImage = input.normal_image;
}
var output = {twitter_handle: twitterHandle, best_available_image: bestAvailableImage};