Sha256: 903afea92e8ab353bbd46f3775059693264dc4c674e33e8f017a07415b4e2ceb

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'rubotnik/helpers'
require 'rubotnik/commands'

module Rubotnik
  # Routing for postbacks
  class PostbackDispatch
    include Rubotnik::Helpers
    include Commands

    attr_reader :postback, :user

    def initialize(postback)
      @postback = postback
      p @postback.class
      p @postback
      @user = Rubotnik::UserStore.instance.find_or_create_user(@postback.sender['id'])
    end

    def route(&block)
      @matched = false
      instance_eval(&block)
    rescue StandardError => error
      raise unless ENV["DEBUG"] == "true"
      stop_thread
      say "There was an error: #{error}"
    end

    private

    def bind(regex_string, to: nil, reply_with: {})
      return unless @postback.payload == regex_string.upcase
      clear_user_state
      @matched = true
      Rubotnik.logger.info "Matched #{regex_string} to #{to.nil? ? 'block' : to}"
      if block_given?
        yield
        return
      end
      handle_command(to, reply_with)
    end

    def handle_command(to, reply_with)
      if reply_with.empty?
        execute(to)
        Rubotnik.logger.info "Command #{to} is executed for user #{@user.id}"
        @user.reset_command
        Rubotnik.logger.info "Command is reset for user #{@user.id}"
      else
        say(reply_with[:message], quick_replies: reply_with[:quick_replies])
        @user.assign_command(to)
        Rubotnik.logger.info "Command #{to} is set for user #{@user.id}"
      end
    end

    def clear_user_state
      @user.reset_command # Stop any current interaction
      @user.session = {} # Reset whatever you stored in the user
    end

    def execute(command)
      method(command).call
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubotnik-0.2.3 lib/rubotnik/postback_dispatch.rb
rubotnik-0.2.2 lib/rubotnik/postback_dispatch.rb