Skip to content

Instantly share code, notes, and snippets.

@bng44270
Last active April 10, 2023 12:13
Show Gist options
  • Save bng44270/d282f5a776b59486f98d2de60f2725d4 to your computer and use it in GitHub Desktop.
Save bng44270/d282f5a776b59486f98d2de60f2725d4 to your computer and use it in GitHub Desktop.
Call IFTTT Webhooks from Google Scripts
/*
GsIFTTT
Call IFTTT Webhooks from Google Scripts
Setup:
Add a Script Property to your project named "ifttt_key" to store your IFTTT API Key
Usage:
var ifttt = new GsIFTTT();
ifttt.setConfig('event-name','value1','value2','value3'); //value2 and value3 are optional
ifttt.call(function() {
//Run this code if call is successful
},function() {
//Run this code if call fails
});
*/
class GsIFTTT {
constructor(key) {
this.key = ScriptProperties.getProperty('ifttt_key');
this.config = null;
}
setConfig(event,v1,v2,v3) {
if (event && v1) {
var payload = {};
payload['value1'] = v1;
if (v2) payload['value2'] = v2;
if (v2) payload['value3'] = v3;
this.config = {
event : event,
payload : JSON.stringify(payload)
};
}
else {
return false;
}
}
call(success,error) {
if (this.config) {
var reqOptions = {
'method' : 'post',
'contentType' : 'application/json',
'payload' : this.config.payload
};
var requestUrl = ('https://maker.ifttt.com/trigger/' + this.config.event + '/with/key/' + this.key).toString();
var req = UrlFetchApp.fetch(requestUrl,reqOptions);
if (req.getResponseCode() == 200) {
console.log(req.getContentText());
if (success) success();
}
else {
console.log(req.getContentText());
if (error) error();
}
}
else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment