facebook_chat 0.0.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 +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +1 -0
- data/facebook_chat.gemspec +26 -0
- data/lib/facebook_chat.rb +6 -0
- data/lib/facebook_chat/client.rb +35 -0
- data/lib/facebook_chat/version.rb +3 -0
- data/lib/facebook_chat/x_facebook_platform.rb +50 -0
- data/spec/client_spec.rb +21 -0
- data/spec/spec_helper.rb +5 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: afcdc532ce64d1fa302edb797bf05d81b61695cf
|
4
|
+
data.tar.gz: e93f1dcf5c534f56c0705613967bcfad18584647
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51b8a9f49ad163d0afaf8635846f351c57a957bfbc2a4fcfb840902f876230305bffb65421a63378462a61985a15279609b158fcff1aac7ba3455a17b903c109
|
7
|
+
data.tar.gz: 5e2d1adbbaf73e673ef3317aec1ffc2d515a03936d0973f76dd8e0d40af872bd9055ac11fa0f7cc2ef5f72ed94328bb08462f4b51769fc8905440e703f9a3847
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kengo Tateishi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# FacebookChat
|
2
|
+
|
3
|
+
FacebookChat is a gem which send a chat message to any facebook friends using Facebook Chat API. It makes you to handle a facebook chat message more easy. If you'd like to know about Facebook Chat API, please check [Facebook Developer's page](https://developers.facebook.com/docs/chat/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'facebook_chat'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```console
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```console
|
22
|
+
$ gem install facebook_chat
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Preparation
|
28
|
+
|
29
|
+
Your facebook application should have `xmpp_login` permission to send a chat message. Please set `xmpp_login` permission in your application before go to next step.
|
30
|
+
|
31
|
+
### Configuration
|
32
|
+
|
33
|
+
First, you have to configure to Facebook Chat API Client.
|
34
|
+
|
35
|
+
If you are developing on Rails, you might want to place following code to `config/initializers/facebook_chat.rb`.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'facebook_chat'
|
39
|
+
|
40
|
+
FacebookChat::Client.configure do |config|
|
41
|
+
config.api_key = '****' # your facebook application's api key
|
42
|
+
config.host = 'chat.facebook.com' # you can omit this line. Default host value is 'chat.facebook.com'
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### Send a message
|
47
|
+
|
48
|
+
And then, create `FacebookChat::Client` instance and call `send` method.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
access_token = '****' # message sender's facebook access token
|
52
|
+
cilent = FacebookChat::Client.new(access_token)
|
53
|
+
|
54
|
+
to = '1000000000' # facebook user id you want to send a message
|
55
|
+
client.send(to, "hey, what's up?")
|
56
|
+
```
|
57
|
+
|
58
|
+
you can notice that a message is sent to the user who have id `1000000000`.
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'facebook_chat/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "facebook_chat"
|
8
|
+
spec.version = FacebookChat::VERSION
|
9
|
+
spec.authors = ["Kengo Tateishi"]
|
10
|
+
spec.email = ["embrace.ddd.flake.peace@gmail.com"]
|
11
|
+
spec.description = %q{Facebook Chat API Client}
|
12
|
+
spec.summary = %q{Facebook Chat API Client}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_dependency "rake"
|
23
|
+
spec.add_dependency "xmpp4r"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'xmpp4r'
|
2
|
+
|
3
|
+
module FacebookChat
|
4
|
+
class Client
|
5
|
+
def initialize(access_token)
|
6
|
+
@client = Jabber::Client.new(Jabber::JID.new)
|
7
|
+
@client.connect(FacebookChat::Client.configuration.host)
|
8
|
+
@client.auth_sasl(XFacebookPlatform.new(@client), access_token)
|
9
|
+
end
|
10
|
+
|
11
|
+
def send(id, message)
|
12
|
+
jid = "#{id}@#{FacebookChat::Client.configuration.host}"
|
13
|
+
message = Jabber::Message.new(jid, message)
|
14
|
+
@client.send(message)
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :configuration
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.configure
|
22
|
+
self.configuration ||= Configuration.new
|
23
|
+
yield(self.configuration)
|
24
|
+
end
|
25
|
+
|
26
|
+
class Configuration
|
27
|
+
attr_accessor :api_key, :host
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
@api_key = ''
|
31
|
+
@host = 'chat.facebook.com'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
module FacebookChat
|
4
|
+
class XFacebookPlatform < Jabber::SASL::Base
|
5
|
+
def initialize(stream)
|
6
|
+
super
|
7
|
+
|
8
|
+
challenge = {}
|
9
|
+
error = nil
|
10
|
+
stream.send(generate_auth('X-FACEBOOK-PLATFORM')) do |reply|
|
11
|
+
if reply.name == 'challenge' and reply.namespace == Jabber::SASL::NS_SASL
|
12
|
+
challenge = CGI.parse(Base64::decode64(reply.text))
|
13
|
+
else
|
14
|
+
error = reply.first_element(nil).name
|
15
|
+
end
|
16
|
+
true
|
17
|
+
end
|
18
|
+
raise error if error
|
19
|
+
|
20
|
+
@method = challenge['method'][0]
|
21
|
+
@nonce = challenge['nonce'][0]
|
22
|
+
end
|
23
|
+
|
24
|
+
def auth(access_token)
|
25
|
+
response = {
|
26
|
+
:method => @method,
|
27
|
+
:nonce => @nonce,
|
28
|
+
:access_token => access_token,
|
29
|
+
:api_key => FacebookChat::Client.configuration.api_key,
|
30
|
+
:call_id => Time.now.to_i,
|
31
|
+
:v => '1.0'
|
32
|
+
}
|
33
|
+
response_text = response.collect {|k, v| "#{k}=#{v}" }.join('&')
|
34
|
+
|
35
|
+
response = REXML::Element.new('response')
|
36
|
+
response.add_namespace Jabber::SASL::NS_SASL
|
37
|
+
response.text = Base64::encode64(response_text)
|
38
|
+
|
39
|
+
error = nil
|
40
|
+
@stream.send(response) do |reply|
|
41
|
+
if reply.name != 'success'
|
42
|
+
error = reply.first_element(nil).name
|
43
|
+
end
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
raise error if error
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'facebook_chat'
|
3
|
+
|
4
|
+
describe FacebookChat::Client do
|
5
|
+
let(:api_key) { 'api_key' }
|
6
|
+
let(:host) { 'example.com' }
|
7
|
+
|
8
|
+
before do
|
9
|
+
FacebookChat::Client.configure do |config|
|
10
|
+
config.api_key = api_key
|
11
|
+
config.host = host
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'configuration' do
|
16
|
+
it 'should have correct configuration' do
|
17
|
+
expect(FacebookChat::Client.configuration.api_key).to eq api_key
|
18
|
+
expect(FacebookChat::Client.configuration.host).to eq host
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: facebook_chat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kengo Tateishi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: xmpp4r
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Facebook Chat API Client
|
84
|
+
email:
|
85
|
+
- embrace.ddd.flake.peace@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- facebook_chat.gemspec
|
96
|
+
- lib/facebook_chat.rb
|
97
|
+
- lib/facebook_chat/client.rb
|
98
|
+
- lib/facebook_chat/version.rb
|
99
|
+
- lib/facebook_chat/x_facebook_platform.rb
|
100
|
+
- spec/client_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.0.3
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Facebook Chat API Client
|
126
|
+
test_files:
|
127
|
+
- spec/client_spec.rb
|
128
|
+
- spec/spec_helper.rb
|