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
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
require "message_quickly/messaging/base"
|
4
|
+
require "message_quickly/messaging/attachment"
|
5
|
+
require "message_quickly/messaging/image_attachment"
|
6
|
+
require "message_quickly/messaging/template_attachment"
|
7
|
+
require "message_quickly/messaging/element"
|
8
|
+
require "message_quickly/messaging/button"
|
9
|
+
require "message_quickly/messaging/web_url_button"
|
10
|
+
require "message_quickly/messaging/postback_button"
|
11
|
+
require "message_quickly/messaging/button_template_attachment"
|
12
|
+
require "message_quickly/messaging/generic_template_attachment"
|
13
|
+
|
14
|
+
require "message_quickly/messaging/receipt/address"
|
15
|
+
require "message_quickly/messaging/receipt/adjustment"
|
16
|
+
require "message_quickly/messaging/receipt/element"
|
17
|
+
require "message_quickly/messaging/receipt/summary"
|
18
|
+
require "message_quickly/messaging/receipt_template_attachment"
|
19
|
+
|
20
|
+
require "message_quickly/messaging/delivery"
|
21
|
+
require "message_quickly/messaging/message"
|
22
|
+
require "message_quickly/messaging/entry"
|
23
|
+
require "message_quickly/messaging/user"
|
24
|
+
require "message_quickly/messaging/recipient"
|
25
|
+
require "message_quickly/messaging/sender"
|
26
|
+
|
27
|
+
require "message_quickly/messaging/event"
|
28
|
+
require "message_quickly/messaging/optin_event"
|
29
|
+
require "message_quickly/messaging/delivery_event"
|
30
|
+
require "message_quickly/messaging/message_event"
|
31
|
+
require "message_quickly/messaging/postback_event"
|
32
|
+
|
33
|
+
module MessageQuickly
|
34
|
+
class CallbackParser
|
35
|
+
|
36
|
+
def initialize(json)
|
37
|
+
@json = json
|
38
|
+
end
|
39
|
+
|
40
|
+
WEBHOOK_LOOKUP = {
|
41
|
+
optin: MessageQuickly::Messaging::OptinEvent,
|
42
|
+
postback: MessageQuickly::Messaging::PostbackEvent,
|
43
|
+
delivery: MessageQuickly::Messaging::DeliveryEvent,
|
44
|
+
message: MessageQuickly::Messaging::MessageEvent
|
45
|
+
}
|
46
|
+
|
47
|
+
def parse
|
48
|
+
events = []
|
49
|
+
process_entry_json(@json['entry']) do |params|
|
50
|
+
WEBHOOK_LOOKUP.keys.each do |key|
|
51
|
+
if params[:messaging][key.to_s]
|
52
|
+
events << WEBHOOK_LOOKUP[key].new(params[:messaging])
|
53
|
+
break
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
events.each { |event| yield event if block_given? }
|
58
|
+
events
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def process_entry_json(json)
|
64
|
+
json.each do |entry_json|
|
65
|
+
entry = Messaging::Entry.new(entry_json)
|
66
|
+
entry_json['messaging'].each do |event_json|
|
67
|
+
sender = Messaging::Sender.new(event_json['sender'])
|
68
|
+
recipient = Messaging::Recipient.new(event_json['recipient'])
|
69
|
+
timestamp = event_json['timestamp']
|
70
|
+
yield callback_params(entry, sender, recipient, timestamp, event_json)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def callback_params(entry, sender, recipient, timestamp, event_json)
|
76
|
+
{
|
77
|
+
entry: entry,
|
78
|
+
sender: sender,
|
79
|
+
recipient: recipient,
|
80
|
+
timestamp: timestamp,
|
81
|
+
messaging: event_json
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module MessageQuickly
|
4
|
+
class CallbackRegistry
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
attr_reader :callbacks
|
9
|
+
|
10
|
+
def register(klass)
|
11
|
+
@callbacks[klass.new.callback_name] = klass.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def handler_for(webhook_name)
|
15
|
+
@callbacks[webhook_name]&.constantize
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
extend ClassMethods
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
CallbackRegistry.class_eval do
|
25
|
+
@callbacks = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
|
5
|
+
isolate_namespace MessageQuickly
|
6
|
+
|
7
|
+
initializer 'message_quickly.action_controller' do |app|
|
8
|
+
ActiveSupport.on_load :action_controller do
|
9
|
+
helper MessageQuickly::ApplicationHelper
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
config.generators do |g|
|
14
|
+
g.test_framework :rspec, :fixture => false
|
15
|
+
g.assets false
|
16
|
+
g.helper false
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class ButtonTemplateAttachment < TemplateAttachment
|
4
|
+
|
5
|
+
attr_accessor :text, :buttons
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
self.buttons ||= []
|
9
|
+
params['template_type'] ||= 'button'
|
10
|
+
super(params)
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_button(button_type)
|
14
|
+
case button_type
|
15
|
+
when :web_url
|
16
|
+
button = WebUrlButton.new
|
17
|
+
when :postback
|
18
|
+
button = PostbackButton.new
|
19
|
+
end
|
20
|
+
buttons << button.tap { |button| yield button }
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
{
|
25
|
+
type: type,
|
26
|
+
payload: {
|
27
|
+
template_type: template_type,
|
28
|
+
text: text,
|
29
|
+
buttons: buttons.collect { |button| button.to_hash }
|
30
|
+
}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# "attachment":{
|
39
|
+
# "type":"template",
|
40
|
+
# "payload":{
|
41
|
+
# "template_type":"button",
|
42
|
+
# "text":"What do you want to do next?",
|
43
|
+
# "buttons":[
|
44
|
+
# {
|
45
|
+
# "type":"web_url",
|
46
|
+
# "url":"https://petersapparel.parseapp.com",
|
47
|
+
# "title":"Show Website"
|
48
|
+
# },
|
49
|
+
# {
|
50
|
+
# "type":"postback",
|
51
|
+
# "title":"Start Chatting",
|
52
|
+
# "payload":"USER_DEFINED_PAYLOAD"
|
53
|
+
# }
|
54
|
+
# ]
|
55
|
+
# }
|
56
|
+
# }
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class Delivery
|
4
|
+
|
5
|
+
attr_accessor :id, :recipient, :message
|
6
|
+
attr_accessor :notification_type # must be of values: REGULAR, SILENT_PUSH, NO_PUSH
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
self.message ||= MessageQuickly::Messaging::Message.new
|
10
|
+
options.each { |key, value| instance_variable_set("@#{key}", value) }
|
11
|
+
yield self if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def build_message
|
15
|
+
yield self.message
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_hash
|
19
|
+
hash = {}
|
20
|
+
hash.merge!(recipient: recipient.to_hash) if recipient
|
21
|
+
hash.merge!(message: message.to_hash) if message
|
22
|
+
hash.merge!(notification_type: notification_type) if notification_type
|
23
|
+
hash.merge!(filedata: Faraday::UploadIO.new(message.attachment.file, message.attachment.file_type)) if message.attachment && message.attachment.file?
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "message_quickly/messaging/event"
|
2
|
+
|
3
|
+
module MessageQuickly
|
4
|
+
module Messaging
|
5
|
+
class DeliveryEvent < Event
|
6
|
+
|
7
|
+
attr_reader :mids, :watermark, :seq
|
8
|
+
|
9
|
+
def initialize(params = {})
|
10
|
+
initialize_params(params['delivery'])
|
11
|
+
super(params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def webhook_name
|
15
|
+
:message_deliveries
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# {
|
23
|
+
# "object":"page",
|
24
|
+
# "entry":[
|
25
|
+
# {
|
26
|
+
# "id":PAGE_ID,
|
27
|
+
# "time":1458668856451,
|
28
|
+
# "messaging":[
|
29
|
+
# {
|
30
|
+
# "sender":{
|
31
|
+
# "id":USER_ID
|
32
|
+
# },
|
33
|
+
# "recipient":{
|
34
|
+
# "id":PAGE_ID
|
35
|
+
# },
|
36
|
+
# "delivery":{
|
37
|
+
# "mids":[
|
38
|
+
# "mid.1458668856218:ed81099e15d3f4f233"
|
39
|
+
# ],
|
40
|
+
# "watermark":1458668856253,
|
41
|
+
# "seq":37
|
42
|
+
# }
|
43
|
+
# }
|
44
|
+
# ]
|
45
|
+
# }
|
46
|
+
# ]
|
47
|
+
# }
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class Element < MessageQuickly::Messaging::Base
|
4
|
+
|
5
|
+
attr_accessor :title, :image_url, :subtitle, :buttons
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
self.buttons ||= []
|
9
|
+
super(params)
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_button(button_type)
|
13
|
+
case button_type
|
14
|
+
when :web_url
|
15
|
+
button = WebUrlButton.new
|
16
|
+
when :postback
|
17
|
+
button = PostbackButton.new
|
18
|
+
end
|
19
|
+
buttons << button.tap { |button| yield button }
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_hash
|
23
|
+
{
|
24
|
+
title: title,
|
25
|
+
image_url: image_url,
|
26
|
+
subtitle: subtitle,
|
27
|
+
buttons: buttons.collect { |button| button.to_hash }
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class Event < Base
|
4
|
+
|
5
|
+
attr_reader :entry, :sender, :recipient, :timestamp
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@sender = Sender.new(params.delete("sender"))
|
9
|
+
@recipient = Recipient.new(params.delete("recipient"))
|
10
|
+
super(params)
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def initialize_params(params)
|
16
|
+
params.each { |key, value| instance_variable_set("@#{key}", value) }
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class GenericTemplateAttachment < TemplateAttachment
|
4
|
+
|
5
|
+
attr_accessor :text, :elements
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
self.elements ||= []
|
9
|
+
params['template_type'] ||= 'generic'
|
10
|
+
super(params)
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_element
|
14
|
+
elements << Element.new.tap { |element| yield element }
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_hash
|
18
|
+
{
|
19
|
+
type: type,
|
20
|
+
payload: {
|
21
|
+
template_type: template_type,
|
22
|
+
elements: elements.collect { |element| element.to_hash }
|
23
|
+
}
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# "attachment":{
|
32
|
+
# "type":"template",
|
33
|
+
# "payload":{
|
34
|
+
# "template_type":"generic",
|
35
|
+
# "elements":[
|
36
|
+
# {
|
37
|
+
# "title":"Classic White T-Shirt",
|
38
|
+
# "image_url":"http://petersapparel.parseapp.com/img/item100-thumb.png",
|
39
|
+
# "subtitle":"Soft white cotton t-shirt is back in style",
|
40
|
+
# "buttons":[
|
41
|
+
# {
|
42
|
+
# "type":"web_url",
|
43
|
+
# "url":"https://petersapparel.parseapp.com/view_item?item_id=100",
|
44
|
+
# "title":"View Item"
|
45
|
+
# },
|
46
|
+
# {
|
47
|
+
# "type":"web_url",
|
48
|
+
# "url":"https://petersapparel.parseapp.com/buy_item?item_id=100",
|
49
|
+
# "title":"Buy Item"
|
50
|
+
# },
|
51
|
+
# {
|
52
|
+
# "type":"postback",
|
53
|
+
# "title":"Bookmark Item",
|
54
|
+
# "payload":"USER_DEFINED_PAYLOAD_FOR_ITEM100"
|
55
|
+
# }
|
56
|
+
# ]
|
57
|
+
# },
|
58
|
+
# {
|
59
|
+
# "title":"Classic Grey T-Shirt",
|
60
|
+
# "image_url":"http://petersapparel.parseapp.com/img/item101-thumb.png",
|
61
|
+
# "subtitle":"Soft gray cotton t-shirt is back in style",
|
62
|
+
# "buttons":[
|
63
|
+
# {
|
64
|
+
# "type":"web_url",
|
65
|
+
# "url":"https://petersapparel.parseapp.com/view_item?item_id=101",
|
66
|
+
# "title":"View Item"
|
67
|
+
# },
|
68
|
+
# {
|
69
|
+
# "type":"web_url",
|
70
|
+
# "url":"https://petersapparel.parseapp.com/buy_item?item_id=101",
|
71
|
+
# "title":"Buy Item"
|
72
|
+
# },
|
73
|
+
# {
|
74
|
+
# "type":"postback",
|
75
|
+
# "title":"Bookmark Item",
|
76
|
+
# "payload":"USER_DEFINED_PAYLOAD_FOR_ITEM101"
|
77
|
+
# }
|
78
|
+
# ]
|
79
|
+
# }
|
80
|
+
# ]
|
81
|
+
# }
|
82
|
+
# }
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class ImageAttachment < Attachment
|
4
|
+
|
5
|
+
attr_accessor :url, :file, :file_type
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
params['type'] ||= 'image'
|
9
|
+
super(params)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash
|
13
|
+
if file?
|
14
|
+
{ type: type, payload: { _: '' } } # cannot send empty hash
|
15
|
+
else
|
16
|
+
{ type: type, payload: { url: url } }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def file?
|
21
|
+
file.present? && file_type.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# "attachments":[
|
29
|
+
# {
|
30
|
+
# "type":"image",
|
31
|
+
# "payload":{
|
32
|
+
# "url":"IMAGE_URL"
|
33
|
+
# }
|
34
|
+
# }
|
35
|
+
# ]
|
36
|
+
|
37
|
+
# curl \
|
38
|
+
# -F recipient='{"id":"USER_ID"}' \
|
39
|
+
# -F message='{"attachment":{"type":"image", "payload":{}}}' \
|
40
|
+
# -F filedata=@/tmp/testpng.png \
|
41
|
+
# "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class Message
|
4
|
+
|
5
|
+
attr_accessor :text, :attachment
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
options.each { |key, value| instance_variable_set("@#{key}", value) }
|
9
|
+
yield self if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_attachment(attachment_type)
|
13
|
+
case attachment_type
|
14
|
+
when :image
|
15
|
+
self.attachment = MessageQuickly::Messaging::ImageAttachment.new
|
16
|
+
when :generic_template
|
17
|
+
self.attachment = MessageQuickly::Messaging::GenericTemplateAttachment.new
|
18
|
+
when :button_template
|
19
|
+
self.attachment = MessageQuickly::Messaging::ButtonTemplateAttachment.new
|
20
|
+
when :receipt_template
|
21
|
+
self.attachment = MessageQuickly::Messaging::ReceiptTemplateAttachment.new
|
22
|
+
end
|
23
|
+
yield attachment if block_given?
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_hash
|
27
|
+
if attachment
|
28
|
+
{ attachment: attachment.to_hash }
|
29
|
+
else
|
30
|
+
{ text: text }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "message_quickly/messaging/event"
|
2
|
+
|
3
|
+
module MessageQuickly
|
4
|
+
module Messaging
|
5
|
+
class MessageEvent < Event
|
6
|
+
|
7
|
+
attr_reader :mid, :seq, :text, :attachments
|
8
|
+
|
9
|
+
def initialize(params = {})
|
10
|
+
initialize_params(params['message'])
|
11
|
+
super(params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def webhook_name
|
15
|
+
:messages
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def initialize_params(params)
|
21
|
+
@attachments = params.delete('attachments').collect { |attachment_params| Attachment.new(attachment_params) } if params['attachments']
|
22
|
+
super(params)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# {
|
30
|
+
# "object":"page",
|
31
|
+
# "entry":[
|
32
|
+
# {
|
33
|
+
# "id":PAGE_ID,
|
34
|
+
# "time":1458696618911,
|
35
|
+
# "messaging":[
|
36
|
+
# {
|
37
|
+
# "sender":{
|
38
|
+
# "id":USER_ID
|
39
|
+
# },
|
40
|
+
# "recipient":{
|
41
|
+
# "id":PAGE_ID
|
42
|
+
# },
|
43
|
+
# "timestamp":1458696618268,
|
44
|
+
# "message":{
|
45
|
+
# "mid":"mid.1458696618141:b4ef9d19ec21086067",
|
46
|
+
# "seq":51,
|
47
|
+
# "attachments":[
|
48
|
+
# {
|
49
|
+
# "type":"image",
|
50
|
+
# "payload":{
|
51
|
+
# "url":"IMAGE_URL"
|
52
|
+
# }
|
53
|
+
# }
|
54
|
+
# ]
|
55
|
+
# }
|
56
|
+
# }
|
57
|
+
# ]
|
58
|
+
# }
|
59
|
+
# ]
|
60
|
+
# }
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "message_quickly/messaging/event"
|
2
|
+
|
3
|
+
module MessageQuickly
|
4
|
+
module Messaging
|
5
|
+
class OptinEvent < Event
|
6
|
+
|
7
|
+
attr_reader :ref
|
8
|
+
|
9
|
+
def initialize(params = {})
|
10
|
+
initialize_params(params['optin'])
|
11
|
+
super(params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def webhook_name
|
15
|
+
:messaging_optins
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# {
|
23
|
+
# "object":"page",
|
24
|
+
# "entry":[
|
25
|
+
# {
|
26
|
+
# "id":PAGE_ID,
|
27
|
+
# "time":12341,
|
28
|
+
# "messaging":[
|
29
|
+
# {
|
30
|
+
# "sender":{
|
31
|
+
# "id":USER_ID
|
32
|
+
# },
|
33
|
+
# "recipient":{
|
34
|
+
# "id":PAGE_ID
|
35
|
+
# },
|
36
|
+
# "timestamp":1234567890,
|
37
|
+
# "optin":{
|
38
|
+
# "ref":"PASS_THROUGH_PARAM"
|
39
|
+
# }
|
40
|
+
# }
|
41
|
+
# ]
|
42
|
+
# }
|
43
|
+
# ]
|
44
|
+
# }
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module MessageQuickly
|
2
|
+
module Messaging
|
3
|
+
class PostbackButton < Button
|
4
|
+
|
5
|
+
attr_accessor :payload
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
params['type'] ||= 'postback'
|
9
|
+
super(params)
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash
|
13
|
+
{ type: type, title: title, payload: payload }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# {
|
21
|
+
# "type":"postback",
|
22
|
+
# "title":"Start Chatting",
|
23
|
+
# "payload":"USER_DEFINED_PAYLOAD"
|
24
|
+
# }
|