Module: Rubotnik::Helpers

Included in:
MessageDispatch, PostbackDispatch
Defined in:
lib/rubotnik/helpers.rb

Constant Summary collapse

GRAPH_URL =
'https://graph.facebook.com/v2.8/'.freeze

Class Method Summary collapse

Class Method Details

.call_graph_api(url) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubotnik/helpers.rb', line 58

def call_graph_api(url)
  @message.typing_on
  response = HTTParty.get(url)
  @message.typing_off
  case response.code
  when 200
    puts "User data received from Graph API: #{response.body}" # logging
    return JSON.parse(response.body, symbolize_names: true)
  else
    return false
  end
end

.get_user_info(*fields) ⇒ Object

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubotnik/helpers.rb', line 46

def (*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
    puts "Couldn't access URL" # logging
    return false
  end
end

.message_contains_location?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubotnik/helpers.rb', line 40

def message_contains_location?
  @message.attachments && @message.attachments.first['type'] == 'location'
end

.next_command(command) ⇒ Object



28
29
30
# File 'lib/rubotnik/helpers.rb', line 28

def next_command(command)
  @user.assign_command(command)
end

.say(text = 'What was I talking about?', quick_replies: [], user: @user) ⇒ Object

abstraction over Bot.deliver to send messages declaratively and directly



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubotnik/helpers.rb', line 12

def say(text = 'What was I talking about?', 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
  Bot.deliver(message_options, access_token: ENV['ACCESS_TOKEN'])
end

.show(ui_element, user: @user) ⇒ Object



24
25
26
# File 'lib/rubotnik/helpers.rb', line 24

def show(ui_element, user: @user)
  ui_element.send(user)
end

.stop_threadObject



32
33
34
# File 'lib/rubotnik/helpers.rb', line 32

def stop_thread
  @user.reset_command
end

.text_message?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rubotnik/helpers.rb', line 36

def text_message?
  @message.respond_to?(:text) && !@message.text.nil?
end