line-bot 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +15 -0
- data/LICENSE +202 -0
- data/README.md +269 -0
- data/lib/line/bot.rb +14 -0
- data/lib/line/bot/api.rb +8 -0
- data/lib/line/bot/api/errors.rb +9 -0
- data/lib/line/bot/api/version.rb +7 -0
- data/lib/line/bot/builder/multiple_message.rb +101 -0
- data/lib/line/bot/builder/rich_message.rb +139 -0
- data/lib/line/bot/client.rb +303 -0
- data/lib/line/bot/event_type.rb +15 -0
- data/lib/line/bot/httpclient.rb +31 -0
- data/lib/line/bot/message.rb +9 -0
- data/lib/line/bot/message/audio.rb +25 -0
- data/lib/line/bot/message/base.rb +35 -0
- data/lib/line/bot/message/contact.rb +18 -0
- data/lib/line/bot/message/content_type.rb +16 -0
- data/lib/line/bot/message/image.rb +23 -0
- data/lib/line/bot/message/location.rb +28 -0
- data/lib/line/bot/message/recipient_type.rb +9 -0
- data/lib/line/bot/message/sticker.rb +26 -0
- data/lib/line/bot/message/text.rb +22 -0
- data/lib/line/bot/message/video.rb +23 -0
- data/lib/line/bot/operation.rb +3 -0
- data/lib/line/bot/operation/added_as_friend.rb +10 -0
- data/lib/line/bot/operation/base.rb +17 -0
- data/lib/line/bot/operation/blocked_account.rb +10 -0
- data/lib/line/bot/operation/op_type.rb +10 -0
- data/lib/line/bot/receive/message.rb +69 -0
- data/lib/line/bot/receive/operation.rb +42 -0
- data/lib/line/bot/receive/request.rb +35 -0
- data/lib/line/bot/request.rb +86 -0
- data/lib/line/bot/response/user/contact.rb +22 -0
- data/lib/line/bot/response/user/profile.rb +30 -0
- data/lib/line/bot/utils.rb +16 -0
- data/line-bot-api.gemspec +32 -0
- data/line-bot.gemspec +32 -0
- metadata +193 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'line/bot/receive/message'
|
2
|
+
require 'json'
|
3
|
+
require 'rack'
|
4
|
+
|
5
|
+
module Line
|
6
|
+
module Bot
|
7
|
+
module Receive
|
8
|
+
class Request < Rack::Request
|
9
|
+
|
10
|
+
def data
|
11
|
+
@data ||= parse_data_from_body
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse_data_from_body
|
15
|
+
body.rewind
|
16
|
+
json = JSON.parse(body.read)
|
17
|
+
result = json['result']
|
18
|
+
|
19
|
+
result.map { |item| create_message_or_operation(item) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_message_or_operation(data)
|
23
|
+
case data['eventType'].to_i
|
24
|
+
when Line::Bot::Receive::EventType::MESSAGE
|
25
|
+
return Line::Bot::Receive::Message.new(data)
|
26
|
+
when Line::Bot::Receive::EventType::OPERATION
|
27
|
+
return Line::Bot::Receive::Operation.new(data)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'line/bot/api/version'
|
2
|
+
require 'line/bot/utils'
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Line
|
8
|
+
module Bot
|
9
|
+
class Request
|
10
|
+
attr_accessor :endpoint, :endpoint_path, :credentials, :to_mid, :message, :to_channel_id, :httpclient
|
11
|
+
|
12
|
+
include Line::Bot::Utils
|
13
|
+
|
14
|
+
# Initializes a new Request
|
15
|
+
#
|
16
|
+
# @return [Line::Bot::Request]
|
17
|
+
def initialize
|
18
|
+
yield(self) if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Array]
|
22
|
+
def to
|
23
|
+
to_mid.is_a?(String) ? [to_mid] : to_mid
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Line::Bot::Message::Base#content]
|
27
|
+
def content
|
28
|
+
message.content
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [Hash]
|
32
|
+
def payload
|
33
|
+
payload = {
|
34
|
+
to: to,
|
35
|
+
toChannel: to_channel_id,
|
36
|
+
eventType: message.event_type.to_s,
|
37
|
+
content: content
|
38
|
+
}
|
39
|
+
|
40
|
+
payload.to_json
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [Hash]
|
44
|
+
def header
|
45
|
+
header = {
|
46
|
+
'Content-Type' => 'application/json; charset=UTF-8',
|
47
|
+
'User-Agent' => "LINE-BotSDK/#{Line::Bot::API::VERSION}",
|
48
|
+
}
|
49
|
+
hash = credentials.inject({}) { |h, (k, v)| h[k] = v.to_s; h }
|
50
|
+
|
51
|
+
header.merge(hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Get content of specified URL.
|
55
|
+
#
|
56
|
+
# @raise [ArgumentError]
|
57
|
+
#
|
58
|
+
# @return [Net::HTTPResponse]
|
59
|
+
def get
|
60
|
+
assert_for_getting_message
|
61
|
+
httpclient.get(endpoint + endpoint_path, header)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Post content of specified URL.
|
65
|
+
#
|
66
|
+
# @raise [ArgumentError]
|
67
|
+
#
|
68
|
+
# @return [Net::HTTPResponse]
|
69
|
+
def post
|
70
|
+
assert_for_posting_message
|
71
|
+
httpclient.post(endpoint + endpoint_path, payload, header)
|
72
|
+
end
|
73
|
+
|
74
|
+
def assert_for_getting_message
|
75
|
+
raise ArgumentError, 'Wrong argument type `endpoint_path`' unless endpoint_path.is_a?(String)
|
76
|
+
end
|
77
|
+
|
78
|
+
def assert_for_posting_message
|
79
|
+
raise ArgumentError, 'Wrong argument type `to_mid`' unless validate_mids(to)
|
80
|
+
raise ArgumentError, 'Invalid argument `message`' unless message.valid?
|
81
|
+
raise ArgumentError, 'Wrong argument type `endpoint_path`' unless endpoint_path.is_a?(String)
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Response
|
6
|
+
module User
|
7
|
+
|
8
|
+
class Contact
|
9
|
+
attr_reader :mid, :display_name, :picture_url, :status_message
|
10
|
+
|
11
|
+
def initialize(attrs)
|
12
|
+
@mid = attrs['mid']
|
13
|
+
@display_name = attrs['displayName']
|
14
|
+
@picture_url = attrs['pictureUrl']
|
15
|
+
@status_message = attrs['statusMessage']
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'line/bot/response/user/contact'
|
3
|
+
|
4
|
+
module Line
|
5
|
+
module Bot
|
6
|
+
module Response
|
7
|
+
module User
|
8
|
+
class Profile
|
9
|
+
attr_reader :start, :count, :total, :contacts, :display
|
10
|
+
|
11
|
+
def initialize(response)
|
12
|
+
json = JSON.parse(response.body)
|
13
|
+
|
14
|
+
@start = json['start']
|
15
|
+
@count = json['count']
|
16
|
+
@total = json['total']
|
17
|
+
@contacts = create_contacts(json['contacts']) || []
|
18
|
+
@display = json['display']
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_contacts(contacts)
|
22
|
+
contacts.map { |c|
|
23
|
+
Contact.new(c)
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
module Utils
|
4
|
+
|
5
|
+
# @return [Boolean]
|
6
|
+
def validate_mids(mids)
|
7
|
+
return false unless mids.is_a?(String) || mids.is_a?(Array)
|
8
|
+
mids = [mids] if mids.is_a?(String)
|
9
|
+
|
10
|
+
return false unless mids.size > 0
|
11
|
+
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'line/bot/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "line-bot-api"
|
8
|
+
spec.version = Line::Bot::API::VERSION
|
9
|
+
spec.authors = ["LINE Corporation"]
|
10
|
+
spec.email = ["hirohisa.kawasaki@gmail.com"]
|
11
|
+
|
12
|
+
spec.description = "Line::Bot::API - SDK of the LINE BOT API"
|
13
|
+
spec.summary = spec.description
|
14
|
+
spec.homepage = "https://github.com/line/line-bot-sdk-ruby"
|
15
|
+
spec.license = "Apache License, Version 2.0"
|
16
|
+
|
17
|
+
spec.files = %w(CONTRIBUTING.md LICENSE README.md line-bot-api.gemspec) + Dir['lib/**/*.rb']
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.0.0'
|
22
|
+
|
23
|
+
spec.add_dependency 'rack', '>= 1.6'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 1.24"
|
28
|
+
spec.add_development_dependency "rest-client", "~> 1.8"
|
29
|
+
spec.add_development_dependency "httpclient", "~> 2.8"
|
30
|
+
spec.add_development_dependency "addressable", "~> 2.3"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
data/line-bot.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'line/bot/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "line-bot"
|
8
|
+
spec.version = Line::Bot::API::VERSION
|
9
|
+
spec.authors = ["LINE Corporation"]
|
10
|
+
spec.email = ["hirohisa.kawasaki@gmail.com"]
|
11
|
+
|
12
|
+
spec.description = "Line::Bot::API - SDK of the LINE BOT API"
|
13
|
+
spec.summary = spec.description
|
14
|
+
spec.homepage = "https://github.com/line/line-bot-sdk-ruby"
|
15
|
+
spec.license = "Apache-2.0"
|
16
|
+
|
17
|
+
spec.files = %w(CONTRIBUTING.md LICENSE README.md line-bot-api.gemspec line-bot.gemspec) + Dir['lib/**/*.rb']
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.0.0'
|
22
|
+
|
23
|
+
spec.add_dependency 'rack', '~> 0'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 1.24"
|
28
|
+
spec.add_development_dependency "rest-client", "~> 1.8"
|
29
|
+
spec.add_development_dependency "httpclient", "~> 2.8"
|
30
|
+
spec.add_development_dependency "addressable", "~> 2.3"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: line-bot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- LINE Corporation
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.24'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.24'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rest-client
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: httpclient
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.8'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.8'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: addressable
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.3'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.3'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.0'
|
125
|
+
description: Line::Bot::API - SDK of the LINE BOT API
|
126
|
+
email:
|
127
|
+
- hirohisa.kawasaki@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- CONTRIBUTING.md
|
133
|
+
- LICENSE
|
134
|
+
- README.md
|
135
|
+
- lib/line/bot.rb
|
136
|
+
- lib/line/bot/api.rb
|
137
|
+
- lib/line/bot/api/errors.rb
|
138
|
+
- lib/line/bot/api/version.rb
|
139
|
+
- lib/line/bot/builder/multiple_message.rb
|
140
|
+
- lib/line/bot/builder/rich_message.rb
|
141
|
+
- lib/line/bot/client.rb
|
142
|
+
- lib/line/bot/event_type.rb
|
143
|
+
- lib/line/bot/httpclient.rb
|
144
|
+
- lib/line/bot/message.rb
|
145
|
+
- lib/line/bot/message/audio.rb
|
146
|
+
- lib/line/bot/message/base.rb
|
147
|
+
- lib/line/bot/message/contact.rb
|
148
|
+
- lib/line/bot/message/content_type.rb
|
149
|
+
- lib/line/bot/message/image.rb
|
150
|
+
- lib/line/bot/message/location.rb
|
151
|
+
- lib/line/bot/message/recipient_type.rb
|
152
|
+
- lib/line/bot/message/sticker.rb
|
153
|
+
- lib/line/bot/message/text.rb
|
154
|
+
- lib/line/bot/message/video.rb
|
155
|
+
- lib/line/bot/operation.rb
|
156
|
+
- lib/line/bot/operation/added_as_friend.rb
|
157
|
+
- lib/line/bot/operation/base.rb
|
158
|
+
- lib/line/bot/operation/blocked_account.rb
|
159
|
+
- lib/line/bot/operation/op_type.rb
|
160
|
+
- lib/line/bot/receive/message.rb
|
161
|
+
- lib/line/bot/receive/operation.rb
|
162
|
+
- lib/line/bot/receive/request.rb
|
163
|
+
- lib/line/bot/request.rb
|
164
|
+
- lib/line/bot/response/user/contact.rb
|
165
|
+
- lib/line/bot/response/user/profile.rb
|
166
|
+
- lib/line/bot/utils.rb
|
167
|
+
- line-bot-api.gemspec
|
168
|
+
- line-bot.gemspec
|
169
|
+
homepage: https://github.com/line/line-bot-sdk-ruby
|
170
|
+
licenses:
|
171
|
+
- Apache-2.0
|
172
|
+
metadata: {}
|
173
|
+
post_install_message:
|
174
|
+
rdoc_options: []
|
175
|
+
require_paths:
|
176
|
+
- lib
|
177
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: 2.0.0
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
requirements: []
|
188
|
+
rubyforge_project:
|
189
|
+
rubygems_version: 2.5.1
|
190
|
+
signing_key:
|
191
|
+
specification_version: 4
|
192
|
+
summary: Line::Bot::API - SDK of the LINE BOT API
|
193
|
+
test_files: []
|