Class: MessengerClient::Client
- Inherits:
-
Object
- Object
- MessengerClient::Client
- Defined in:
- lib/messenger_client/client.rb
Constant Summary collapse
- URL_BASE =
"https://graph.facebook.com/v2.6/%s"
- URL_TEMPLATE =
URL_BASE % "me/%s"
Instance Method Summary collapse
- #audio(recipient_id:, url:) ⇒ Object
- #button_template(recipient_id:, text:, buttons:) ⇒ Object
- #delete(url, payload) ⇒ Object
- #delete_get_started ⇒ Object
- #delete_persistent_menu ⇒ Object
- #file(recipient_id:, url:) ⇒ Object
- #generic_template(recipient_id:, template_items:) ⇒ Object
- #get(url, query = {}) ⇒ Object
- #get_profile(facebook_id:, scopes:) ⇒ Object
- #image(recipient_id:, url:) ⇒ Object
-
#initialize(page_access_token) ⇒ Client
constructor
A new instance of Client.
- #list_template(recipient_id:, template_items:, buttons: []) ⇒ Object
- #location(recipient_id:) ⇒ Object
- #post(payload, url = message_url) ⇒ Object
- #profile_post(payload) ⇒ Object
- #qr(recipient_id:, text:, replies:) ⇒ Object
- #send(recipient_id, data, opts = {}) ⇒ Object
- #set_get_started(postback = "get_started") ⇒ Object
- #text(recipient_id:, text:) ⇒ Object
- #typing(recipient_id:, seconds:, &blk) ⇒ Object
- #video(recipient_id:, url:) ⇒ Object
- #youtube_video(recipient_id:, url:) ⇒ Object
Constructor Details
#initialize(page_access_token) ⇒ Client
Returns a new instance of Client.
6 7 8 9 |
# File 'lib/messenger_client/client.rb', line 6 def initialize(page_access_token) @page_access_token = page_access_token @logger = MessengerClient::Config.logger end |
Instance Method Details
#audio(recipient_id:, url:) ⇒ Object
67 68 69 70 |
# 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:) ⇒ Object
42 43 44 45 |
# File 'lib/messenger_client/client.rb', line 42 def (recipient_id:, text:, buttons:) template = ButtonTemplate.new(text, ) send(recipient_id, template.to_json) end |
#delete(url, payload) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/messenger_client/client.rb', line 140 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 ⇒ Object
16 17 18 19 |
# File 'lib/messenger_client/client.rb', line 16 def delete_get_started payload = { fields: ["get_started"] } delete(profile_url, payload) end |
#delete_persistent_menu ⇒ Object
21 22 23 24 |
# File 'lib/messenger_client/client.rb', line 21 def payload = { fields: ["persistent_menu"] } delete(profile_url, payload) end |
#file(recipient_id:, url:) ⇒ Object
72 73 74 75 |
# 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:) ⇒ Object
52 53 54 55 |
# 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 = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# 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? JSON.load(res.body) else @logger.error("GET Request to #{url} with query #{query.inspect} Failed with #{res.code}") nil end end |
#get_profile(facebook_id:, scopes:) ⇒ Object
26 27 28 29 30 31 |
# 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:) ⇒ Object
57 58 59 60 |
# 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: []) ⇒ Object
47 48 49 50 |
# File 'lib/messenger_client/client.rb', line 47 def list_template(recipient_id:, template_items:, buttons: []) template = ListTemplate.new(template_items, ) send(recipient_id, template.to_json) end |
#location(recipient_id:) ⇒ Object
81 82 83 84 |
# 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) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/messenger_client/client.rb', line 112 def post(payload, 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) ⇒ Object
121 122 123 124 125 126 127 128 |
# 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:) ⇒ Object
37 38 39 40 |
# 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 = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# 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(, body: json(payload), params: { access_token: @page_access_token }, headers: headers) @logger.debug() 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") ⇒ Object
11 12 13 14 |
# 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:) ⇒ Object
33 34 35 |
# File 'lib/messenger_client/client.rb', line 33 def text(recipient_id:, text:) send(recipient_id, text: text) end |
#typing(recipient_id:, seconds:, &blk) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# 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:) ⇒ Object
62 63 64 65 |
# 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:) ⇒ Object
77 78 79 |
# File 'lib/messenger_client/client.rb', line 77 def youtube_video(recipient_id:, url:) text(recipient_id: recipient_id, text: url) end |