Class: Bobot::Page
- Inherits:
-
Object
- Object
- Bobot::Page
- Defined in:
- lib/bobot/page.rb
Instance Attribute Summary collapse
-
#get_started_payload ⇒ Object
Returns the value of attribute get_started_payload.
-
#language ⇒ Object
Returns the value of attribute language.
-
#page_access_token ⇒ Object
Returns the value of attribute page_access_token.
-
#page_id ⇒ Object
Returns the value of attribute page_id.
-
#slug ⇒ Object
Returns the value of attribute slug.
Class Method Summary collapse
Instance Method Summary collapse
-
#deliver(payload_template:, to:) ⇒ Object
REQUESTS.
-
#initialize(options = {}) ⇒ Page
constructor
A new instance of Page.
- #mark_as_seen(to: nil) ⇒ Object
- #send(payload_message:, to: nil) ⇒ Object
- #send_attachment(url:, type:, to: nil) ⇒ Object
- #send_audio(url:, to: nil) ⇒ Object
- #send_buttons(text:, buttons:, to: nil) ⇒ Object
- #send_file(url:, to: nil) ⇒ Object
- #send_generic(elements:, image_aspect_ratio: 'square', to: nil) ⇒ Object (also: #send_carousel)
- #send_image(url:, to: nil) ⇒ Object
- #send_quick_replies(text:, quick_replies:, to: nil) ⇒ Object
- #send_text(text:, to: nil) ⇒ Object
- #send_video(url:, to: nil) ⇒ Object
- #sender_action(sender_action:, to: nil) ⇒ Object
-
#set_get_started_button! ⇒ Object
You can define the action to trigger when new humans click on == == the Get Started button.
-
#set_greeting_text! ⇒ Object
Set bot description (only displayed on first time).
-
#set_persistent_menu! ⇒ Object
You can show a persistent menu to humans.
-
#set_whitelist_domains! ⇒ Object
Set bot whitelist domains (only displayed on first time) == == Some features like Messenger Extensions and Checkbox Plugin require == == a page to specify a domain whitelist.
- #show_typing(state:, to: nil) ⇒ Object
-
#subscribe_to_facebook_page! ⇒ Object
Subcribe your bot to your page ==.
- #unset_get_started_button! ⇒ Object
- #unset_greeting_text! ⇒ Object
- #unset_persistent_menu! ⇒ Object
- #unset_whitelist_domains! ⇒ Object
-
#unsubscribe_to_facebook_page! ⇒ Object
Unsubcribe your bot from your page ==.
-
#update_facebook_setup! ⇒ Object
SETUP.
Constructor Details
#initialize(options = {}) ⇒ Page
Returns a new instance of Page.
5 6 7 8 9 10 11 |
# File 'lib/bobot/page.rb', line 5 def initialize( = {}) self.slug = [:slug] self.language = [:language] self.page_id = [:page_id] self.page_access_token = [:page_access_token] self.get_started_payload = [:get_started_payload] end |
Instance Attribute Details
#get_started_payload ⇒ Object
Returns the value of attribute get_started_payload.
3 4 5 |
# File 'lib/bobot/page.rb', line 3 def get_started_payload @get_started_payload end |
#language ⇒ Object
Returns the value of attribute language.
3 4 5 |
# File 'lib/bobot/page.rb', line 3 def language @language end |
#page_access_token ⇒ Object
Returns the value of attribute page_access_token.
3 4 5 |
# File 'lib/bobot/page.rb', line 3 def page_access_token @page_access_token end |
#page_id ⇒ Object
Returns the value of attribute page_id.
3 4 5 |
# File 'lib/bobot/page.rb', line 3 def page_id @page_id end |
#slug ⇒ Object
Returns the value of attribute slug.
3 4 5 |
# File 'lib/bobot/page.rb', line 3 def slug @slug end |
Class Method Details
.[](value) ⇒ Object
27 28 29 |
# File 'lib/bobot/page.rb', line 27 def self.[](value) find(value) || find_by_slug(value) end |
Instance Method Details
#deliver(payload_template:, to:) ⇒ Object
REQUESTS
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bobot/page.rb', line 37 def deliver(payload_template:, to:) raise Bobot::FieldFormat.new('payload_template is required.') unless payload_template.present? Bobot::Commander.deliver( body: { recipient: { id: to }, }.merge(payload_template), query: { access_token: page_access_token, }, ) end |
#mark_as_seen(to: nil) ⇒ Object
57 58 59 |
# File 'lib/bobot/page.rb', line 57 def mark_as_seen(to: nil) sender_action(sender_action: 'mark_seen', to: to) end |
#send(payload_message:, to: nil) ⇒ Object
61 62 63 |
# File 'lib/bobot/page.rb', line 61 def send(payload_message:, to: nil) deliver(payload_template: { message: }, to: to) end |
#send_attachment(url:, type:, to: nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bobot/page.rb', line 76 def (url:, type:, to: nil) raise Bobot::FieldFormat.new('url is required.') unless url.present? raise Bobot::FieldFormat.new('type is required.') unless type.present? raise Bobot::FieldFormat.new('type is invalid, only "image, audio, video, file" are permitted.') unless %w[image audio video file].include?(type) send( payload_message: { attachment: { type: type, payload: { url: url, }.tap { |properties| properties.merge!(is_reusable: true) if type == 'image' }, }, }, to: to, ) end |
#send_audio(url:, to: nil) ⇒ Object
97 98 99 |
# File 'lib/bobot/page.rb', line 97 def send_audio(url:, to: nil) (url: url, type: 'audio', to: to) end |
#send_buttons(text:, buttons:, to: nil) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/bobot/page.rb', line 123 def (text:, buttons:, to: nil) raise Bobot::FieldFormat.new('text is required.') unless text.present? raise Bobot::FieldFormat.new('text length is limited to 640.') if text.size > 640 raise Bobot::FieldFormat.new('buttons are required.') unless .present? raise Bobot::FieldFormat.new('buttons are limited to 3.') if .size > 3 send( payload_message: { attachment: { type: 'template', payload: { template_type: 'button', text: text, buttons: , }, }, }, to: to, ) end |
#send_file(url:, to: nil) ⇒ Object
105 106 107 |
# File 'lib/bobot/page.rb', line 105 def send_file(url:, to: nil) (url: url, type: 'file', to: to) end |
#send_generic(elements:, image_aspect_ratio: 'square', to: nil) ⇒ Object Also known as: send_carousel
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bobot/page.rb', line 143 def send_generic(elements:, image_aspect_ratio: 'square', to: nil) raise Bobot::FieldFormat.new('elements are required.') if elements.nil? raise Bobot::FieldFormat.new('elements are limited to 10.') if elements.size > 10 raise Bobot::FieldFormat.new('image_aspect_ratio is required.') if image_aspect_ratio.nil? raise Bobot::FieldFormat.new('image_aspect_ratio is invalid, only "square, horizontal" are permitted.') unless %w[square horizontal].include?(image_aspect_ratio) send( payload_message: { attachment: { type: 'template', payload: { template_type: 'generic', image_aspect_ratio: image_aspect_ratio, elements: elements, }, }, }, to: to, ) end |
#send_image(url:, to: nil) ⇒ Object
93 94 95 |
# File 'lib/bobot/page.rb', line 93 def send_image(url:, to: nil) (url: url, type: 'image', to: to) end |
#send_quick_replies(text:, quick_replies:, to: nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bobot/page.rb', line 109 def send_quick_replies(text:, quick_replies:, to: nil) raise Bobot::FieldFormat.new('text is required.') unless text.present? raise Bobot::FieldFormat.new('text length is limited to 640.') if text.size > 640 raise Bobot::FieldFormat.new('quick_replies are required.') unless quick_replies.present? raise Bobot::FieldFormat.new('quick_replies are limited to 11.') if quick_replies.size > 11 send( payload_message: { text: text, quick_replies: quick_replies, }, to: to, ) end |
#send_text(text:, to: nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bobot/page.rb', line 65 def send_text(text:, to: nil) raise Bobot::FieldFormat.new('text is required.') unless text.present? raise Bobot::FieldFormat.new('text length is limited to 640.') if text.size > 640 send( payload_message: { text: text, }, to: to, ) end |
#send_video(url:, to: nil) ⇒ Object
101 102 103 |
# File 'lib/bobot/page.rb', line 101 def send_video(url:, to: nil) (url: url, type: 'video', to: to) end |
#sender_action(sender_action:, to: nil) ⇒ Object
49 50 51 |
# File 'lib/bobot/page.rb', line 49 def sender_action(sender_action:, to: nil) deliver(payload_template: { sender_action: sender_action }, to: to) end |
#set_get_started_button! ⇒ Object
You can define the action to trigger when new humans click on ==
the Get Started button. Before doing it you should check to select the ==
messaging_postbacks field when setting up your webhook. ==
261 262 263 264 265 266 267 268 |
# File 'lib/bobot/page.rb', line 261 def raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? raise Bobot::InvalidParameter.new(:get_started_payload) unless get_started_payload.present? Bobot::Profile.set( body: { get_started: { payload: get_started_payload } }, query: { access_token: page_access_token }, ) end |
#set_greeting_text! ⇒ Object
Set bot description (only displayed on first time). ==
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/bobot/page.rb', line 202 def set_greeting_text! raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? greeting_texts = [] if language.nil? # Default text greeting_text = I18n.t("bobot.#{slug}.config.greeting_text", locale: I18n.default_locale, default: nil) greeting_texts << { locale: 'default', text: greeting_text } if greeting_text.present? # Each languages I18n.available_locales.each do |locale| greeting_text = I18n.t("bobot.#{slug}.config.greeting_text", locale: locale, default: nil) next unless greeting_text.present? facebook_locales = I18n.t("bobot.#{slug}.config.facebook_locales", locale: locale, default: nil) facebook_locales.to_a.each do |locale_long| greeting_texts << { locale: locale_long, text: greeting_text } end end else greeting_text = I18n.t("bobot.#{slug}.config.greeting_text", locale: language, default: nil) greeting_texts << { locale: 'default', text: greeting_text } if greeting_text.present? end if greeting_texts.present? Bobot::Profile.set( body: { greeting: greeting_texts }, query: { access_token: page_access_token }, ) end end |
#set_persistent_menu! ⇒ Object
You can show a persistent menu to humans. ==
If you want to have a persistent menu, you have to set get_started ==
button before. ==
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/bobot/page.rb', line 281 def raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? = [] # Default text if language.nil? = I18n.t("bobot.#{slug}.config.persistent_menu", locale: I18n.default_locale, default: nil) if .present? << { locale: 'default', composer_input_disabled: [:composer_input_disabled], call_to_actions: [:call_to_actions], } end # Each languages I18n.available_locales.each do |locale| = I18n.t("bobot.#{slug}.config.persistent_menu", locale: locale, default: nil) facebook_locales = I18n.t("bobot.#{slug}.config.facebook_locales", locale: locale, default: nil) next unless .present? facebook_locales.to_a.each do |locale_long| << { locale: locale_long, composer_input_disabled: [:composer_input_disabled], call_to_actions: [:call_to_actions], } end end else = I18n.t("bobot.#{slug}.config.persistent_menu", locale: language, default: nil) if .present? << { locale: 'default', composer_input_disabled: [:composer_input_disabled], call_to_actions: [:call_to_actions], } end end if .present? Bobot::Profile.set( body: { persistent_menu: }, query: { access_token: page_access_token }, ) end end |
#set_whitelist_domains! ⇒ Object
Set bot whitelist domains (only displayed on first time) ==
Some features like Messenger Extensions and Checkbox Plugin require ==
a page to specify a domain whitelist. ==
241 242 243 244 245 246 247 248 |
# File 'lib/bobot/page.rb', line 241 def set_whitelist_domains! raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? raise Bobot::InvalidParameter.new(:domains) unless Bobot.config.domains.present? Bobot::Profile.set( body: { whitelisted_domains: Bobot.config.domains }, query: { access_token: page_access_token }, ) end |
#show_typing(state:, to: nil) ⇒ Object
53 54 55 |
# File 'lib/bobot/page.rb', line 53 def show_typing(state:, to: nil) sender_action(sender_action: (state ? 'typing_on' : 'typing_off'), to: to) end |
#subscribe_to_facebook_page! ⇒ Object
Subcribe your bot to your page ==
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bobot/page.rb', line 178 def subscribe_to_facebook_page! raise Bobot::InvalidParameter.new(:page_id) unless page_id.present? raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? Bobot::Subscription.set( query: { page_id: page_id, access_token: page_access_token, }, ) end |
#unset_get_started_button! ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/bobot/page.rb', line 270 def raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? Bobot::Profile.unset( body: { fields: %w[persistent_menu get_started] }, query: { access_token: page_access_token }, ) end |
#unset_greeting_text! ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/bobot/page.rb', line 230 def unset_greeting_text! raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? Bobot::Profile.unset( body: { fields: %w[greeting] }, query: { access_token: page_access_token }, ) end |
#unset_persistent_menu! ⇒ Object
325 326 327 328 329 330 331 |
# File 'lib/bobot/page.rb', line 325 def raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? Bobot::Profile.unset( body: { fields: ["persistent_menu"] }, query: { access_token: page_access_token }, ) end |
#unset_whitelist_domains! ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/bobot/page.rb', line 250 def unset_whitelist_domains! raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? Bobot::Profile.unset( body: { fields: ["whitelisted_domains"] }, query: { access_token: page_access_token }, ) end |
#unsubscribe_to_facebook_page! ⇒ Object
Unsubcribe your bot from your page ==
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/bobot/page.rb', line 190 def unsubscribe_to_facebook_page! raise Bobot::InvalidParameter.new(:page_id) unless page_id.present? raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present? Bobot::Subscription.unset( query: { page_id: page_id, access_token: page_access_token, }, ) end |
#update_facebook_setup! ⇒ Object
SETUP
169 170 171 172 173 174 175 |
# File 'lib/bobot/page.rb', line 169 def update_facebook_setup! subscribe_to_facebook_page! set_greeting_text! set_whitelist_domains! end |