Last active
December 14, 2019 06:54
-
-
Save mitolog/a2d2a9bef995bfbc315cd88fe0bec96d to your computer and use it in GitHub Desktop.
Translation bot for slack by using gas (LanguageApp)
This file contains 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
// Originally taken from this article | |
// https://qiita.com/hotpepsi/items/3862618b38b463d37b53 | |
var TOKEN = "xoxp-YOUR_TOKEN_HERE"; | |
function doPost(e) { | |
try { | |
var json = JSON.parse(e.postData.getDataAsString()); | |
if (json.type == "url_verification") { | |
return ContentService.createTextOutput(json.challenge); | |
} | |
// https://api.slack.com/events/reaction_added | |
// scope: "reactions:read" | |
if (json.type == "event_callback" && json.event.type == "reaction_added") { | |
return ContentService.createTextOutput(onReactionAdded(json.event)); | |
} | |
} catch (ex) { | |
} | |
} | |
function postDebugMessage(json) { | |
channel = "#__debug"; | |
text = JSON.stringify(json); | |
UrlFetchApp.fetch("https://slack.com/api/chat.postMessage?token=" + TOKEN + "&channel=" + encodeURIComponent(channel) + "&text=" + encodeURIComponent(text)); | |
} | |
// https://api.slack.com/methods/chat.postMessage | |
// scope: "chat:write:user" or "chat:write:bot" | |
function postThreadMessage(channel, ts, text) { | |
if (channel && ts && text) { | |
UrlFetchApp.fetch("https://slack.com/api/chat.postMessage?token=" + TOKEN + "&channel=" + channel + "&thread_ts=" + ts + "&text=" + encodeURIComponent(text)); | |
} | |
} | |
// https://api.slack.com/methods/conversations.replies | |
// "channels:history" or "groups:history" | |
function getMessages(channel, ts) { | |
var response = UrlFetchApp.fetch("https://slack.com/api/conversations.replies?token=" + TOKEN + "&channel=" + channel + "&ts=" + ts); | |
var json = JSON.parse(response.getContentText()); | |
return json.messages; | |
} | |
function isTranslated(messages, lang) { | |
for (var i in messages) { | |
var message = messages[i]; | |
if (message.text.substring(0, lang.length) == lang) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function onReactionAdded(json) { | |
// postDebugMessage(json); | |
var channel = json.item.channel; | |
var type = json.item.type; | |
var ts = json.item.ts; | |
var reaction = json.reaction; | |
if (type == "message") { | |
var messages = getMessages(channel, ts); | |
// postDebugMessage(messages); | |
if (messages) { | |
var message = messages[0].text; | |
var languagePrefix; | |
var translateTo; | |
if ((reaction == "us" || reaction == "flag-us") && !isTranslated(messages, ":us:")) { | |
languagePrefix = ":us:"; | |
translateTo = "en"; | |
} | |
if ((reaction == "vn" || reaction == "flag-vn") && !isTranslated(messages, ":vn:")) { | |
languagePrefix = ":vn:"; | |
translateTo = "vi"; | |
} | |
if ((reaction == "jp" || reaction == "flag-jp") && !isTranslated(messages, ":jp:")) { | |
languagePrefix = ":jp:"; | |
translateTo = "ja"; | |
} | |
if (translateTo) { | |
var translatedMessage = languagePrefix + " " + LanguageApp.translate(message, "", translateTo); | |
postThreadMessage(channel, ts, translatedMessage); | |
} | |
} | |
} | |
return "OK"; | |
} | |
function checkVi() { | |
var res = LanguageApp.translate("OK. I'm checking in now. I'll do it after I calm down.", "", "vi"); | |
Logger.log(res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment