Module: Hg::Bot::ClassMethods

Defined in:
lib/hg/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#call_to_actionsObject

Returns the value of attribute call_to_actions.



14
15
16
# File 'lib/hg/bot.rb', line 14

def call_to_actions
  @call_to_actions
end

#chunksObject

Returns the value of attribute chunks.



12
13
14
# File 'lib/hg/bot.rb', line 12

def chunks
  @chunks
end

#default_chunkObject

Returns the value of attribute default_chunk.



13
14
15
# File 'lib/hg/bot.rb', line 13

def default_chunk
  @default_chunk
end

#get_started_contentObject

Returns the value of attribute get_started_content.



15
16
17
# File 'lib/hg/bot.rb', line 15

def get_started_content
  @get_started_content
end

#greeting_text(text) ⇒ Object

Returns the value of attribute greeting_text.



16
17
18
# File 'lib/hg/bot.rb', line 16

def greeting_text
  @greeting_text
end

#image_url_base_portionObject

Returns the value of attribute image_url_base_portion.



17
18
19
# File 'lib/hg/bot.rb', line 17

def image_url_base_portion
  @image_url_base_portion
end

#routesObject

Returns the value of attribute routes.



11
12
13
# File 'lib/hg/bot.rb', line 11

def routes
  @routes
end

Instance Method Details

#call_to_action(text, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hg/bot.rb', line 42

def call_to_action(text, options = {})
  call_to_action_content = {
    title: text
  }

  if options[:to]
    call_to_action_content[:type] = 'postback'
    call_to_action_content[:payload] = options[:to].to_s
  elsif options[:url]
    call_to_action_content[:type] = 'web_url'
    call_to_action_content[:url] = options[:url]
  end

  @call_to_actions << call_to_action_content
end

#default(chunk) ⇒ Object



26
27
28
# File 'lib/hg/bot.rb', line 26

def default(chunk)
  @default_chunk = chunk
end

#get_started(chunk) ⇒ Object



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

def get_started(chunk)
  @get_started_content = {
    setting_type: 'call_to_actions',
    thread_state: 'new_thread',
    call_to_actions: [
                    {
                      payload: chunk.to_s
                    }
                  ]
  }
end

#image_url_base(base) ⇒ Object



78
79
80
# File 'lib/hg/bot.rb', line 78

def image_url_base(base)
  @image_url_base_portion = base
end

#initObject



19
20
21
22
23
24
# File 'lib/hg/bot.rb', line 19

def init
  initialize_router
  initialize_persistent_menu
  initialize_get_started_button
  initialize_greeting_text
end

#initialize_get_started_buttonObject



70
71
72
# File 'lib/hg/bot.rb', line 70

def initialize_get_started_button
  Facebook::Messenger::Thread.set @get_started_content, access_token: ENV['ACCESS_TOKEN']
end

#initialize_greeting_textObject



82
83
84
85
86
87
88
89
# File 'lib/hg/bot.rb', line 82

def initialize_greeting_text
  Facebook::Messenger::Thread.set({
    setting_type: 'greeting',
    greeting: {
      text: @greeting_text
    }
  }, access_token: ENV['ACCESS_TOKEN'])
end

#initialize_persistent_menuObject



34
35
36
37
38
39
40
# File 'lib/hg/bot.rb', line 34

def initialize_persistent_menu
  Facebook::Messenger::Thread.set({
    setting_type:    'call_to_actions',
    thread_state:    'existing_thread',
    call_to_actions: @call_to_actions
  }, access_token: ENV['ACCESS_TOKEN'])
end

#initialize_routerObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hg/bot.rb', line 96

def initialize_router
  ::Facebook::Messenger::Bot.on :postback do |postback|
    Rails.logger.info 'POSTBACK'
    Rails.logger.info postback.payload

    run_postback_payload(postback.payload, postback.sender)
  end

  ::Facebook::Messenger::Bot.on :message do |message|
    Rails.logger.info 'MESSAGE'
    Rails.logger.info message.text
    Rails.logger.info message.quick_reply

    # Attempt to run a quick reply payload
    if message.quick_reply
      run_postback_payload(message.quick_reply, message.sender)
    # Fall back to fuzzy matching keywords
    elsif fuzzy_match = FuzzyMatch.new(@routes.keys).find(message.text)
      @routes[fuzzy_match].deliver(message.sender)
    # Fallback behavior
    else
      @default_chunk.deliver(message.sender)
    end
  end
end

#persistent_menu(&block) ⇒ Object



30
31
32
# File 'lib/hg/bot.rb', line 30

def persistent_menu(&block)
  yield
end

#run_postback_payload(payload, recipient, context) ⇒ Object



91
92
93
94
# File 'lib/hg/bot.rb', line 91

def run_postback_payload(payload, recipient, context)
  # TODO: Shouldn't be constantizing user input. Need a way to sanitize this.
  payload.constantize.new(recipient: recipient, context: context).deliver
end