Last active
May 6, 2016 12:21
-
-
Save yucao24hours/f103b494a236008a6fe79939b28449b4 to your computer and use it in GitHub Desktop.
Shiritorichan checks if the Shiritori word has been used in Slack channel.
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
| var token = "<your_slack_token>"; | |
| var shiritori_channel = "<your_channel_id>"; | |
| var outgoing_token = "<your_outgoing_webhook_token>"; | |
| var bot_name = "shiritorichan"; | |
| function doPost(e) { | |
| // To use `SlackApp` class, you need to activate SlackApp Library in Google Apps Script menu. | |
| var app = SlackApp.create(token); | |
| // Ignore the message posted by slackapp | |
| if(e.parameter.user_name == "slackbot"){ | |
| return null; | |
| } | |
| shiritoriChan(app, e); | |
| } | |
| function shiritoriChan(app, e){ | |
| var histories = app.channelsHistory(shiritori_channel).messages; | |
| // Remove the last message posted by the user not to compare with itself | |
| histories.shift(); | |
| var param = e.parameter.text | |
| for each(var history in histories) { | |
| var history_text = history.text | |
| if (history_text === param) { | |
| var message = param + " is already used:no_good:"; | |
| var response = app.postMessage(shiritori_channel, message, { | |
| username: bot_name, | |
| icon_emoji: ":new_moon_with_face:" | |
| }); | |
| break; | |
| } | |
| } | |
| return true; | |
| } | |
| function doGet(e){ | |
| if (!e){ | |
| //for test | |
| e = { | |
| parameter : { | |
| token : outgoing_token, | |
| team_id : "T0001", | |
| channel_id : "C12345678910", | |
| channel_name : "test", | |
| timestamp : "1355517523.000005", | |
| user_id : "U2147483697", | |
| user_name : "Steve", | |
| trigger_word : "MyFirstBot:" | |
| } | |
| }; | |
| } | |
| if (outgoing_token != e.parameter.token) { | |
| throw new Error("invalid token."); | |
| } | |
| doPost(e); | |
| return null; | |
| } | |
| function init(){ | |
| var e = { | |
| parameter: { | |
| text: "This is init", | |
| user_name: "test", | |
| channel_id: shiritori_channel | |
| } | |
| }; | |
| doGet(e); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment