module Bot class Responder include ActionView::Helpers::TextHelper include ActionView::Helpers::DateHelper class_attribute :respond_to_types attr_reader :message, :user, :responses, :handler def self.respond_to(*types) new_types = (respond_to_types || []).dup types.each { |type| new_types << type.to_s }.uniq self.respond_to_types = new_types.freeze end def initialize(message, user, responses, handler) @message = message @user = user @responses = responses @handler = handler end def can_respond_to_type?(type) valid_message_types = self.class.respond_to_types || ['text'] valid_message_types.include?(type) end def can_handle? !responses.count end def handle raise NotImplementedError end protected def text_response(message_text, suggested_responses=false) text_response = { 'type' => 'text', 'to' => message['from'], 'body' => message_text, 'typeTime' => 0 } text_response['suggestedResponses'] = suggested_responses if suggested_responses text_response end def photo_response(photo_url, suggested_responses=false) photo_response = { 'type' => 'gallery', 'to' => message['from'], 'picUrl' => photo_url } photo_response['suggestedResponses'] = suggested_responses if suggested_responses photo_response end def video_response(video_id, suggested_responses=false) video_response = { 'type' => 'video', 'to' => message['from'], 'videoId' => video_id, 'muted' => true, 'autoplay' => true } video_response['suggestedResponses'] = suggested_responses if suggested_responses video_response end def delay(message, delay) message['delay'] = delay message end def match_message(match) if match.is_a?(Regexp) !!(message['body'] =~ match) else message['body'].upcase == match.upcase end end def no_responses? responses.empty? end def reexecute_with(current_response, new_message=false) handler.execute_chain(new_message || message, Array.wrap(current_response), user) end def scan_data JSON.parse(message['data']) end end end