Sha256: ab4de07e22ff624d961ad98550f67f90341e9a5ade035d79b6a9996a54d43e93

Contents?: true

Size: 1.63 KB

Versions: 17

Compression:

Stored size: 1.63 KB

Contents

module Bobot
  module Event
    class Message
      include Bobot::Event::Common

      ATTACHMENT_TYPES = %w[image audio video file location fallback].freeze

      def id
        @messaging['message']['mid']
      end

      def seq
        @messaging['message']['seq']
      end

      def text
        @messaging['message']['text']
      end

      def echo?
        @messaging['message']['is_echo']
      end

      def attachments
        @messaging['message']['attachments']
      end

      def nlp
        @messaging['message']['nlp']
      end

      def app_id
        @messaging['message']['app_id']
      end

      ATTACHMENT_TYPES.each do |attachment_type|
        define_method "#{attachment_type}_attachment?" do
          attachment_type?(attachment_type)
        end
      end

      def attachment_type
        return if attachments.nil?

        attachments.first['type']
      end

      def attachment_url
        return if attachments.nil?
        return unless %w[image audio video file].include? attachment_type

        attachments.first['payload']['url']
      end

      def location_coordinates
        return [] unless attachment_type?('location')

        coordinates_data = attachments.first['payload']['coordinates']
        [coordinates_data['lat'], coordinates_data['long']]
      end

      def quick_reply
        return unless @messaging['message']['quick_reply']

        @messaging['message']['quick_reply']['payload']
      end
      alias_method :payload, :quick_reply

    private

      def attachment_type?(attachment_type)
        !attachments.nil? && attachments.first['type'] == attachment_type
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
bobot-3.6.3 lib/bobot/events/message.rb
bobot-3.6.0 lib/bobot/events/message.rb
bobot-3.5.2 lib/bobot/events/message.rb
bobot-3.5.1 lib/bobot/events/message.rb
bobot-3.5.0 lib/bobot/events/message.rb
bobot-3.0.7 lib/bobot/events/message.rb
bobot-3.0.6 lib/bobot/events/message.rb
bobot-3.0.5 lib/bobot/events/message.rb
bobot-3.0.3 lib/bobot/events/message.rb
bobot-3.0.2 lib/bobot/events/message.rb
bobot-3.0.1 lib/bobot/events/message.rb
bobot-2.6.2 lib/bobot/events/message.rb
bobot-2.6.1 lib/bobot/events/message.rb
bobot-2.6.0 lib/bobot/events/message.rb
bobot-2.5.0 lib/bobot/events/message.rb
bobot-2.3.0 lib/bobot/events/message.rb
bobot-2.1.0 lib/bobot/events/message.rb