facebook-bot 0.1.0 → 0.1.1
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 +4 -4
- data/.gitignore +5 -0
- data/README.md +42 -41
- data/example/facebook_bot.rb +11 -0
- data/lib/facebook/bot.rb +23 -1
- data/lib/facebook/bot/config.rb +11 -0
- data/lib/facebook/bot/receiver.rb +26 -0
- data/lib/facebook/bot/space.rb +12 -0
- data/lib/facebook/bot/station.rb +17 -0
- data/lib/facebook/bot/transmitter.rb +34 -0
- data/lib/facebook/bot/version.rb +1 -1
- metadata +7 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d12d92afa1c4c2fdf37348f8555565fa6b55f6d9
|
4
|
+
data.tar.gz: 7fcb1b1a4ca389f863c9b4419237d476bb7ec7b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ffc4e940d1d57682883d1815655c03a7307c43a7315c13ee5422209475eff67add35764c65d4a53b94d7a170ac50681de2f079cd652abf34bf5ba510b17dc63
|
7
|
+
data.tar.gz: 47dcefcd546c14730354a1edcea6ef4753f5e61354d25e4d59b8c5fb327af951828687eb3220ad2335aa763e22bd6fc5a2d25df50e0a330ae6d530dfa32a89a7
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,41 +1,42 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
1
|
+
#facebook-bot
|
2
|
+
[](https://travis-ci.org/jun85664396/facebook-bot)
|
3
|
+
[](https://badge.fury.io/rb/facebook-bot)
|
4
|
+
|
5
|
+
Very easy Ruby on Rails client!! for [Facebook Messenger Platform](https://developers.facebook.com/docs/messenger-platform)
|
6
|
+
|
7
|
+
Requires Rails >= 4.2.0
|
8
|
+
|
9
|
+
##Installation
|
10
|
+
|
11
|
+
gem install 'facebook-bot'
|
12
|
+
|
13
|
+
##Quickstart
|
14
|
+
|
15
|
+
#config/initializers/facebook_bot.rb
|
16
|
+
|
17
|
+
Facebook::Bot.config do |config|
|
18
|
+
config.access_token = <ACCESS_TOKEN>
|
19
|
+
config.validation_token = <VERIFY_TOKEN>
|
20
|
+
end
|
21
|
+
##Example
|
22
|
+
|
23
|
+
[Example](https://github.com/jun85664396/facebook-bot/blob/master/example/facebook_bot.rb)
|
24
|
+
|
25
|
+
#app/models/facebook_bot.rb
|
26
|
+
|
27
|
+
class FacebookBot
|
28
|
+
Facebook::Bot.on("message") do |event, sender|
|
29
|
+
sender.reply({ text: "Reply: #{event['message']['text']}" })
|
30
|
+
end
|
31
|
+
Facebook::Bot.on("delivery") do |event, sender|
|
32
|
+
#BlahBlah
|
33
|
+
end
|
34
|
+
Facebook::Bot.on("postback") do |event, sender|
|
35
|
+
#BlahBlah
|
36
|
+
end
|
37
|
+
end
|
38
|
+

|
39
|
+
|
40
|
+
## license
|
41
|
+
|
42
|
+
MIT, see [LICENSE.txt](LICENSE.txt)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class FacebookBot
|
2
|
+
Facebook::Bot.on("message") do |event, sender|
|
3
|
+
sender.reply({ text: "Reply: #{event['message']['text']}" })
|
4
|
+
end
|
5
|
+
Facebook::Bot.on("delivery") do |event, sender|
|
6
|
+
#BlahBlah
|
7
|
+
end
|
8
|
+
Facebook::Bot.on("postback") do |event, sender|
|
9
|
+
#BlahBlah
|
10
|
+
end
|
11
|
+
end
|
data/lib/facebook/bot.rb
CHANGED
@@ -1,7 +1,29 @@
|
|
1
1
|
require "facebook/bot/version"
|
2
|
+
require "facebook/bot/config"
|
3
|
+
require "facebook/bot/transmitter"
|
4
|
+
require "facebook/bot/receiver"
|
2
5
|
|
3
6
|
module Facebook
|
4
7
|
module Bot
|
5
|
-
|
8
|
+
def self.config(&block)
|
9
|
+
if block_given?
|
10
|
+
block.call(Facebook::Bot::Config)
|
11
|
+
else
|
12
|
+
Facebook::Bot::Config
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.on(event, &block)
|
17
|
+
if %w(message postback delivery).include?(event)
|
18
|
+
if block_given?
|
19
|
+
Facebook::Bot::Receiver.define_event(event, &block)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
raise "'#{event}' Not support event."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
autoload :Space, "facebook/bot/space"
|
27
|
+
|
6
28
|
end
|
7
29
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Facebook
|
2
|
+
module Bot
|
3
|
+
class Receiver
|
4
|
+
def self.share(data)
|
5
|
+
messaging_events = data["entry"].first["messaging"]
|
6
|
+
messaging_events.each_with_index do |event, key|
|
7
|
+
if event["message"]
|
8
|
+
self.class.send(:message, event)
|
9
|
+
elsif event["postback"]
|
10
|
+
self.class.send(:postback, event)
|
11
|
+
elsif event["delivery"]
|
12
|
+
self.class.send(:delivery, event)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.define_event(event, &block)
|
18
|
+
self.class.instance_eval do
|
19
|
+
define_method(event.to_sym) do |event|
|
20
|
+
yield(event, Facebook::Bot::Transmitter.new(event["sender"]["id"]))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Facebook
|
2
|
+
module Bot
|
3
|
+
class Space < ::Rails::Engine
|
4
|
+
isolate_namespace Space
|
5
|
+
autoload :StationController, "facebook/bot/station"
|
6
|
+
Space.routes.draw do
|
7
|
+
get "/", to: "station#validation"
|
8
|
+
post "/", to: "station#receive"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Facebook
|
2
|
+
module Bot
|
3
|
+
class Space::StationController < ::ApplicationController
|
4
|
+
def validation
|
5
|
+
if params["hub.verify_token"] === Facebook::Bot::Config.verify_token
|
6
|
+
return render json: params["hub.challenge"]
|
7
|
+
end
|
8
|
+
render body: "Error, wrong validation token"
|
9
|
+
end
|
10
|
+
|
11
|
+
def receive
|
12
|
+
Facebook::Bot::Receiver.share(params)
|
13
|
+
render body: "ok"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Facebook
|
2
|
+
module Bot
|
3
|
+
class Transmitter
|
4
|
+
def initialize(sender)
|
5
|
+
@data = {
|
6
|
+
recipient: { id: sender }
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def reply(data)
|
11
|
+
@data[:message] = data
|
12
|
+
send_message(@data)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def send_message(data)
|
18
|
+
url = URI.parse("https://graph.facebook.com/v2.6/me/messages?access_token=#{Facebook::Bot::Config.access_token}")
|
19
|
+
http = Net::HTTP.new(url.host, 443)
|
20
|
+
http.use_ssl = true
|
21
|
+
begin
|
22
|
+
request = Net::HTTP::Post.new(url.request_uri)
|
23
|
+
request["Content-Type"] = "application/json"
|
24
|
+
request.body = data.to_json
|
25
|
+
response = http.request(request)
|
26
|
+
body = JSON(response.body)
|
27
|
+
return { ret: body["error"].nil?, body: body }
|
28
|
+
rescue => e
|
29
|
+
raise e
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/facebook/bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: facebook-bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JunSangPil
|
@@ -69,8 +69,14 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
|
+
- example/facebook_bot.rb
|
72
73
|
- facebook-bot.gemspec
|
73
74
|
- lib/facebook/bot.rb
|
75
|
+
- lib/facebook/bot/config.rb
|
76
|
+
- lib/facebook/bot/receiver.rb
|
77
|
+
- lib/facebook/bot/space.rb
|
78
|
+
- lib/facebook/bot/station.rb
|
79
|
+
- lib/facebook/bot/transmitter.rb
|
74
80
|
- lib/facebook/bot/version.rb
|
75
81
|
homepage: https://github.com/jun85664396/facebook-bot
|
76
82
|
licenses:
|