Last active
April 10, 2023 12:13
-
-
Save bng44270/d282f5a776b59486f98d2de60f2725d4 to your computer and use it in GitHub Desktop.
Call IFTTT Webhooks from Google Scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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