message_quickly 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +29 -0
- data/app/assets/javascripts/message_quickly/index.js.erb +15 -0
- data/app/assets/stylesheets/message_quickly/application.css +15 -0
- data/app/controllers/message_quickly/application_controller.rb +4 -0
- data/app/controllers/message_quickly/webhooks_controller.rb +23 -0
- data/app/helpers/message_quickly/application_helper.rb +25 -0
- data/app/views/layouts/message_quickly/application.html.erb +14 -0
- data/config/routes.rb +4 -0
- data/lib/generators/message_quickly/callbacks/USAGE +11 -0
- data/lib/generators/message_quickly/callbacks/callbacks_generator.rb +23 -0
- data/lib/generators/message_quickly/callbacks/templates/authentication_callback.rb +10 -0
- data/lib/generators/message_quickly/callbacks/templates/message_delivered_callback.rb +10 -0
- data/lib/generators/message_quickly/callbacks/templates/message_received_callback.rb +10 -0
- data/lib/generators/message_quickly/callbacks/templates/postback_callback.rb +10 -0
- data/lib/generators/message_quickly/callbacks/templates/process_messenger_callback_job.rb +13 -0
- data/lib/generators/message_quickly/callbacks/templates/send_messenger_delivery_job.rb +20 -0
- data/lib/generators/message_quickly/callbacks/templates/webhooks.rb +5 -0
- data/lib/message_quickly/api/base.rb +20 -0
- data/lib/message_quickly/api/client.rb +62 -0
- data/lib/message_quickly/api/facebook_api_exception.rb +31 -0
- data/lib/message_quickly/api/graph_method_exception.rb +20 -0
- data/lib/message_quickly/api/messages.rb +46 -0
- data/lib/message_quickly/api/no_matching_user_exception.rb +6 -0
- data/lib/message_quickly/api/not_permitted_exception.rb +6 -0
- data/lib/message_quickly/api/oauth_exception.rb +20 -0
- data/lib/message_quickly/api/send_message_exception.rb +6 -0
- data/lib/message_quickly/api/thread_settings.rb +43 -0
- data/lib/message_quickly/api/user_profile.rb +22 -0
- data/lib/message_quickly/callback.rb +4 -0
- data/lib/message_quickly/callback_parser.rb +86 -0
- data/lib/message_quickly/callback_registry.rb +28 -0
- data/lib/message_quickly/engine.rb +20 -0
- data/lib/message_quickly/messaging/attachment.rb +14 -0
- data/lib/message_quickly/messaging/base.rb +12 -0
- data/lib/message_quickly/messaging/button.rb +9 -0
- data/lib/message_quickly/messaging/button_template_attachment.rb +56 -0
- data/lib/message_quickly/messaging/delivery.rb +29 -0
- data/lib/message_quickly/messaging/delivery_event.rb +47 -0
- data/lib/message_quickly/messaging/element.rb +33 -0
- data/lib/message_quickly/messaging/entry.rb +14 -0
- data/lib/message_quickly/messaging/event.rb +21 -0
- data/lib/message_quickly/messaging/generic_template_attachment.rb +82 -0
- data/lib/message_quickly/messaging/image_attachment.rb +41 -0
- data/lib/message_quickly/messaging/message.rb +36 -0
- data/lib/message_quickly/messaging/message_event.rb +60 -0
- data/lib/message_quickly/messaging/optin_event.rb +44 -0
- data/lib/message_quickly/messaging/postback_button.rb +24 -0
- data/lib/message_quickly/messaging/postback_event.rb +44 -0
- data/lib/message_quickly/messaging/receipt/address.rb +31 -0
- data/lib/message_quickly/messaging/receipt/adjustment.rb +23 -0
- data/lib/message_quickly/messaging/receipt/element.rb +22 -0
- data/lib/message_quickly/messaging/receipt/summary.rb +27 -0
- data/lib/message_quickly/messaging/receipt_template_attachment.rb +108 -0
- data/lib/message_quickly/messaging/recipient.rb +17 -0
- data/lib/message_quickly/messaging/sender.rb +6 -0
- data/lib/message_quickly/messaging/template_attachment.rb +14 -0
- data/lib/message_quickly/messaging/user.rb +23 -0
- data/lib/message_quickly/messaging/web_url_button.rb +24 -0
- data/lib/message_quickly/version.rb +4 -0
- data/lib/message_quickly.rb +22 -0
- data/spec/callback_parser_spec.rb +53 -0
- data/spec/callback_registry_spec.rb +7 -0
- data/spec/controllers/webhooks_controller_spec.rb +95 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/process_messenger_callback_job.rb +13 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/webhooks/authentication_callback.rb +10 -0
- data/spec/dummy/app/webhooks/message_delivered_callback.rb +10 -0
- data/spec/dummy/app/webhooks/message_received_callback.rb +10 -0
- data/spec/dummy/app/webhooks/postback_callback.rb +10 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config/application.rb +31 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/webhooks.rb +5 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +18183 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/fixtures/12057251_909506139117248_2059695706_n.png +0 -0
- data/spec/fixtures/delivery_request.json +26 -0
- data/spec/fixtures/message_request.json +25 -0
- data/spec/fixtures/message_request_with_attachment.json +32 -0
- data/spec/fixtures/optin_request.json +23 -0
- data/spec/fixtures/postback_request.json +23 -0
- data/spec/helpers/application_helper_spec.rb +13 -0
- data/spec/message_quickly/api/messages_spec.rb +251 -0
- data/spec/message_quickly/api/thread_settings_spec.rb +31 -0
- data/spec/message_quickly/api/user_profile_spec.rb +29 -0
- data/spec/message_quickly/messaging/user_spec.rb +10 -0
- data/spec/spec_helper.rb +24 -0
- metadata +318 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21279dc223fc0f6b479e69362523c487b396b574
|
4
|
+
data.tar.gz: 0c1ab814d05652215757b7922e05dbd7220a9153
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38cd46f284b303c47dcc3653c706c44831ee87ba04497d958b62c20dce4d3427d2032c5d2f14138cff5e55053f643af7940435916eebc5aa0dee0ebdd3c2d6df
|
7
|
+
data.tar.gz: 3325e5fa6b2cf9cb79ed681a8426cb045de26e70d2a2c3ce571eb1b94beb6b3ebfd4c7911d127e96de6971fd33ad4355104f4b138ea61aa04b6e1f4105fa58e4
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Jaryl Sim
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'MessageQuickly'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rspec/core'
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new(:spec)
|
28
|
+
|
29
|
+
task :default => :spec
|
@@ -0,0 +1,15 @@
|
|
1
|
+
window.fbAsyncInit = function() {
|
2
|
+
FB.init({
|
3
|
+
appId : '<%= ENV['FACEBOOK_APP_ID'] %>',
|
4
|
+
xfbml : true,
|
5
|
+
version : 'v2.6'
|
6
|
+
});
|
7
|
+
};
|
8
|
+
|
9
|
+
(function(d, s, id){
|
10
|
+
var js, fjs = d.getElementsByTagName(s)[0];
|
11
|
+
if (d.getElementById(id)) {return;}
|
12
|
+
js = d.createElement(s); js.id = id;
|
13
|
+
js.src = "//connect.facebook.net/en_US/sdk.js";
|
14
|
+
fjs.parentNode.insertBefore(js, fjs);
|
15
|
+
}(document, 'script', 'facebook-jssdk'));
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
class WebhooksController < ApplicationController
|
3
|
+
|
4
|
+
skip_before_action :verify_authenticity_token
|
5
|
+
|
6
|
+
def verify
|
7
|
+
if params['hub.verify_token'] == ENV['FACEBOOK_MESSENGER_VERIFICATION_TOKEN']
|
8
|
+
render plain: params['hub.challenge'], status: 200
|
9
|
+
else
|
10
|
+
render plain: 'Wrong validation token', status: 500
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def callback
|
15
|
+
json = JSON.parse(request.body.read)
|
16
|
+
ProcessMessengerCallbackJob.perform_later(json)
|
17
|
+
render nothing: true, status: 200
|
18
|
+
rescue JSON::ParserError => e
|
19
|
+
render plain: 'Error processing callback', status: 500
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module ApplicationHelper
|
3
|
+
|
4
|
+
def send_to_messenger(params = {})
|
5
|
+
params[:class] = 'fb-send-to-messenger'
|
6
|
+
params[:messenger_app_id] = ENV['FACEBOOK_APP_ID']
|
7
|
+
params[:page_id] = ENV['FACEBOOK_PAGE_ID']
|
8
|
+
params[:data] ||= {}
|
9
|
+
params[:data][:ref] ||= 'PASS_THROUGH_PARAM'
|
10
|
+
params[:color] ||= 'blue'
|
11
|
+
params[:size] ||= 'standard'
|
12
|
+
content_tag(:div, '', params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def message_us(params = {})
|
16
|
+
params[:class] = 'fb-messengermessageus'
|
17
|
+
params[:messenger_app_id] = ENV['FACEBOOK_APP_ID']
|
18
|
+
params[:page_id] = ENV['FACEBOOK_PAGE_ID']
|
19
|
+
params[:color] ||= 'blue'
|
20
|
+
params[:size] ||= 'standard'
|
21
|
+
content_tag(:div, '', params)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>MessageQuickly</title>
|
5
|
+
<%= stylesheet_link_tag "message_quickly/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "message_quickly/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Description:
|
2
|
+
Explain the generator
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate callbacks
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
app/webhooks/authentication_callback.rb
|
9
|
+
app/webhooks/message_delivered_callback.rb
|
10
|
+
app/webhooks/message_received_callback.rb
|
11
|
+
app/webhooks/postback_callback.rb
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Generators
|
3
|
+
class CallbacksGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def copy_webhooks
|
8
|
+
|
9
|
+
copy_file "webhooks.rb", "config/initializers/webhooks.rb"
|
10
|
+
|
11
|
+
copy_file "authentication_callback.rb", "app/webhooks/authentication_callback.rb"
|
12
|
+
copy_file "message_delivered_callback.rb", "app/webhooks/message_delivered_callback.rb"
|
13
|
+
copy_file "message_received_callback.rb", "app/webhooks/message_received_callback.rb"
|
14
|
+
copy_file "postback_callback.rb", "app/webhooks/postback_callback.rb"
|
15
|
+
|
16
|
+
copy_file "process_messenger_callback_job.rb", "app/jobs/process_messenger_callback_job.rb"
|
17
|
+
copy_file "send_messenger_delivery_job.rb", "app/jobs/send_messenger_delivery_job.rb"
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class ProcessMessengerCallbackJob < ActiveJob::Base
|
2
|
+
|
3
|
+
queue_as :messaging
|
4
|
+
|
5
|
+
def perform(json)
|
6
|
+
CallbackParser.new(json.deep_dup).parse do |event|
|
7
|
+
callback_handler = CallbackRegistry.handler_for(event.webhook_name)
|
8
|
+
return unless callback_handler.present?
|
9
|
+
callback_handler.new.run(event, json)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class SendMessengerDeliveryJob < ActiveJob::Base
|
2
|
+
|
3
|
+
queue_as :messaging
|
4
|
+
|
5
|
+
def perform(hash)
|
6
|
+
MessageQuickly::Api::Client.create_from_hash(hash)
|
7
|
+
end
|
8
|
+
|
9
|
+
# To use this:
|
10
|
+
|
11
|
+
# recipient = MessageQuickly::Messaging::Recipient.new(id: '<facebook page specific id here>')
|
12
|
+
# delivery = MessageQuickly::Messaging::Delivery.new(recipient: recipient) do |delivery|
|
13
|
+
# delivery.build_message do |message|
|
14
|
+
# message.text = 'Hello'
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# SendMessengerDeliveryJob.perform_later(delivery.to_hash)
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "faraday"
|
2
|
+
|
3
|
+
module MessageQuickly
|
4
|
+
module Api
|
5
|
+
class Base
|
6
|
+
|
7
|
+
def self.client
|
8
|
+
@@client ||= Client.new do |client|
|
9
|
+
client.page_access_token = ENV['FACEBOOK_MESSENGER_PAGE_ACCESS_TOKEN']
|
10
|
+
client.page_id = ENV['FACEBOOK_MESSENGER_PAGE_ID']
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(override_client = nil)
|
15
|
+
@client = override_client || self.class.client
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "faraday"
|
2
|
+
|
3
|
+
module MessageQuickly
|
4
|
+
module Api
|
5
|
+
class Client
|
6
|
+
|
7
|
+
attr_accessor :page_access_token, :page_id
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
options.each { |key, value| instance_variable_set("@#{key}", value) }
|
11
|
+
yield self if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(request_string, params = {})
|
15
|
+
params[:access_token] = page_access_token
|
16
|
+
response = connection.get(request_string, params)
|
17
|
+
parse_json(response)
|
18
|
+
end
|
19
|
+
|
20
|
+
def post(request_string, params = {})
|
21
|
+
params[:access_token] = page_access_token
|
22
|
+
response = connection.post(request_string) do |request|
|
23
|
+
if params[:filedata].present?
|
24
|
+
request.headers['Content-Type'] = 'multipart/form-data'
|
25
|
+
request.body = params
|
26
|
+
else
|
27
|
+
request.headers['Content-Type'] = 'application/json'
|
28
|
+
request.body = JSON.generate(params)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
parse_json(response)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def parse_json(response)
|
37
|
+
json = JSON.parse(response.body)
|
38
|
+
if json['error']
|
39
|
+
case json['error']['type']
|
40
|
+
when 'OAuthException'
|
41
|
+
raise OauthException.new(json['error'])
|
42
|
+
when 'GraphMethodException'
|
43
|
+
raise GraphMethodException.new(json['error'])
|
44
|
+
else
|
45
|
+
raise FacebookApiException.new(json['error'])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
json
|
49
|
+
end
|
50
|
+
|
51
|
+
def connection(params = {})
|
52
|
+
@connection ||= Faraday.new(:url => "https://graph.facebook.com/#{MessageQuickly::FB_MESSENGER_VERSION}") do |faraday|
|
53
|
+
faraday.request :multipart # if params[:multipart]
|
54
|
+
faraday.request :url_encoded # form-encode POST params
|
55
|
+
faraday.response :logger unless Rails.env.test? # log requests to STDOUT
|
56
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Api
|
3
|
+
class FacebookApiException < StandardError
|
4
|
+
|
5
|
+
attr_reader :code, :error_data, :fbtrace_id
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@message = params['message']
|
9
|
+
@code = params['code']
|
10
|
+
@error_data = params['error_data']
|
11
|
+
@fbtrace_id = params['fbtrace_id']
|
12
|
+
super(message)
|
13
|
+
end
|
14
|
+
|
15
|
+
def message
|
16
|
+
"#{@message}: #{@error_data}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# {
|
24
|
+
# "error":{
|
25
|
+
# "message":"Invalid parameter",
|
26
|
+
# "type":"FacebookApiException",
|
27
|
+
# "code":100,
|
28
|
+
# "error_data":"No matching user found.",
|
29
|
+
# "fbtrace_id":"D2kxCybrKVw"
|
30
|
+
# }
|
31
|
+
# }
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Api
|
3
|
+
class GraphMethodException < StandardError
|
4
|
+
|
5
|
+
attr_reader :code, :fbtrace_id
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@message = params['message']
|
9
|
+
@code = params['code']
|
10
|
+
@fbtrace_id = params['fbtrace_id']
|
11
|
+
super(message)
|
12
|
+
end
|
13
|
+
|
14
|
+
def message
|
15
|
+
@message
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Api
|
3
|
+
class Messages < Base
|
4
|
+
|
5
|
+
def self.create(recipient = nil, delivery = nil, &block)
|
6
|
+
# curl -X POST -H "Content-Type: application/json" -d '{
|
7
|
+
# "recipient":{
|
8
|
+
# "id":"USER_ID"
|
9
|
+
# },
|
10
|
+
# "message":{
|
11
|
+
# "text":"hello, world!"
|
12
|
+
# }
|
13
|
+
# }' "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
|
14
|
+
|
15
|
+
# {
|
16
|
+
# "recipient_id": "1008372609250235",
|
17
|
+
# "message_id": "mid.1456970487936:c34767dfe57ee6e339"
|
18
|
+
# }
|
19
|
+
Messages.new.create(recipient, delivery, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def create(recipient = nil, delivery = nil, &block)
|
23
|
+
delivery ||= MessageQuickly::Messaging::Delivery.new(recipient: recipient)
|
24
|
+
block.call(delivery.message) if block
|
25
|
+
delivery.id = perform_call(delivery.to_hash)
|
26
|
+
delivery
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.create_from_hash(hash)
|
30
|
+
Messages.new.create_from_hash(hash)
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_from_hash(hash)
|
34
|
+
perform_call(hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def perform_call(hash)
|
40
|
+
json = @client.post("me/messages", hash)
|
41
|
+
json['message_id']
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Api
|
3
|
+
class OauthException < StandardError
|
4
|
+
|
5
|
+
attr_reader :code, :fbtrace_id
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@message = params['message']
|
9
|
+
@code = params['code']
|
10
|
+
@fbtrace_id = params['fbtrace_id']
|
11
|
+
super(message)
|
12
|
+
end
|
13
|
+
|
14
|
+
def message
|
15
|
+
@message
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Api
|
3
|
+
class ThreadSettings < Base
|
4
|
+
|
5
|
+
def self.create(message)
|
6
|
+
# curl -X POST -H "Content-Type: application/json" -d '{
|
7
|
+
# "setting_type":"call_to_actions",
|
8
|
+
# "thread_state":"new_thread",
|
9
|
+
# "call_to_actions":[
|
10
|
+
# {
|
11
|
+
# "message":{
|
12
|
+
# "text":"Welcome to My Company!"
|
13
|
+
# }
|
14
|
+
# }
|
15
|
+
# ]
|
16
|
+
# }' "https://graph.facebook.com/v2.6/<PAGE_ID>/thread_settings?access_token=<PAGE_ACCESS_TOKEN>"
|
17
|
+
ThreadSettings.new.create(message)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.delete
|
21
|
+
# curl -X POST -H "Content-Type: application/json" -d '{
|
22
|
+
# "setting_type":"call_to_actions",
|
23
|
+
# "thread_state":"new_thread",
|
24
|
+
# "call_to_actions":[]
|
25
|
+
# }' "https://graph.facebook.com/v2.6/<PAGE_ID>/thread_settings?access_token=<PAGE_ACCESS_TOKEN>"
|
26
|
+
ThreadSettings.new.delete
|
27
|
+
end
|
28
|
+
|
29
|
+
def create(message)
|
30
|
+
request_string = "#{ENV['FACEBOOK_MESSENGER_PAGE_ID']}/thread_settings"
|
31
|
+
json = @client.post(request_string, { setting_type: 'call_to_actions', thread_state: 'new_thread', call_to_actions: [{ message: { text: message } }] })
|
32
|
+
json['result'] == "Successfully added new_thread's CTAs"
|
33
|
+
end
|
34
|
+
|
35
|
+
def delete
|
36
|
+
request_string = "#{ENV['FACEBOOK_MESSENGER_PAGE_ID']}/thread_settings"
|
37
|
+
json = @client.post(request_string, { setting_type: 'call_to_actions', thread_state: 'new_thread', call_to_actions: [] })
|
38
|
+
json['result'] == "Successfully removed all new_thread's CTAs"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Api
|
3
|
+
class UserProfile < Base
|
4
|
+
|
5
|
+
def self.find(id)
|
6
|
+
# curl -X GET "https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>"
|
7
|
+
# {
|
8
|
+
# "first_name": "Peter",
|
9
|
+
# "last_name": "Chang",
|
10
|
+
# "profile_pic": "https://fbcdn-profile-a.akamaihd.net/hprofile...70ec9c19b18"
|
11
|
+
# }
|
12
|
+
UserProfile.new.find(id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def find(id)
|
16
|
+
json = @client.get(id, { fields: 'first_name,last_name,profile_pic' })
|
17
|
+
Messaging::User.new(json)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|