facebook-messenger 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a67d299ebe188c1b50d8adcae153084eb694c4c
4
- data.tar.gz: 2b882acad66c7260c99670b8b11dcd6771c18b1f
3
+ metadata.gz: 04588f9760568902f90018f5aa3fa15f9c21f99f
4
+ data.tar.gz: 93ae73db1af321f0bcd252ebb16671298a0a437b
5
5
  SHA512:
6
- metadata.gz: 8fa27eacf08884194a942969822356c84e1de6c74d88b02451a858c5227351305a77e33d4c7eeba4dae146891b8d3c9d429924009bacb2055433db4c3947c071
7
- data.tar.gz: 9f7975f548098478e922daf12433e19c28483edde278f0b7b6ee885ca2748c4d4107c06c4e25693f640210f35f1dc407fe9e3a5f461292fdade85fc4a9249933
6
+ metadata.gz: 8ebbc1a17931bf605c9918de42057d80b346c48ac1400f52580c42bcf73c0de26150e5d9ce05442d7cdb812ac1b9f83e31e506dfbd792340882b783c65fd939a
7
+ data.tar.gz: 4f00ba96f37b46e58b9a5e8519bafc67ae8bb89a65f60f331ab5089dee63feefa9f12a92127b1f4ec80de1fda175ad20b5617473baf3d9b0eff9c42921a71add
data/README.md CHANGED
@@ -1,7 +1,14 @@
1
1
  <p align="center">
2
- <img src="https://rawgit.com/hyperoslo/facebook-messenger/master/docs/example_conversation_with_logo.png">
2
+ <img src="https://rawgit.com/hyperoslo/facebook-messenger/master/docs/conversation_with_logo.gif">
3
3
  </p>
4
4
 
