slack-ruby / slack-ruby-bot

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

Adding attachments to the message. #239

Closed TheTharin closed 4 years ago

TheTharin commented 4 years ago

Hi there! Wanted to ask for a propper way to add attachments or blocks to the client.say command. I tried plain hashes, JSON unencoded strings, URL-encoded JSON strings, - nothing works for me. Could you please provide an example? UPD: okay, I see that Slack RTM Api doesn't support message formatting currently, only ordinary WEB API. What are your suggestions then? Should I just use plain slack-ruby-client with my Rails app for that?

dblock commented 4 years ago

You can't, see https://api.slack.com/rtm.

_The RTM API only supports posting simple messages formatted using our default message formatting mode. It does not support attachments or other message formatting modes. To post a more complex message as a user clients can call the chat.postMessage Web API method with asuser set to true.

dblock commented 4 years ago

Use the web client that was used to obtain the RTM client and send messages with chat_postMessage, https://github.com/slack-ruby/slack-ruby-client/blob/master/lib/slack/real_time/client.rb#L19.

Example: https://github.com/dblock/slack-market/blob/bf9c29b6190249c3bf8741f6c49c62dbb259e552/slack-market/commands/sucks.rb#L12