line-bot-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/LICENSE +202 -0
- data/README.md +216 -0
- data/lib/line/bot.rb +11 -0
- data/lib/line/bot/api/errors.rb +7 -0
- data/lib/line/bot/api/version.rb +7 -0
- data/lib/line/bot/builder/multiple_message.rb +102 -0
- data/lib/line/bot/builder/rich_message.rb +138 -0
- data/lib/line/bot/client.rb +249 -0
- data/lib/line/bot/event_type.rb +15 -0
- data/lib/line/bot/message.rb +7 -0
- data/lib/line/bot/message/audio.rb +25 -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/image.rb +23 -0
- data/lib/line/bot/message/location.rb +28 -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/add_friend.rb +10 -0
- data/lib/line/bot/operation/base.rb +17 -0
- data/lib/line/bot/operation/block_account.rb +10 -0
- data/lib/line/bot/operation/op_type.rb +10 -0
- data/lib/line/bot/receive/message.rb +60 -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 +72 -0
- data/lib/line/bot/response/user/contact.rb +22 -0
- data/lib/line/bot/response/user/profile.rb +30 -0
- data/line-bot-api.gemspec +27 -0
- metadata +147 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
module EventType
|
4
|
+
MESSAGE = 138311608800106203
|
5
|
+
MULTIPLE_MESSAGE = 140177271400161403
|
6
|
+
end
|
7
|
+
|
8
|
+
module Receive
|
9
|
+
module EventType
|
10
|
+
MESSAGE = 138311609000106303
|
11
|
+
OPERATION = 138311609100106403
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'line/bot/message/base'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Message
|
6
|
+
class Audio < Line::Bot::Message::Base
|
7
|
+
|
8
|
+
def content
|
9
|
+
{
|
10
|
+
contentType: ContentType::AUDIO,
|
11
|
+
toType: 1,
|
12
|
+
originalContentUrl: attrs[:audio_url],
|
13
|
+
contentMetadata: {
|
14
|
+
AUDLEN: attrs[:duration].to_s,
|
15
|
+
},
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid?
|
20
|
+
attrs[:audio_url] && attrs[:duration]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
module Message
|
4
|
+
class Base
|
5
|
+
attr_reader :attrs
|
6
|
+
|
7
|
+
def initialize(attrs = {})
|
8
|
+
@attrs = attrs
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](key)
|
12
|
+
@attrs[key]
|
13
|
+
end
|
14
|
+
|
15
|
+
def event_type
|
16
|
+
138311608800106203
|
17
|
+
end
|
18
|
+
|
19
|
+
def content
|
20
|
+
raise NotImplementedError, "Implement this method in a child class"
|
21
|
+
end
|
22
|
+
|
23
|
+
def valid?
|
24
|
+
raise NotImplementedError, "Implement this method in a child class"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'line/bot/message/base'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Message
|
6
|
+
class Image < Line::Bot::Message::Base
|
7
|
+
|
8
|
+
def content
|
9
|
+
{
|
10
|
+
contentType: ContentType::IMAGE,
|
11
|
+
toType: 1,
|
12
|
+
originalContentUrl: attrs[:image_url],
|
13
|
+
previewImageUrl: attrs[:preview_url],
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid?
|
18
|
+
attrs[:image_url] && attrs[:preview_url]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'line/bot/message/base'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Message
|
6
|
+
class Location < Line::Bot::Message::Base
|
7
|
+
|
8
|
+
def content
|
9
|
+
{
|
10
|
+
contentType: ContentType::LOCATION,
|
11
|
+
toType: 1,
|
12
|
+
text: attrs[:address] || attrs[:title],
|
13
|
+
location: {
|
14
|
+
title: attrs[:title],
|
15
|
+
address: attrs[:address],
|
16
|
+
latitude: attrs[:latitude],
|
17
|
+
longitude: attrs[:longitude],
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid?
|
23
|
+
attrs[:title] && attrs[:latitude] && attrs[:longitude]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'line/bot/message/base'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Message
|
6
|
+
class Sticker < Line::Bot::Message::Base
|
7
|
+
|
8
|
+
def content
|
9
|
+
{
|
10
|
+
contentType: ContentType::STICKER,
|
11
|
+
toType: 1,
|
12
|
+
contentMetadata: {
|
13
|
+
STKPKGID: attrs[:stkpkgid].to_s,
|
14
|
+
STKID: attrs[:stkid].to_s,
|
15
|
+
STKVER: attrs[:stkver].to_s,
|
16
|
+
},
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid?
|
21
|
+
attrs[:stkpkgid] && attrs[:stkid] && attrs[:stkver]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'line/bot/message/base'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Message
|
6
|
+
class Text < Line::Bot::Message::Base
|
7
|
+
|
8
|
+
def content
|
9
|
+
{
|
10
|
+
contentType: ContentType::TEXT,
|
11
|
+
toType: 1,
|
12
|
+
text: attrs[:text]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
!attrs[:text].nil?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'line/bot/message/base'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Message
|
6
|
+
class Video < Line::Bot::Message::Base
|
7
|
+
|
8
|
+
def content
|
9
|
+
{
|
10
|
+
contentType: ContentType::VIDEO,
|
11
|
+
toType: 1,
|
12
|
+
originalContentUrl: attrs[:video_url],
|
13
|
+
previewImageUrl: attrs[:preview_url],
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid?
|
18
|
+
attrs[:video_url] && attrs[:preview_url]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Line
|
2
|
+
module Bot
|
3
|
+
module Receive
|
4
|
+
class Message
|
5
|
+
attr_reader :id, :from_mid, :to_mid, :from_channel_id, :to_channel_id, :event_type, :content
|
6
|
+
|
7
|
+
def initialize(env)
|
8
|
+
@id = env['content']['id']
|
9
|
+
@from_mid = env['content']['from']
|
10
|
+
@to_mid = env['content']['to']
|
11
|
+
|
12
|
+
@from_channel_id = env['fromChannel']
|
13
|
+
@to_channel_id = env['toChannel']
|
14
|
+
|
15
|
+
@event_type = env['eventType']
|
16
|
+
@content = create_content(env['content'])
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_content(attrs)
|
20
|
+
case attrs['contentType']
|
21
|
+
when Line::Bot::Message::ContentType::TEXT
|
22
|
+
return Line::Bot::Message::Text.new(
|
23
|
+
text: attrs['text'],
|
24
|
+
)
|
25
|
+
when Line::Bot::Message::ContentType::IMAGE
|
26
|
+
return Line::Bot::Message::Image.new(
|
27
|
+
image_url: attrs['originalContentUrl'],
|
28
|
+
preview_url: attrs['previewImageUrl'],
|
29
|
+
)
|
30
|
+
when Line::Bot::Message::ContentType::VIDEO
|
31
|
+
return Line::Bot::Message::Video.new(
|
32
|
+
video_url: attrs['originalContentUrl'],
|
33
|
+
preview_url: attrs['previewImageUrl'],
|
34
|
+
)
|
35
|
+
when Line::Bot::Message::ContentType::AUDIO
|
36
|
+
return Line::Bot::Message::Audio.new(
|
37
|
+
audio_url: attrs['originalContentUrl'],
|
38
|
+
duration: attrs['contentMetadata']['duration'].to_i,
|
39
|
+
)
|
40
|
+
when Line::Bot::Message::ContentType::LOCATION
|
41
|
+
return Line::Bot::Message::Location.new(
|
42
|
+
title: attrs['location']['title'],
|
43
|
+
address: attrs['location']['address'],
|
44
|
+
latitude: attrs['location']['latitude'],
|
45
|
+
longitude: attrs['location']['longitude'],
|
46
|
+
)
|
47
|
+
when Line::Bot::Message::ContentType::STICKER
|
48
|
+
return Line::Bot::Message::Sticker.new(
|
49
|
+
stkpkgid: attrs['contentMetadata']['STKPKGID'],
|
50
|
+
stkid: attrs['contentMetadata']['STKID'],
|
51
|
+
stkver: attrs['contentMetadata']['STKVER'],
|
52
|
+
)
|
53
|
+
else
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'line/bot/operation/op_type'
|
2
|
+
|
3
|
+
module Line
|
4
|
+
module Bot
|
5
|
+
module Receive
|
6
|
+
class Operation
|
7
|
+
attr_reader :id, :from_mid, :to_mid, :from_channel_id, :to_channel_id, :event_type, :content
|
8
|
+
|
9
|
+
def initialize(env)
|
10
|
+
@id = env['content']['id']
|
11
|
+
@from_mid = env['content']['from']
|
12
|
+
@to_mid = env['content']['to']
|
13
|
+
|
14
|
+
@from_channel_id = env['fromChannel']
|
15
|
+
@to_channel_id = env['toChannel']
|
16
|
+
|
17
|
+
@event_type = env['eventType']
|
18
|
+
@content = create_content(env['content'])
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_content(attrs)
|
22
|
+
case attrs['opType']
|
23
|
+
when Line::Bot::Operation::OpType::ADD_FRIEND
|
24
|
+
return Line::Bot::Operation::AddFriend.new(
|
25
|
+
revision: attrs['revision'],
|
26
|
+
op_type: attrs['opType'],
|
27
|
+
params: attrs['params'],
|
28
|
+
)
|
29
|
+
when Line::Bot::Operation::OpType::BLOCK_ACCOUNT
|
30
|
+
return Line::Bot::Operation::BlockAccount.new(
|
31
|
+
revision: attrs['revision'],
|
32
|
+
op_type: attrs['opType'],
|
33
|
+
params: attrs['params'],
|
34
|
+
)
|
35
|
+
else
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -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,72 @@
|
|
1
|
+
require 'line/bot/api/version'
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module Line
|
6
|
+
module Bot
|
7
|
+
class Request
|
8
|
+
attr_accessor :endpoint_path, :credentials, :to_mid, :message
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
yield(self) if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def https
|
15
|
+
https = Net::HTTP.new('trialbot-api.line.me', 443)
|
16
|
+
https.use_ssl = true
|
17
|
+
|
18
|
+
https
|
19
|
+
end
|
20
|
+
|
21
|
+
def to
|
22
|
+
to_mid.instance_of?(String) ? [to_mid] : to_mid
|
23
|
+
end
|
24
|
+
|
25
|
+
def content
|
26
|
+
message.content
|
27
|
+
end
|
28
|
+
|
29
|
+
def payload
|
30
|
+
payload = {
|
31
|
+
to: to,
|
32
|
+
toChannel: 1383378250, # Fixed value
|
33
|
+
eventType: message.event_type.to_s,
|
34
|
+
content: content
|
35
|
+
}
|
36
|
+
|
37
|
+
payload.to_json
|
38
|
+
end
|
39
|
+
|
40
|
+
def header
|
41
|
+
header = {
|
42
|
+
'Content-Type' => 'application/json; charset=UTF-8',
|
43
|
+
'User-Agent' => "LINE-BotSDK/#{Line::Bot::API::VERSION}",
|
44
|
+
}
|
45
|
+
hash = credentials.inject({}) { |h, (k, v)| h[k] = v.to_s; h }
|
46
|
+
|
47
|
+
header.merge(hash)
|
48
|
+
end
|
49
|
+
|
50
|
+
def get
|
51
|
+
raise ArgumentError, "Invalid arguments" unless validate_for_getting_message?
|
52
|
+
|
53
|
+
https.get(endpoint_path, header)
|
54
|
+
end
|
55
|
+
|
56
|
+
def post
|
57
|
+
raise ArgumentError, "Invalid arguments" unless validate_for_posting_message?
|
58
|
+
|
59
|
+
https.post(endpoint_path, payload, header)
|
60
|
+
end
|
61
|
+
|
62
|
+
def validate_for_getting_message?
|
63
|
+
!endpoint_path.nil?
|
64
|
+
end
|
65
|
+
|
66
|
+
def validate_for_posting_message?
|
67
|
+
to.size > 0 && message.valid? && endpoint_path
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|