slack-ruby / slack-ruby-bot

The easiest way to write a Slack bot in Ruby.
MIT License
1.12k stars 187 forks source link

Bot responds to text in attachments. #177

Closed hmiyado closed 6 years ago

hmiyado commented 6 years ago

I would like my bot to respond text in attachments. However, slack ruby bot can respond to only plain text now. Does anyone have good idea to implement this?

dblock commented 6 years ago

That probably means you need to be listening to file upload events, so I would start finding out what these are and writing some simple hook.

mdudzinski commented 6 years ago

@dblock Attachments can come with message events. For example Slack GitHub integration uses attachments for posting notifications. Example format:

{
  "text": "",
  "bot_id": "B9AL1G5DE",
  "attachments": [
    {
      "fallback": "[mdudzinski\/gh-notifications-test] New comment by mdudzinski on pull request <https:\/\/github.com\/mdudzinski\/gh-notifications-test\/pull\/1#issuecomment-367010472|#1: second commit>",
      "text": "foobar something",
      "pretext": "[mdudzinski\/gh-notifications-test] New comment by mdudzinski on pull request <https:\/\/github.com\/mdudzinski\/gh-notifications-test\/pull\/1#issuecomment-367010472|#1: second commit>",
      "id": 1,
      "color": "C4E8B4",
      "mrkdwn_in": [
        "text",
        "pretext"
      ]
    }
  ],
  "type": "message",
  "subtype": "bot_message",
  "team": "T09P9CDD3",
  "channel": "G8S920G5N",
  "event_ts": "1519139910.000648",
  "ts": "1519139910.000648"
}

So currently slack-ruby-bot cannot handle such messages. As a short term solution I came up with an idea of message hook decorator which just appends attachment.text to data.text and calls SlackRubyBot::Hooks::Message#call.

But I think this deserves a long term solution. Maybe a new DSL to listen for commands/matches on attachments text/pretext/title field? Or perhaps existing DSLs (command etc) should just check against attachments field as well.

dblock commented 6 years ago

+1 on a new DSL

mdudzinski commented 6 years ago

I think I can take this one. I'll see what can be done :)

mdudzinski commented 6 years ago

This issue is addressed in #180.

dblock commented 6 years ago

I just merged https://github.com/slack-ruby/slack-ruby-bot/pull/180, please give it a try from HEAD!

hmiyado commented 6 years ago

Thanks, this is a great update!