Skip to content

Instantly share code, notes, and snippets.

@yhay81
Last active March 24, 2018 11:22
Show Gist options
  • Save yhay81/ee6510070aacae551be398ca5dd4b2b7 to your computer and use it in GitHub Desktop.
Save yhay81/ee6510070aacae551be398ca5dd4b2b7 to your computer and use it in GitHub Desktop.
Slack bot who react some words and return a giphy.
var SLACK_ACCESS_TOKEN = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
var GIPHY_API_KEY = PropertiesService.getScriptProperties().getProperty('GIPHY_API_KEY');
// var SPREAD_SHEET_ID = PropertiesService.getScriptProperties().getProperty('SPREAD_SHEAT_ID');
var POST_MESSAGE_ENDPOINT = 'https://slack.com/api/chat.postMessage';
var TEXTS = [
'Do you like dogs?:dog:',
'You said the word!:grinning:',
'I hope you like this...:poodle:',
'Dogs are cute!:dog2: Dogs are cute!:dog2:',
'I know, you want this.:thumbsup:'
];
function doPost(e){
return ContentService.createTextOutput(JSON.parse(e.postData.contents).challenge);
var event = JSON.parse(e.postData.contents).event;
// log('POST event: ' + JSON.stringify(event));
if(event.hasOwnProperty('bot_id')){
return;
}else if(event.text.match(/dog/)){
postGiphy2Slack(event, ['cute', 'corgi', 'dog']);
}else if(event.text.match(/corgi/)){
postGiphy2Slack(event, ['cute', 'corgi']);
}else if(event.text.match(/butt/)){
postGiphy2Slack(event, ['cute', 'corgi', 'butt']);
}else if(event.text.match(/shake/)){
postGiphy2Slack(event, ['cute', 'corgi', 'shake']);
}else if(event.text.match(/shiba/)){
postGiphy2Slack(event, ['cute', 'shiba']);
}
}
function postGiphy2Slack(event, keywords){
var url = getGiphyUrl(keywords);
var random_params = Math.floor(Math.random() * TEXTS.length);
var payload = {token:SLACK_ACCESS_TOKEN, channel:event.channel, text:TEXTS[random_params] + '\n' + url};
UrlFetchApp.fetch(POST_MESSAGE_ENDPOINT, {method: 'post', payload:payload});
}
function getGiphyUrl(keywords){
var url = 'http://api.giphy.com/v1/gifs/search?q=' + keywords.join('%20') + '&api_key=' + GIPHY_API_KEY;
var response = JSON.parse(UrlFetchApp.fetch(url));
var random_params = Math.floor(Math.random() * response["data"].length);
return response["data"][random_params]["bitly_gif_url"];
}
// function log(text){
// var sheet = SpreadsheetApp.openById(SPREAD_SHEET_ID).getSheetByName('Sheet1');
// sheet.appendRow([new Date(), text]);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment