オッサンホイホイなEmojiを出力する Slash Command をGASで作成

## <i class='fa fa-gamepad'></i> お題
SlackのSlashコマンドで、昔懐かしいゲーム風のEmojiをランダムに生成します。
Webサービス側は Google Apps Script で実装します。

Slackの外部Integrationの無料枠と
Google Apps Scriptで実現可能なお手軽構成です。

## <i class='fa fa-chrome'></i> Google Apps Script の実装
### <i class='fa fa-book'></i> ライブラリを追加
#### <i class='fa fa-plus-square'></i> Underscore.js を追加
`M3i7wmUA_5n0NSEaa6NnNqOBao7QLBR4j` でライブラリを検索し、Underscore を追加します

![gas_add_library.png](https://qiita-image-store.s3.amazonaws.com/0/43480/6022cb51-78a2-21eb-0bc3-6660766dd0fd.png)

### <i class='fa fa-code'></i> コード
```javascripot
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:"]);
}
```

### <i class='fa fa-globe'></i> 公開設定
Slack の Slash コマンドと Google Apps Script の連携設定については以下を参照ください

[SlackのSlash Commands+Google Apps Scriptで無料で無料ボット生活 - Tbpgr Blog](http://tbpgr.hatenablog.com/entry/2016/11/25/234846)

## <i class='fa fa-flask'></i> デモ
`/ossan_hoihoi` コマンドで実行可能です

![ossan_hoihoi.gif](https://qiita-image-store.s3.amazonaws.com/0/43480/ca6e6df2-c403-c8b3-5faa-e1bdb69e42ef.gif)

## <i class="fa fa-comments-o" style="font-size:1em;"></i> 雑感
ホイホイできたのだろうか？

## <i class='fa fa-book'></i> 関連資料
* [GASでSpreadsheetの範囲取得時のTips](http://hhmmm.hateblo.jp/entry/2016/04/11/181203)
* [simula-innovation/gas-underscore - GitHub](https://github.com/simula-innovation/gas-underscore)
* [Underscore.js](http://underscorejs.org/#shuffle)

## Advent Calendar
明日は **絵を描くWebデザイナーの湊川あい( @llminatoll )** さんです！