hubot+slack で Attachments を使おうとしてハマった話

An introduction to messages | Slack

ここに書かれている通り、slackにメッセージを投稿する時、Attachmentsという機能を使うとメッセージを装飾することができる。
で、それをhubotから利用しようとしてハマったというお話。

調べる中で「msg.sendではなくhubot.emitを使う」という記事を見つけたのでその通りにしてみたところ動かず。

さらに色々調べたところ、なんと最近仕様が変わったとのこと。おかげでネット上にある記事のほとんどが役に立たなくなってしまっていた。

最新のQAを漁ったりドキュメントを読み直したりしながらなんとか問題を解決。
以下がそのサンプルコード。

module.exports = (robot) ->
  robot.respond /HELLO/, (msg) ->
    data =
      attachments: JSON.stringify([{
        "fallback": "Required plain-text summary of the attachment.",
        "color": "#36a64f",
        "pretext": "Optional text that appears above the attachment block",
        "author_name": "Bobby Tables",
        "author_link": "http://flickr.com/bobby/",
        "author_icon": "http://flickr.com/icons/bobby.jpg",
        "title": "Slack API Documentation",
        "title_link": "https://api.slack.com/",
        "text": "Optional text that appears within the attachment",
        "image_url": "http://my-website.com/path/to/image.jpg",
        "thumb_url": "http://example.com/path/to/thumb.png",
        "footer": "Slack API",
        "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
        "ts": "123456789"
        }])
    msg.send data

結局 msg.send で問題なし。
JSON.stringify でJSON文字列に変換するのは以前と同じ。