Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Forms経由で匿名でSlackに投稿するGoogle Apps Script
/*
Google Forms経由で匿名でSlackに投稿するGoogle Apps Script
*/
var config = {
slack: {
token: 'xoxp-XXXXXXXXXXX-XXXXXXXXX...',
iconEmoji: ':mona:',
},
channel: '#channel',
};
function formPost(e) {
try {
var res = e.response.getItemResponses();
var name = res[0].getResponse();
var text = res[1].getResponse();
var isFujiwara = res[2] !== undefined;
if (isFujiwara) {
if (name === undefined || name === '') name = '名無しの藤原竜也さん';
text = fujiwarify(text);
} else {
if (name === undefined || name === '') name = '名無しの大二郎さん';
}
postFileToSlack(config.channel, makeHandle(name), text);
e.source.deleteAllResponses();
} catch (e) {
Loggerr.log(e);
}
}
function stringToArray (str) {
// from https://qiita.com/YusukeHirao/items/2f0fb8d5bbb981101be0
return str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) || [];
}
function fujiwarify(s) {
s = s.replace(/(ください|下さい|ます|です|たい|う|た|よ)([。!!]|$)/g, function (s, a, b) {
if (a === 'よ') {
return a + 'ぉぉぉぉ' + b;
} else {
return a + 'よぉぉぉぉ' + b;
}
});
var chars = stringToArray(s);
var result = '';
chars.forEach(function (c) {
result += c + '゛';
});
return result;
}
function makeHandle (nickname) {
return nickname.replace(/◆/g, '◇')
.replace(/(#[\x20-\x7e]{1,8})$/, function (s, a) {
var salt = 'long long text here.'
var sha1 = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, salt + a);
var sha1text = Utilities.base64Encode(sha1);
return '◆' + sha1text.substr(0,10);
});
}
function postFileToSlack(channel, botName, title, content) {
var escape = function(s){
if (s === undefined || s === null) s = "";
return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
};
var url = 'https://slack.com/api/chat.postMessage';
var payload = {
token : config.slack.token,
channel : channel,
text: title,
username : botName,
attachments: [{
text: '```' + content + '```',
mrkdwn_in: 'text'
}],
icon_emoji: config.slack.iconEmoji
};
if( payload.channel.charAt(0) !== "#" && payload.channel.charAt(0) !== "@" ) payload.channel = "#" + payload.channel;
if (content === undefined) delete payload.attachments;
else payload.attachments = JSON.stringify(payload.attachments);
var res = UrlFetchApp.fetch(url, { method : 'post', payload : payload });
if (res.getResponseCode() === 200) {
var json = JSON.parse(res.getContentText());
if (json.ok === true) {
return true;
} else {
Logger.log(json);
return false;
}
} else {
Logger.log('Status=' + res.getResponseCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.