module Rubotnik::Helpers

Constants

GRAPH_URL

Mixed-in methods become private

Public Instance Methods

call_graph_api(url) click to toggle source
# File lib/rubotnik/helpers.rb, line 60
def call_graph_api(url)
  @message.typing_on
  response = HTTParty.get(url)
  @message.typing_off
  case response.code
  when 200
    Rubotnik.logger.info "Data received from Graph API: #{response.body}" # logging
    return JSON.parse(response.body, symbolize_names: true)
  else
    Rubotnik.logger.info "Request failed: #{response.body}"
    return false
  end
end
get_user_info(*fields) click to toggle source

Get user info from Graph API. Takes names of required fields as symbols developers.facebook.com/docs/graph-api/reference/v2.2/user

# File lib/rubotnik/helpers.rb, line 49
def get_user_info(*fields)
  str_fields = fields.map(&:to_s).join(',')
  url = GRAPH_URL + @user.id + '?fields=' + str_fields + '&access_token=' +
        ENV['ACCESS_TOKEN']
  begin
    return call_graph_api(url)
  rescue => e
    return false
  end
end
message_contains_location?() click to toggle source
# File lib/rubotnik/helpers.rb, line 43
def message_contains_location?
  !@message.attachments.nil? && @message.attachments.first['type'] == 'location'
end
next_command(command) click to toggle source
# File lib/rubotnik/helpers.rb, line 31
def next_command(command)
  @user.assign_command(command)
end
say(text, quick_replies: [], user: @user) click to toggle source

abstraction over Bot.deliver to send messages declaratively and directly

# File lib/rubotnik/helpers.rb, line 12
def say(text, quick_replies: [], user: @user)
  message_options = {
    recipient: { id: user.id },
    message: { text: text }
  }
  if quick_replies && !quick_replies.empty?
    message_options[:message][:quick_replies] = UI::QuickReplies
                                                  .build(*quick_replies)
  end

  send_message(message_options)
end
send_message(payload) click to toggle source
# File lib/rubotnik/helpers.rb, line 74
def send_message(payload)
  Bot.deliver(payload, access_token: ENV['ACCESS_TOKEN'])
end
show(ui_element, user: @user) click to toggle source
# File lib/rubotnik/helpers.rb, line 25
def show(ui_element, user: @user)
  payload = ui_element.build(user)

  send_message(payload)
end
stop_thread() click to toggle source
# File lib/rubotnik/helpers.rb, line 35
def stop_thread
  @user.reset_command
end
text_message?() click to toggle source
# File lib/rubotnik/helpers.rb, line 39
def text_message?
  @message.respond_to?(:text) && !@message.text.nil?
end