slack-ruby / slack-ruby-bot

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

Extract attachments text #262

Open dblock opened 4 years ago

dblock commented 4 years ago

IFTT messages are attachments, use fallback text or extract text from those.

 #<Slack::Messages::Message attachments=#<Hashie::Array [#<Slack::Messages::Message fallback="MSFT" mrkdwn_in=#<Hashie::Array ["text", "pretext"]> pretext="MSFT" title="https://ifttt.com/share/XnSQgQ2fJZ24" title_link="https://ifttt.com/share/XnSQgQ2fJZ24">]> bot_id="B01641RK3SR" bot_profile=#<Slack::Messages::Message app_id="A028LGAFF" deleted=false icons=#<Slack::Messages::Message image_36="https://avatars.slack-edge.com/2020-03-31/1037355424996_31b25e6141973a9d613b_36.jpg" image_48="https://avatars.slack-edge.com/2020-03-31/1037355424996_31b25e6141973a9d613b_48.jpg" image_72="https://avatars.slack-edge.com/2020-03-31/1037355424996_31b25e6141973a9d613b_72.jpg"> id="B01641RK3SR" name="IFTTT" team_id="T04KB5WQH" updated=1593219506> channel="C0LAW5Z1U" event_ts="1593220314.001400" icons=#<Slack::Messages::Message image_36="https://a.slack-edge.com/095d8b/img/services/ifttt_36.png" image_48="https://a.slack-edge.com/095d8b/img/services/ifttt_48.png" image_72="https://a.slack-edge.com/095d8b/img/services/ifttt_72.png"> subtype="bot_message" suppress_notification=false team="T04KB5WQH" text="" ts="1593220314.001400" type="message" username="IFTTT">

Workaround:

module SlackRubyBot
  module Hooks
    class Message
      alias_method :_call, :call

      def call(client, data)
        data.text = data.attachments&.map(&:fallback)&.join("\n") if data.text.blank?
        _call(client, data)
      end
    end
  end
end