line-bot-lite 0.1.1 → 0.2.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 +4 -4
- data/README.md +42 -7
- data/lib/line/bot.rb +4 -1
- data/lib/line/bot/certentials.rb +1 -1
- data/lib/line/bot/client.rb +4 -0
- data/lib/line/bot/event.rb +46 -0
- data/lib/line/bot/message.rb +6 -0
- data/lib/line/bot/message/base.rb +29 -0
- data/lib/line/bot/message/content_type.rb +16 -0
- data/lib/line/bot/message/recipient_type.rb +9 -0
- data/lib/line/bot/message/text.rb +19 -0
- data/lib/line/bot/request.rb +1 -5
- data/lib/line/bot/response.rb +35 -0
- data/lib/line/bot/version.rb +1 -1
- data/line-bot.gemspec +2 -0
- metadata +37 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05573f2631ba35fde97b47a5ca9c15060a9b8559
|
4
|
+
data.tar.gz: e72f0fc487509829246bcbb68ff18c9c089ecdeb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37f2058edb8705b2a510ca621448b9ac5f15aaed172760769fac604ae121120ce829694249307d38e89424e89483bf60ef1763d80ba045849ff15ccf60cb400b
|
7
|
+
data.tar.gz: 0e9becc3174b06ff0fbed98f05948c1ee2360fe0b2b448368419c27c70fa8e76af051487b24b4dce3ee6f2fc972eda533cb4d3f49cb048e8e157ff9ba8f02de2
|
data/README.md
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# Line Bot Lite
|
2
2
|
|
3
|
+
[](https://rubygems.org/gems/line-bot-lite)
|
3
4
|
[](https://travis-ci.org/elct9620/line-bot)
|
4
5
|
[](https://coveralls.io/github/elct9620/line-bot?branch=master)
|
6
|
+
[](https://codeclimate.com/github/elct9620/line-bot)
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
TODO: Delete this and the text above, and describe your gem
|
8
|
+
Yet another line bot SDK implement. This version use all ruby builtin library and without dependencies problem like `rack` version is lock down to `>= 0`
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
14
14
|
```ruby
|
15
|
-
gem 'line-bot'
|
15
|
+
gem 'line-bot-lite'
|
16
16
|
```
|
17
17
|
|
18
18
|
And then execute:
|
@@ -21,11 +21,46 @@ And then execute:
|
|
21
21
|
|
22
22
|
Or install it yourself as:
|
23
23
|
|
24
|
-
$ gem install line-bot
|
24
|
+
$ gem install line-bot-lite
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
|
28
|
+
Current only implemented `send_text` method, and still need refactor to support all types message.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'line/bot'
|
32
|
+
require 'json'
|
33
|
+
|
34
|
+
def client
|
35
|
+
@client ||= Line::Bot::Client.new(
|
36
|
+
channel_id: ENV['LINE_CHANNEL_ID'],
|
37
|
+
channel_secret: ENV['LINE_CHANNEL_SECRET'],
|
38
|
+
channel_mid: ENV['LINE_CHANNEL_MID']
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
post '/callback' do
|
43
|
+
signature = request.env['HTTP_X_LINE_CHANNELSIGNATURE']
|
44
|
+
unless client.certentials.validate_signature?(request.body.read, signature)
|
45
|
+
error 400 do 'Bad Request' end
|
46
|
+
end
|
47
|
+
|
48
|
+
request.body.rewind
|
49
|
+
json = JSON.parse(request.body.read)
|
50
|
+
result = json['result']
|
51
|
+
|
52
|
+
result.each do |message|
|
53
|
+
case message['eventType']
|
54
|
+
when Line::Bot::Receive::EventType::MESSAGE.to_s
|
55
|
+
client.send_text(message['content']['from'], message['content']['text'])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
"OK"
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
WARNING: The API will changed until it stable, and next release will provide response parser helper.
|
29
64
|
|
30
65
|
## Development
|
31
66
|
|
@@ -35,5 +70,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
35
70
|
|
36
71
|
## Contributing
|
37
72
|
|
38
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/line-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.
|
39
74
|
|
data/lib/line/bot.rb
CHANGED
@@ -4,5 +4,8 @@ require 'line/bot/client'
|
|
4
4
|
require 'line/bot/certentials'
|
5
5
|
require 'line/bot/http'
|
6
6
|
require 'line/bot/request'
|
7
|
-
require 'line/bot/
|
7
|
+
require 'line/bot/response'
|
8
8
|
require 'line/bot/event_type'
|
9
|
+
require 'line/bot/event'
|
10
|
+
require 'line/bot/message'
|
11
|
+
require 'line/bot/api'
|
data/lib/line/bot/certentials.rb
CHANGED
@@ -26,7 +26,7 @@ module Line
|
|
26
26
|
payload.values.all?
|
27
27
|
end
|
28
28
|
|
29
|
-
def validate_signature
|
29
|
+
def validate_signature(body = "", channel_signature)
|
30
30
|
return false unless !channel_signature.nil? && valid?
|
31
31
|
|
32
32
|
hash = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, channel_secret, body)
|
data/lib/line/bot/client.rb
CHANGED
@@ -10,6 +10,10 @@ module Line
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def send_text(to_mid, message)
|
13
|
+
send_message(to_mid, Message::Text.new(text: message))
|
14
|
+
end
|
15
|
+
|
16
|
+
def send_message(to_mid, message)
|
13
17
|
request = Request.new do |config|
|
14
18
|
config.to_mid = to_mid
|
15
19
|
config.certentials = @certentials
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
class Event
|
4
|
+
|
5
|
+
attr_reader :event_type, :content
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
options.each do |key, value|
|
9
|
+
instance_variable_set("@#{key}", value)
|
10
|
+
end
|
11
|
+
|
12
|
+
@content = parse_content
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
!@event_type.nil? && !@content.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def parse_content
|
22
|
+
case @event_type
|
23
|
+
when Receive::EventType::MESSAGE
|
24
|
+
return parse_message_data
|
25
|
+
when Receive::EventType::OPERATION
|
26
|
+
return parse_operation_data
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_message_data
|
31
|
+
return if @data.nil?
|
32
|
+
case @data['contentType']
|
33
|
+
when Message::ContentType::TEXT
|
34
|
+
return Message::Text.new(text: @data['text'])
|
35
|
+
# TODO: Implement other type message type
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_operation_data
|
40
|
+
# TODO: Implement operation content
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
module Message
|
4
|
+
class Base < Hash
|
5
|
+
attr_reader :recipient_type
|
6
|
+
|
7
|
+
def initialize(attrs = {})
|
8
|
+
self.merge!(attrs)
|
9
|
+
end
|
10
|
+
|
11
|
+
def recipient_type
|
12
|
+
self[:recipient_type] ||= RecipientType::USER
|
13
|
+
end
|
14
|
+
|
15
|
+
def event_type
|
16
|
+
Line::Bot::EventType::MESSAGE
|
17
|
+
end
|
18
|
+
|
19
|
+
def content
|
20
|
+
raise NotImplementedError, "Should implement this method in child class"
|
21
|
+
end
|
22
|
+
|
23
|
+
def valid?
|
24
|
+
raise NotImplementedError, "Should implement this method in child class"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
module Message
|
4
|
+
class Text < Base
|
5
|
+
def content
|
6
|
+
{
|
7
|
+
contentType: ContentType::TEXT,
|
8
|
+
toType: recipient_type,
|
9
|
+
text: self[:text]
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
!self[:text].nil?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/line/bot/request.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
class Response < Array
|
6
|
+
def initialize(data = "")
|
7
|
+
json = JSON.parse(data)
|
8
|
+
self.concat(json['result'])
|
9
|
+
parse_event
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def parse_event
|
15
|
+
self.map! do |event|
|
16
|
+
create_event(event)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_event(event_data)
|
21
|
+
case event_data['eventType'].to_i
|
22
|
+
when Receive::EventType::MESSAGE
|
23
|
+
type = Receive::EventType::MESSAGE
|
24
|
+
when Receive::EventType::OPERATION
|
25
|
+
type = Receive::EventType::OPERATION
|
26
|
+
end
|
27
|
+
|
28
|
+
Event.new(
|
29
|
+
event_type: type,
|
30
|
+
data: event_data['content']
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/line/bot/version.rb
CHANGED
data/line-bot.gemspec
CHANGED
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency "rspec", "~> 3.0"
|
24
24
|
spec.add_development_dependency "fuubar", "~> 2.0"
|
25
25
|
spec.add_development_dependency "coveralls", "~> 0.8"
|
26
|
+
spec.add_development_dependency "simplecov", "~> 0.11"
|
27
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.5"
|
26
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: line-bot-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 蒼時弦也
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.11'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.11'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: codeclimate-test-reporter
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.5'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.5'
|
83
111
|
description: " Line Bot API usuall depend on rack but it cause newer rack cannot working
|
84
112
|
correctly, so I create yet another library for it. "
|
85
113
|
email:
|
@@ -101,9 +129,16 @@ files:
|
|
101
129
|
- lib/line/bot/api.rb
|
102
130
|
- lib/line/bot/certentials.rb
|
103
131
|
- lib/line/bot/client.rb
|
132
|
+
- lib/line/bot/event.rb
|
104
133
|
- lib/line/bot/event_type.rb
|
105
134
|
- lib/line/bot/http.rb
|
135
|
+
- lib/line/bot/message.rb
|
136
|
+
- lib/line/bot/message/base.rb
|
137
|
+
- lib/line/bot/message/content_type.rb
|
138
|
+
- lib/line/bot/message/recipient_type.rb
|
139
|
+
- lib/line/bot/message/text.rb
|
106
140
|
- lib/line/bot/request.rb
|
141
|
+
- lib/line/bot/response.rb
|
107
142
|
- lib/line/bot/version.rb
|
108
143
|
- line-bot.gemspec
|
109
144
|
homepage: https://github.com/elct9620/line-bot
|