Module: Hg::Bot::ClassMethods
- Defined in:
- lib/hg/bot.rb
Instance Attribute Summary collapse
-
#call_to_actions ⇒ Object
Returns the value of attribute call_to_actions.
-
#chunks ⇒ Object
Returns the value of attribute chunks.
-
#default_chunk ⇒ Object
Returns the value of attribute default_chunk.
-
#get_started_content ⇒ Object
Returns the value of attribute get_started_content.
-
#greeting_text(text) ⇒ Object
Returns the value of attribute greeting_text.
-
#image_url_base_portion ⇒ Object
Returns the value of attribute image_url_base_portion.
-
#routes ⇒ Object
Returns the value of attribute routes.
Instance Method Summary collapse
- #call_to_action(text, options = {}) ⇒ Object
- #default(chunk) ⇒ Object
- #get_started(chunk) ⇒ Object
- #image_url_base(base) ⇒ Object
- #init ⇒ Object
- #initialize_get_started_button ⇒ Object
- #initialize_greeting_text ⇒ Object
- #initialize_persistent_menu ⇒ Object
- #initialize_router ⇒ Object
- #persistent_menu(&block) ⇒ Object
- #run_postback_payload(payload, recipient, context) ⇒ Object
Instance Attribute Details
#call_to_actions ⇒ Object
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 |
#chunks ⇒ Object
Returns the value of attribute chunks.
12 13 14 |
# File 'lib/hg/bot.rb', line 12 def chunks @chunks end |
#default_chunk ⇒ Object
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_content ⇒ Object
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_portion ⇒ Object
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 |
#routes ⇒ Object
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, = {}) call_to_action_content = { title: text } if [:to] call_to_action_content[:type] = 'postback' call_to_action_content[:payload] = [:to].to_s elsif [:url] call_to_action_content[:type] = 'web_url' call_to_action_content[:url] = [: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 |
#init ⇒ Object
19 20 21 22 23 24 |
# File 'lib/hg/bot.rb', line 19 def init initialize_router initialize_greeting_text end |
#initialize_get_started_button ⇒ Object
70 71 72 |
# File 'lib/hg/bot.rb', line 70 def Facebook::Messenger::Thread.set @get_started_content, access_token: ENV['ACCESS_TOKEN'] end |
#initialize_greeting_text ⇒ Object
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_menu ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/hg/bot.rb', line 34 def 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_router ⇒ Object
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 || Rails.logger.info 'MESSAGE' Rails.logger.info .text Rails.logger.info .quick_reply # Attempt to run a quick reply payload if .quick_reply run_postback_payload(.quick_reply, .sender) # Fall back to fuzzy matching keywords elsif fuzzy_match = FuzzyMatch.new(@routes.keys).find(.text) @routes[fuzzy_match].deliver(.sender) # Fallback behavior else @default_chunk.deliver(.sender) end end end |
#persistent_menu(&block) ⇒ Object
30 31 32 |
# File 'lib/hg/bot.rb', line 30 def (&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 |