5
+
6
+ [![Gem Version](https://img.shields.io/gem/v/facebook-messenger.svg?style=flat)](https://rubygems.org/gems/facebook-messenger)
7
+ [![Build Status](https://img.shields.io/travis/hyperoslo/facebook-messenger.svg?style=flat)](https://travis-ci.org/hyperoslo/facebook-messenger)
8
+ [![Dependency Status](https://img.shields.io/gemnasium/hyperoslo/facebook-messenger.svg?style=flat)](https://gemnasium.com/hyperoslo/facebook-messenger)
9
+ [![Code Climate](https://img.shields.io/codeclimate/github/hyperoslo/facebook-messenger.svg?style=flat)](https://codeclimate.com/github/hyperoslo/facebook-messenger)
10
+ [![Coverage Status](https://img.shields.io/coveralls/hyperoslo/facebook-messenger.svg?style=flat)](https://coveralls.io/r/hyperoslo/facebook-messenger)
11
+
5
12
  ## Installation
6
13
 
7
14
  $ gem install facebook-messenger
@@ -20,7 +27,7 @@ include Facebook::Messenger
20
27
 
21
28
  Bot.on :message do |message|
22
29
  message.id # => 'mid.1457764197618:41d102a3e1ae206a38'
23
- message.sender # => { id: '1008372609250235' }
30
+ message.sender # => { 'id' => '1008372609250235' }
24
31
  message.seq # => 73
25
32
  message.sent_at # => 2016-04-22 21:30:36 +0200
26
33
  message.text # => 'Hello, bot!'
@@ -98,8 +105,8 @@ When the human has selected an option, you can act on it:
98
105
 
99
106
  ```ruby
100
107
  Bot.on :postback do |postback|
101
- postback.sender # => { id: '1008372609250235' }
102
- postback.recipient # => { id: '2015573629214912' }
108
+ postback.sender # => { 'id' => '1008372609250235' }
109
+ postback.recipient # => { 'id' => '2015573629214912' }
103
110
  postback.sent_at # => 2016-04-22 21:30:36 +0200
104
111
  postback.payload # => 'EXTERMINATE'
105
112
 
@@ -118,10 +125,10 @@ embedded on a website, you will receive an `optin` event.
118
125
 
119
126
  ```ruby
120
127
  Bot.on :optin do |optin|
121
- postback.sender # => { id: '1008372609250235' }
122
- postback.recipient # => { id: '2015573629214912' }
123
- postback.sent_at # => 2016-04-22 21:30:36 +0200
124
- postback.ref # => 'CONTACT_SKYNET'
128
+ optin.sender # => { 'id' => '1008372609250235' }
129
+ optin.recipient # => { 'id' => '2015573629214912' }
130
+ optin.sent_at # => 2016-04-22 21:30:36 +0200
131
+ optin.ref # => 'CONTACT_SKYNET'
125
132
 
126
133
  Bot.deliver(
127
134
  recipient: {
@@ -132,7 +139,7 @@ Bot.on :optin do |optin|
132
139
  }
133
140
  )
134
141
  end
135
-
142
+ ```
136
143
 
137
144
  #### Message delivery receipts
138
145
 
@@ -141,8 +148,8 @@ You can stalk the human:
141
148
  ```ruby
142
149
  Bot.on :delivery do |delivery|
143
150
  delivery.ids # => 'mid.1457764197618:41d102a3e1ae206a38'
144
- delivery.sender # => { id: '1008372609250235' }
145
- delivery.recipient # => { id: '2015573629214912' }
151
+ delivery.sender # => { 'id' => '1008372609250235' }
152
+ delivery.recipient # => { 'id' => '2015573629214912' }
146
153
  delivery.at # => 2016-04-22 21:30:36 +0200
147
154
  delivery.seq # => 37
148
155
 
@@ -154,8 +161,8 @@ end
154
161
 
155
162
  ### Create an Application on Facebook
156
163
 
157
- Create an Application on [developers.facebook.com][facebook-developers] and go
158
- to the Messenger tab. Select the Page you want to install your bot on.
164
+ Create an Application on [developers.facebook.com] and go to the Messenger
165
+ tab. Select the Page you want to install your bot on.
159
166
 
160
167
  Create a new webhook, enter the domain your bot is connected to and a verify
161
168
  token of your choosing.
@@ -180,6 +187,10 @@ from Facebook:
180
187
  Facebook::Messenger::Subscriptions.subscribe
181
188
  ```
182
189
 
190
+ ### Run it
191
+
192
+ ##### ... with Rack
193
+
183
194
  The bot runs on [Rack][rack], so you hook it up like you would an ordinary
184
195
  web application:
185
196
 
@@ -195,6 +206,38 @@ run Facebook::Messenger::Server
195
206
  $ rackup
196
207
  ```
197
208
 
209
+ ##### ... with Rails
210
+
211
+ Rails doesn't give you much that you'll need for a bot, but if you have an
212
+ existing application that you'd like to launch it from or just like Rails
213
+ a lot, you can mount it:
214
+
215
+ ```ruby
216
+ # config/routes.rb
217
+
218
+ Rails.application.routes.draw do
219
+ # ...
220
+
221
+ mount Facebook::Messenger::Server, at: 'bot'
222
+ end
223
+ ```
224
+
225
+ We suggest that you put your bot code in `app/bot`.
226
+
227
+ ```ruby
228
+ # app/bot/example.rb
229
+
230
+ include Facebook::Messenger
231
+
232
+ Bot.on :message do |message|
233
+ Bot.deliver(
234
+ recipient: message.sender,
235
+ message: {
236
+ text: 'Hello, human!'
237
+ }
238
+ )
239
+ end
240
+ ```
198
241
 
199
242
  ## Development
200
243
 
@@ -225,6 +268,6 @@ If you're using Facebook Messenger, we probably want to [hire you].
225
268
  [MIT License]: http://opensource.org/licenses/MIT
226
269
  [rubygems.org]: https://rubygems.org
227
270
  [message-documentation]: https://developers.facebook.com/docs/messenger-platform/send-api-reference#request
228
- [facebook-developers]: https://developers.facebook.com
271
+ [developers.facebook.com]: https://developers.facebook.com/
229
272
  [rack]: https://github.com/rack/rack
230
273
  [send-to-messenger-plugin]: https://developers.facebook.com/docs/messenger-platform/plugin-reference
@@ -60,7 +60,7 @@ module Facebook
60
60
  # event - A String describing a Messenger event.
61
61
  # args - Arguments to pass to the hook.
62
62
  def trigger(event, *args)
63
- @hooks.fetch(event).call(*args)
63
+ hooks.fetch(event).call(*args)
64
64
  rescue KeyError
65
65
  $stderr.puts "Ignoring #{event} (no hook registered)"
66
66
  end
@@ -8,19 +8,21 @@ module Facebook
8
8
  # The Incoming module parses and abstracts incoming requests from
9
9
  # Facebook Messenger.
10
10
  module Incoming
11
+ EVENTS = {
12
+ 'message' => Message,
13
+ 'delivery' => Delivery,
14
+ 'postback' => Postback,
15
+ 'optin' => Optin
16
+ }.freeze
17
+
11
18
  # Parse the given payload.
12
19
  #
13
20
  # payload - A Hash describing a payload from Facebook.
14
21
  #
15
22
  # * https://developers.facebook.com/docs/messenger-platform/webhook-reference
16
23
  def self.parse(payload)
17
- {
18
- 'message' => Message,
19
- 'delivery' => Delivery,
20
- 'postback' => Postback,
21
- 'optin' => Optin
22
- }.each do |key, klass|
23
- return klass.new(payload) if payload.key? key
24
+ EVENTS.each do |event, klass|
25
+ return klass.new(payload) if payload.key?(event)
24
26
  end
25
27
 
26
28
  raise UnknownPayload, payload
@@ -6,48 +6,46 @@ module Facebook
6
6
  # This module holds the server that processes incoming messages from the
7
7
  # Facebook Messenger Platform.
8
8
  class Server
9
- class << self
10
- def call(env)
11
- @request = Rack::Request.new(env)
12
- @response = Rack::Response.new
13
-
14
- case @request.request_method
15
- when 'GET' then verify
16
- when 'POST' then receive
17
- else method_not_allowed
18
- end
19
- end
9
+ def self.call(env)
10
+ new.call(env)
11
+ end
20
12
 
21
- def verify
22
- verify_token = Facebook::Messenger.config.verify_token
13
+ def call(env)
14
+ @request = Rack::Request.new(env)
15
+ @response = Rack::Response.new
23
16
 
24
- if @request.params['hub.verify_token'] == verify_token
25
- @response.write @request.params['hub.challenge']
26
- else
27
- @response.write 'Error; wrong verify token'
28
- end
29
-
30
- @response.finish
17
+ case
18
+ when @request.get? then verify
19
+ when @request.post? then receive
20
+ else @response.status = 405
31
21
  end
32
22
 
33
- def receive
34
- hash = JSON.parse(@request.body.read)
35
- # Facebook may batch several items in the 'entry' array during
36
- # periods of high load.
37
- hash['entry'].each do |entry|
38
- # Facebook may batch several items in the 'messaging' array during
39
- # periods of high load.
40
- entry['messaging'].each do |messaging|
41
- Facebook::Messenger::Bot.receive(messaging)
42
- end
43
- end
23
+ @response.finish
24
+ end
44
25
 
45
- @response.finish
26
+ def verify
27
+ if @request['hub.verify_token'] == verify_token
28
+ @response.write @request['hub.challenge']
29
+ else
30
+ @response.write 'Error; wrong verify token'
46
31
  end
32
+ end
33
+
34
+ def verify_token
35
+ Facebook::Messenger.config.verify_token
36
+ end
47
37
 
48
- def method_not_allowed
49
- @response.status = 405
50
- @response.finish
38
+ def receive
39
+ hash = JSON.parse(@request.body.read)
40
+
41
+ # Facebook may batch several items in the 'entry' array during
42
+ # periods of high load.
43
+ hash['entry'].each do |entry|
44
+ # Facebook may batch several items in the 'messaging' array during
45
+ # periods of high load.
46
+ entry['messaging'].each do |messaging|
47
+ Facebook::Messenger::Bot.receive(messaging)
48
+ end
51
49
  end
52
50
  end
53
51
  end
@@ -1,5 +1,5 @@
1
1
  module Facebook
2
2
  module Messenger
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook-messenger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Gorset
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-24 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -114,14 +114,28 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '10.0'
117
+ version: '11.0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '10.0'
124
+ version: '11.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: coveralls
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: pry
127
141
  requirement: !ruby/object:Gem::Requirement