Class: Kogno::Messenger::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/core/lib/messenger/api.rb

Class Method Summary collapse

Class Method Details

.add_domain(domain) ⇒ Object



251
252
253
254
255
256
257
258
259
# File 'lib/core/lib/messenger/api.rb', line 251

def add_domain(domain)
  body = {
    whitelisted_domains: [
      domain
    ]
  }
  logger.write_json body, :bright
  self.setting(body.to_json)
end

.attachment_message(attachment) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/core/lib/messenger/api.rb', line 121

def attachment_message(attachment)

  {
    attachment: attachment
  }

end

.button_template(text, buttons) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/core/lib/messenger/api.rb', line 129

def button_template(text,buttons)

  attachment = {
    type: :template,
    payload: {
      template_type: :button,
      text: text,
      buttons: buttons
    }
  }
  return self.attachment_message(attachment)

end

.generic_template(elements, image_aspect_ratio = :horizontal) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/core/lib/messenger/api.rb', line 169

def generic_template(elements,image_aspect_ratio=:horizontal)

  elements = [elements] unless elements.class == Array

  attachment = {
    type: :template,
    payload: {
      template_type: :generic,
      image_aspect_ratio: image_aspect_ratio,
      elements: elements
    }
  }

  return self.attachment_message(attachment)

end

.generic_template_with_replies(elements, image_aspect_ratio = :horizontal, quick_replies) ⇒ Object



186
187
188
189
190
# File 'lib/core/lib/messenger/api.rb', line 186

def generic_template_with_replies(elements,image_aspect_ratio=:horizontal, quick_replies)
  message = generic_template(elements,image_aspect_ratio=:horizontal)
  message[:quick_replies] = quick_replies
  return message
end

