お題
SlackのSlashコマンドで、昔懐かしいゲーム風のEmojiをランダムに生成します。
Webサービス側は Google Apps Script で実装します。
Slackの外部Integrationの無料枠と
Google Apps Scriptで実現可能なお手軽構成です。
Google Apps Script の実装
ライブラリを追加
Underscore.js を追加
M3i7wmUA_5n0NSEaa6NnNqOBao7QLBR4j
でライブラリを検索し、Underscore を追加します
コード
function doGet(e) {
var _ = Underscore.load();
r = Math.random()
var game;
if (r > 0.5) {
game = bomb(_);
} else {
game = dokan(_);
}
var res = {"text": game};
return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
}
function bomb(_) {
var bombTemplate = "<%=parts[0]%>:black_large_square:<%=parts[1]%>:black_large_square:<%=parts[2]%>\n" +
"<%=parts[3]%><%=parts[4]%><%=parts[5]%><%=parts[6]%><%=parts[7]%>\n" +
"<%=parts[8]%>:black_large_square:<%=parts[9]%>:black_large_square:<%=parts[10]%>\n" +
"<%=parts[11]%><%=parts[12]%><%=parts[13]%><%=parts[14]%><%=parts[15]%>\n" +
"<%=parts[16]%>:black_large_square:<%=parts[17]%>:black_large_square:<%=parts[18]%>\n";
var parts = [":bomb:", ":fire:", ":skull:", ":hole:", ":ghost:", ":japanese_goblin:", ":moyai:", ":dragon:", ":athletic_shoe:"];
parts = _.sample(parts, 7);
parts.push(":walking:");
parts.push(":key:");
parts.push(":door:");
for (var i = 0;i<10;i++) {
parts.push(":grey_question:");
}
parts = _.shuffle(parts)
var t = _.template(bombTemplate);
var result = t({parts: parts});
return result;
}
function dokan(_) {
var dokanTemplate = ":cloud::cloud::cloud::cloud:<%=sunMoon%>:cloud::cloud:\n" +
":large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle:\n" +
":large_blue_circle::large_blue_circle:<%=onQuestionBlock%><%=onBlockEnemy1%>:large_blue_circle::large_blue_circle::large_blue_circle:\n" +
":large_blue_circle::black_large_square::question::black_large_square::large_blue_circle::large_blue_circle::japanese_castle:\n" +
":large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::black_large_square::black_large_square:\n" +
":runner::large_blue_circle::large_blue_circle:<%=onBlockEnemy2%>:black_large_square::black_large_square::black_large_square:\n" +
":black_large_square::large_blue_circle::large_blue_circle::black_large_square::black_large_square::black_large_square::black_large_square:\n"
var sunMoon = sunMoonSample(_);
var onQuestionBlock = onQuestionBlockSample(_);
var onBlockEnemy1 = onBlockEnemySample(_);
var onBlockEnemy2 = onBlockEnemySample(_);
var t = _.template(dokanTemplate);
var result = t({sunMoon: sunMoon, onQuestionBlock: onQuestionBlock, onBlockEnemy1: onBlockEnemy1, onBlockEnemy2: onBlockEnemy2});
return result;
}
function sunMoonSample(_) {
return _.sample([":sunny:", ":crescent_moon:"]);
}
function onQuestionBlockSample(_) {
return _.sample([":mushroom:", ":star2:", ":seedling:"]);
}
function onBlockEnemySample(_) {
return _.sample([":turtle:", ":blowfish:", ":fire:"]);
}
公開設定
Slack の Slash コマンドと Google Apps Script の連携設定については以下を参照ください
SlackのSlash Commands+Google Apps Scriptで無料で無料ボット生活 - Tbpgr Blog
デモ
/ossan_hoihoi
コマンドで実行可能です
雑感
ホイホイできたのだろうか?
関連資料
Advent Calendar
明日は 絵を描くWebデザイナーの湊川あい( @llminatoll ) さんです!