lita で slack attachment を使う


はじめに

lita-slack 1.6.0 以上で slack attachments を使うことが出来る

https://github.com/litaio/lita-slack/releases/tag/v1.6.0
https://api.slack.com/docs/attachments

方法

今回は lita-echo handler を作った
https://github.com/mayok/lita-echo

echo.rb
module Lita
  module Handlers
    class Echo < Handler
      route(/^echo\s+(.+)/, :echo, help: {"echo TEXT" => "Echoes back TEXT"})

      def echo(response)
        text = response.matches[0][0]
        target = response.room
        attachment = Lita::Adapters::Slack::Attachment.new(
          text, {
            color: "#36a64f",
            pretext: "Optional text that appears above the attachment block",
            author_name: "Bobby Tables",
            title: "Slack API Documentation",
            title_link: "https://api.slack.com/",
            text: text,
            fields: [
              {
                title: "Priority",
                value: "High",
                short: true
              },
              {
                title: "Environment",
                value: "production",
                short: true
              }
            ]
          }
        )

        robot.chat_service.send_attachment(target, attachment)
      end

      Lita.register_handler(self)
    end
  end
end

まとめ

response.reply ではなく,
robot.chat_service.send_attachment(target, attachment) を使うと出来る

雑感

text = response.matches[0][0] てかっこ悪いのでなんとかしたい