Class: Bobot::Configuration::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/bobot/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Page

Returns a new instance of Page.



27
28
29
30
31
32
# File 'lib/bobot/configuration.rb', line 27

def initialize(options = {})
  self.slug = options[:slug]
  self.language = options[:language]
  self.page_id = options[:page_id]
  self.page_access_token = options[:page_access_token]
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



25
26
27
# File 'lib/bobot/configuration.rb', line 25

def language
  @language
end

#page_access_tokenObject

Returns the value of attribute page_access_token.



25
26
27
# File 'lib/bobot/configuration.rb', line 25

def page_access_token
  @page_access_token
end

#page_idObject

Returns the value of attribute page_id.



25
26
27
# File 'lib/bobot/configuration.rb', line 25

def page_id
  @page_id
end

#slugObject

Returns the value of attribute slug.



25
26
27
# File 'lib/bobot/configuration.rb', line 25

def slug
  @slug
end

Instance Method Details

#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. ==



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bobot/configuration.rb', line 128

def set_get_started_button!
  raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present?
  if I18n.exists?("bobot.#{slug}.config.get_started.payload")
    Bobot::Profile.set(
      body: { get_started: { payload: I18n.t("bobot.#{slug}.config.get_started.payload") } },
      query: { access_token: page_access_token },
    )
  else
    unset_get_started_button!
  end
end

#set_greeting_text!Object

Set bot description (only displayed on first time). ==



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bobot/configuration.rb', line 67

def set_greeting_text!
  raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present?
  greeting_texts = []
  # 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
  if greeting_texts.present?
    Bobot::Profile.set(
      body: { greeting: greeting_texts },
      query: { access_token: page_access_token },
    )
  else
    unset_greeting_text!
  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. ==



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/bobot/configuration.rb', line 151

def set_persistent_menu!
  raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present?
  persistent_menus = []
  # Default text
  persistent_menu = I18n.t("bobot.#{slug}.config.persistent_menu", locale: I18n.default_locale, default: nil)
  if persistent_menu.present?
    persistent_menus << {
      locale: 'default',
      composer_input_disabled: persistent_menu[:composer_input_disabled],
      call_to_actions: persistent_menu[:call_to_actions],
    }
  end
  # Each languages
  I18n.available_locales.each do |locale|
    persistent_menu = 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 persistent_menu.present?
    facebook_locales.to_a.each do |locale_long|
      persistent_menus << {
        locale: locale_long,
        composer_input_disabled: persistent_menu[:composer_input_disabled],
        call_to_actions: persistent_menu[:call_to_actions],
      }
    end
  end
  if persistent_menus.present?
    Bobot::Profile.set(
      body: { persistent_menu: persistent_menus },
      query: { access_token: page_access_token },
    )
  else
    unset_persistent_menu!
  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. ==



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bobot/configuration.rb', line 103

def set_whitelist_domains!
  raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present?
  raise Bobot::InvalidParameter.new(:domains) unless domains.present?
  if domains.present?
    Bobot::Profile.set(
      body: { whitelisted_domains: domains },
      query: { access_token: page_access_token },
    )
  else
    unset_whitelist_domains!
  end
end

#subscribe_to_facebook_page!Object

Subcribe your bot to your page ==



43
44
45
46
47
48
49
50
51
52
# File 'lib/bobot/configuration.rb', line 43

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



140
141
142
143
144
145
146
# File 'lib/bobot/configuration.rb', line 140

def unset_get_started_button!
  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



92
93
94
95
96
97
98
# File 'lib/bobot/configuration.rb', line 92

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



186
187
188
189
190
191
192
# File 'lib/bobot/configuration.rb', line 186

def unset_persistent_menu!
  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



116
117
118
119
120
121
122
123
# File 'lib/bobot/configuration.rb', line 116

def unset_whitelist_domains!
  raise Bobot::InvalidParameter.new(:access_token) unless page_access_token.present?
  raise Bobot::InvalidParameter.new(:domains) unless domains.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 ==



55
56
57
58
59
60
61
62
63
64
# File 'lib/bobot/configuration.rb', line 55

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



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

def update_facebook_setup!
  subscribe_to_facebook_page!
  set_greeting_text!
  set_whitelist_domains!
  set_get_started_button!
  set_persistent_menu!
end