.get_access_token(page_id = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/core/lib/messenger/api.rb', line 203

def get_access_token(page_id=nil)

  if page_id.nil?
    Kogno::Application.messenger.pages.first[1][:token]
  else
    Kogno::Application.messenger.pages[page_id][:token] rescue nil
  end

end

.get_started_button(page_id = nil) ⇒ Object



233
234
235
236
237
238
239
240
241
# File 'lib/core/lib/messenger/api.rb', line 233

def get_started_button(page_id=nil)
  body = {
    get_started: {
      payload: Kogno::Application.messenger.welcome_screen_payload
    }
  }
  logger.write_json body, :bright
  self.setting(body.to_json, page_id)
end

.greeting(page_id = nil) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/core/lib/messenger/api.rb', line 269

def greeting(page_id=nil)
  body = {
    greeting: Kogno::Application.messenger.greeting
  }
  logger.write_json body, :bright
  self.setting(body.to_json, page_id)
end

.ice_breakers(page_id = nil) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/core/lib/messenger/api.rb', line 243

def ice_breakers(page_id=nil)
  body = {
    ice_breakers: Kogno::Application.messenger.ice_breakers
  }
  logger.write_json body, :bright
  self.setting(body.to_json, page_id)
end

.list_template(elements, buttons = []) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/core/lib/messenger/api.rb', line 154

def list_template(elements, buttons = [])
  attachment = {
    type: :template,
    payload: {
      template_type: :list,
      top_element_style: :compact,
      elements: elements,
      buttons: buttons
    }
  }

  return self.attachment_message(attachment)

end

.persistent_menu(page_id = nil) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/core/lib/messenger/api.rb', line 213

def persistent_menu(page_id=nil)
  persistent_menu = Kogno::Application.messenger.persistent_menu
  if persistent_menu.nil?
    body = {
      persistent_menu:[
        {
          locale: "default",
          composer_input_disabled: false,
        }
      ]
    }
  else
    body = {
      persistent_menu: persistent_menu
    }
  end
  logger.write_json body, :bright
  self.setting(body.to_json, page_id)
end

.quick_replies(title, replies) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/core/lib/messenger/api.rb', line 143

def quick_replies(title,replies)

  response = {
    text: title,
    quick_replies: replies
  }

  return response

end

.request(body, access_token, type = :messages, method = :post) ⇒ Object

:messages, :messenger_profile



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/core/lib/messenger/api.rb', line 12

def request(body,access_token,type=:messages,method=:post) # :messages, :messenger_profile
  url = URI("#{Kogno::Application.messenger.graph_url}/#{type}?access_token=#{access_token}")

  logger.write "REQUEST TO: #{url}", :pink
  logger.write "SENT: #{JSON.parse(body)}", :blue      

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  case method
    when :delete
      request = Net::HTTP::Delete.new(url)
    else 
      request = Net::HTTP::Post.new(url)
  end    

  request["content-type"] = 'application/json'

  request.body = body

  response = http.request(request)
  # logger.debug_json JSON.parse(body), :green
  response_hash = JSON.parse(response.read_body, {:symbolize_names => true})
  logger.write "RESPONSE: #{response_hash}\n", :light_blue
  return(response_hash)
end

.send_action(recipient_psid, action, messaging_type = "RESPONSE", page_id = nil) ⇒ Object

action => mark_seen, typing_on, typing_off



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/core/lib/messenger/api.rb', line 97

def send_action(recipient_psid,action,messaging_type="RESPONSE",page_id=nil) # action => mark_seen, typing_on, typing_off
  # body = %{{"recipient":{"id":"#{recipient_psid}"},"sender_action":"#{action}"}}
  body = {
    messaging_type: messaging_type,
    recipient: {
      id: recipient_psid
    },
    sender_action: action
  }.to_json

  access_token = self.get_access_token(page_id)
  return(self.request(body,access_token))
end

.send_message(recipient_psid, message, messaging_type = "RESPONSE", page_id = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/core/lib/messenger/api.rb', line 52

def send_message(recipient_psid,message,messaging_type="RESPONSE",page_id=nil)

  body = {
    messaging_type: messaging_type,
    recipient: {
      id: recipient_psid
    },
    message: message
  }.to_json

  access_token = self.get_access_token(page_id)
  return(self.request(body,access_token))

end

.send_message_with_rn_token(notification_token, message, page_id = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/core/lib/messenger/api.rb', line 67

def send_message_with_rn_token(notification_token,message,page_id=nil)

  body = {
    recipient: {
      notification_messages_token: notification_token
    },
    message: message
  }.to_json

  access_token = self.get_access_token(page_id)
  return(self.request(body,access_token))

end

.send_private_reply(recipient_id, message, page_id = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/core/lib/messenger/api.rb', line 82

def send_private_reply(recipient_id,message,page_id=nil)

  body = {
    :recipient => {
      :comment_id => recipient_id
    },
    :message => message
  }.to_json

  access_token = self.get_access_token(page_id)
  return(self.request(body,access_token))
end

.setting(body, page_id = nil) ⇒ Object



40
41
42
43
# File 'lib/core/lib/messenger/api.rb', line 40

def setting(body,page_id=nil)
  access_token = self.get_access_token(page_id)
  self.request(body,access_token,:messenger_profile)
end

.setting_delete(fields, page_id = nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/core/lib/messenger/api.rb', line 45

def setting_delete(fields,page_id=nil)
  access_token = self.get_access_token(page_id)
  self.request({
    fields: fields
  }.to_json,access_token,:messenger_profile, :delete)
end

.show_ad_message(message) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/core/lib/messenger/api.rb', line 192

def show_ad_message(message)

  JSON.pretty_generate(
    {
      message: message,
      user_edit: true
    }
  )

end

.text_message(text) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/core/lib/messenger/api.rb', line 111

def text_message(text)

  {
    :text => text
  }
  # return %{{"text":"#{text}"}}

end

.update_whitelisted_domainsObject



261
262
263
264
265
266
267
# File 'lib/core/lib/messenger/api.rb', line 261

def update_whitelisted_domains
  body = {
    whitelisted_domains: Kogno::Application.messenger.whitelisted_domains
  }
  logger.write_json body, :bright
  self.setting(body.to_json)
end