class Rubotnik::PostbackDispatch

Routing for postbacks

Attributes

postback[R]
user[R]

Public Class Methods

new(postback) click to toggle source
# File lib/rubotnik/postback_dispatch.rb, line 12
def initialize(postback)
  @postback = postback
  p @postback.class
  p @postback
  @user = Rubotnik::UserStore.instance.find_or_create_user(@postback.sender['id'])
end

Public Instance Methods

route(&block) click to toggle source
# File lib/rubotnik/postback_dispatch.rb, line 19
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 Instance Methods

bind(regex_string, to: nil, reply_with: {}) { || ... } click to toggle source
# File lib/rubotnik/postback_dispatch.rb, line 30
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
clear_user_state() click to toggle source
# File lib/rubotnik/postback_dispatch.rb, line 55
def clear_user_state
  @user.reset_command # Stop any current interaction
  @user.session = {} # Reset whatever you stored in the user
end
execute(command) click to toggle source
# File lib/rubotnik/postback_dispatch.rb, line 60
def execute(command)
  method(command).call
end
handle_command(to, reply_with) click to toggle source
# File lib/rubotnik/postback_dispatch.rb, line 42
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