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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6337b7f9dfde56d57e2b8a2b17fa50c6f744b140
4
- data.tar.gz: b4ad7a62f8f9f3d7bac14b9bb14c97920b811825
3
+ metadata.gz: d12d92afa1c4c2fdf37348f8555565fa6b55f6d9
4
+ data.tar.gz: 7fcb1b1a4ca389f863c9b4419237d476bb7ec7b3
5
5
  SHA512:
6
- metadata.gz: a0e0653119e032e323cde003081df43dc5641878b265d47c1f4da9d4e6bcb4374a0377c269127baf63491c7178c7b6aa2bbce78fe8a944711012a0c7eaa10de8
7
- data.tar.gz: 86890e07910c15ae4c5b65ba58e478d0e7f78d3eb2d43721c12f6628f0d44c6a1fb07dd9c6ed84cf5bd6889268da92f0db76f3efcf8324d0e32d20a78744f072
6
+ metadata.gz: 8ffc4e940d1d57682883d1815655c03a7307c43a7315c13ee5422209475eff67add35764c65d4a53b94d7a170ac50681de2f079cd652abf34bf5ba510b17dc63
7
+ data.tar.gz: 47dcefcd546c14730354a1edcea6ef4753f5e61354d25e4d59b8c5fb327af951828687eb3220ad2335aa763e22bd6fc5a2d25df50e0a330ae6d530dfa32a89a7
data/.gitignore CHANGED
@@ -7,3 +7,8 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/README.md CHANGED
@@ -1,41 +1,42 @@
1
- # Facebook::Bot
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/facebook/bot`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'facebook-bot'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install facebook-bot
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/facebook-bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
-
37
-
38
- ## License
39
-
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
1
+ #facebook-bot
2
+ [![Build Status](https://travis-ci.org/jun85664396/facebook-bot.svg?branch=master)](https://travis-ci.org/jun85664396/facebook-bot)
3
+ [![Gem Version](https://badge.fury.io/rb/facebook-bot.svg)](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
+ ![Ex](https://camo.githubusercontent.com/2452b2ca2f748f2695e545c5c14e70356df5d673/68747470733a2f2f692e696d6775722e636f6d2f59544d4f5967362e676966)
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
@@ -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
- # Your code goes here...
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,11 @@
1
+ module Facebook
2
+ module Bot
3
+ module Config
4
+ class << self
5
+ attr_accessor :access_token
6
+
7
+ attr_accessor :validation_token
8
+ end
9
+ end
10
+ end
11
+ 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
@@ -1,5 +1,5 @@
1
1
  module Facebook
2
2
  module Bot
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
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.0
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: