class MessengerClient::Client

Constants

URL_BASE
URL_TEMPLATE

Public Class Methods

new(page_access_token) click to toggle source
# File lib/messenger_client/client.rb, line 6
def initialize(page_access_token)
  @page_access_token = page_access_token
  @logger            = MessengerClient::Config.logger
end

Public Instance Methods

audio(recipient_id:, url:) click to toggle source
# File lib/messenger_client/client.rb, line 67
def audio(recipient_id:, url:)
  media = URLAudio.new(url)
  send(recipient_id, media.to_json)
end
button_template(recipient_id:, text:, buttons:) click to toggle source
# File lib/messenger_client/client.rb, line 42
def button_template(recipient_id:, text:, buttons:)
  template = ButtonTemplate.new(text, buttons)
  send(recipient_id, template.to_json)
end
delete(url, payload) click to toggle source
# File lib/messenger_client/client.rb, line 138
def delete(url, payload)
  res = Typhoeus.delete(url, body: json(payload), params: { access_token: @page_access_token }, headers: headers)
  @logger.debug(url)           if ENV["DEBUG"]
  @logger.debug(json(payload)) if ENV["DEBUG"]
  @logger.debug(res.body)      if ENV["DEBUG"]
  @logger.debug(res.headers)   if ENV["DEBUG"]
  res
end
delete_get_started() click to toggle source
# File lib/messenger_client/client.rb, line 16
def delete_get_started
  payload = { fields: ["get_started"] }
  delete(profile_url, payload)
end
delete_persistent_menu() click to toggle source
# File lib/messenger_client/client.rb, line 21
def delete_persistent_menu
  payload = { fields: ["persistent_menu"] }
  delete(profile_url, payload)
end
file(recipient_id:, url:) click to toggle source
# File lib/messenger_client/client.rb, line 72
def file(recipient_id:, url:)
  media = URLFile.new(url)
  send(recipient_id, media.to_json)
end
generic_template(recipient_id:, template_items:) click to toggle source
# File lib/messenger_client/client.rb, line 52
def generic_template(recipient_id:, template_items:)
  template = GenericTemplate.new(template_items)
  send(recipient_id, template.to_json)
end
get(url, query = {}) click to toggle source
# File lib/messenger_client/client.rb, line 130
def get(url, query = {})
  res = Typhoeus.get(url, params: { access_token: @page_access_token }.merge(query), headers: headers)
  if !res.success?
    @logger.error("GET Request to #{url} with query #{query.inspect} Failed with #{res.code}")
  end
  res
end
get_profile(facebook_id:, scopes:) click to toggle source
# File lib/messenger_client/client.rb, line 26
def get_profile(facebook_id:, scopes:)
  scope_list = scopes.join(",")
  query = { fields: scope_list }
  url = URL_BASE % facebook_id
  get(url, query)
end
image(recipient_id:, url:) click to toggle source
# File lib/messenger_client/client.rb, line 57
def image(recipient_id:, url:)
  media = URLImage.new(url)
  send(recipient_id, media.to_json)
end
list_template(recipient_id:, template_items:, buttons: []) click to toggle source
# File lib/messenger_client/client.rb, line 47
def list_template(recipient_id:, template_items:, buttons: [])
  template = ListTemplate.new(template_items, buttons)
  send(recipient_id, template.to_json)
end
location(recipient_id:) click to toggle source
# File lib/messenger_client/client.rb, line 81
def location(recipient_id:)
  loc = MessengerClient::LocationTemplate.new
  send(recipient_id, loc.to_json)
end
post(payload, url = message_url) click to toggle source
# File lib/messenger_client/client.rb, line 112
def post(payload, url = message_url)
  res = Typhoeus.post(url,  body: json(payload), params: { access_token: @page_access_token }, headers: headers)
  @logger.debug(url)           if ENV["DEBUG"]
  @logger.debug(json(payload)) if ENV["DEBUG"]
  @logger.debug(res.body)      if ENV["DEBUG"]
  @logger.debug(res.headers)   if ENV["DEBUG"]
  res
end
profile_post(payload) click to toggle source
# File lib/messenger_client/client.rb, line 121
def profile_post(payload)
  res = Typhoeus.post(profile_url, body: json(payload), params: { access_token: @page_access_token }, headers: headers)
  @logger.debug(profile_url)   if ENV["DEBUG"]
  @logger.debug(json(payload)) if ENV["DEBUG"]
  @logger.debug(res.body)      if ENV["DEBUG"]
  @logger.debug(res.headers)   if ENV["DEBUG"]
  res
end
qr(recipient_id:, text:, replies:) click to toggle source
# File lib/messenger_client/client.rb, line 37
def qr(recipient_id:, text:, replies:)
  qr_template = QuickReplies.new(text, replies)
  send(recipient_id, qr_template.to_json)
end
send(recipient_id, data, opts={}) click to toggle source
# File lib/messenger_client/client.rb, line 96
def send(recipient_id, data, opts={})
  payload = {
    recipient: {
      id: recipient_id
    }
  }
  payload.merge!(message: data) unless data.nil?
  payload.merge!(opts)
  res = Typhoeus.post(message_url, body: json(payload), params: { access_token: @page_access_token }, headers: headers)
  @logger.debug(message_url)   if ENV["DEBUG"]
  @logger.debug(json(payload)) if ENV["DEBUG"]
  @logger.debug(res.body)      if ENV["DEBUG"]
  @logger.debug(res.headers)   if ENV["DEBUG"]
  res
end
set_get_started(postback="get_started") click to toggle source
# File lib/messenger_client/client.rb, line 11
def set_get_started(postback="get_started")
  gs = GetStarted.new(postback)
  profile_post(gs.to_json)
end
text(recipient_id:, text:) click to toggle source
# File lib/messenger_client/client.rb, line 33
def text(recipient_id:, text:)
  send(recipient_id, text: text)
end
typing(recipient_id:, seconds:, &blk) click to toggle source
# File lib/messenger_client/client.rb, line 86
def typing(recipient_id:, seconds:, &blk)
  send(recipient_id, nil, sender_action: "typing_on", mark_seen: "true")
  if seconds > 0
    sleep(seconds)
    if block_given?
      blk.call
    end
  end
end
video(recipient_id:, url:) click to toggle source
# File lib/messenger_client/client.rb, line 62
def video(recipient_id:, url:)
  media = URLVideo.new(url)
  send(recipient_id, media.to_json)
end
youtube_video(recipient_id:, url:) click to toggle source
# File lib/messenger_client/client.rb, line 77
def youtube_video(recipient_id:, url:)
  text(recipient_id: recipient_id, text: url)
end

Private Instance Methods

headers() click to toggle source
# File lib/messenger_client/client.rb, line 161
def headers
  { 'Accept-Encoding' => 'application/json',
    'Content-Type'    => 'application/json' }
end
json(payload) click to toggle source
# File lib/messenger_client/client.rb, line 157
def json(payload)
  JSON.dump(payload)
end
message_url() click to toggle source
# File lib/messenger_client/client.rb, line 149
def message_url
  URL_TEMPLATE % "messages"
end
profile_url() click to toggle source
# File lib/messenger_client/client.rb, line 153
def profile_url
  URL_TEMPLATE % "messenger_profile"
end