line-bot 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +15 -0
  3. data/LICENSE +202 -0
  4. data/README.md +269 -0
  5. data/lib/line/bot.rb +14 -0
  6. data/lib/line/bot/api.rb +8 -0
  7. data/lib/line/bot/api/errors.rb +9 -0
  8. data/lib/line/bot/api/version.rb +7 -0
  9. data/lib/line/bot/builder/multiple_message.rb +101 -0
  10. data/lib/line/bot/builder/rich_message.rb +139 -0
  11. data/lib/line/bot/client.rb +303 -0
  12. data/lib/line/bot/event_type.rb +15 -0
  13. data/lib/line/bot/httpclient.rb +31 -0
  14. data/lib/line/bot/message.rb +9 -0
  15. data/lib/line/bot/message/audio.rb +25 -0
  16. data/lib/line/bot/message/base.rb +35 -0
  17. data/lib/line/bot/message/contact.rb +18 -0
  18. data/lib/line/bot/message/content_type.rb +16 -0
  19. data/lib/line/bot/message/image.rb +23 -0
  20. data/lib/line/bot/message/location.rb +28 -0
  21. data/lib/line/bot/message/recipient_type.rb +9 -0
  22. data/lib/line/bot/message/sticker.rb +26 -0
  23. data/lib/line/bot/message/text.rb +22 -0
  24. data/lib/line/bot/message/video.rb +23 -0
  25. data/lib/line/bot/operation.rb +3 -0
  26. data/lib/line/bot/operation/added_as_friend.rb +10 -0
  27. data/lib/line/bot/operation/base.rb +17 -0
  28. data/lib/line/bot/operation/blocked_account.rb +10 -0
  29. data/lib/line/bot/operation/op_type.rb +10 -0
  30. data/lib/line/bot/receive/message.rb +69 -0
  31. data/lib/line/bot/receive/operation.rb +42 -0
  32. data/lib/line/bot/receive/request.rb +35 -0
  33. data/lib/line/bot/request.rb +86 -0
  34. data/lib/line/bot/response/user/contact.rb +22 -0
  35. data/lib/line/bot/response/user/profile.rb +30 -0
  36. data/lib/line/bot/utils.rb +16 -0
  37. data/line-bot-api.gemspec +32 -0
  38. data/line-bot.gemspec +32 -0
  39. metadata +193 -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,31 @@
1
+ require 'line/bot/api/version'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ module Line
7
+ module Bot
8
+ class HTTPClient
9
+
10
+ # @return [Net::HTTP]
11
+ def http(uri)
12
+ http = Net::HTTP.new(uri.host, uri.port)
13
+ if uri.scheme == "https"
14
+ http.use_ssl = true
15
+ end
16
+
17
+ http
18
+ end
19
+
20
+ def get(url, header = {})
21
+ uri = URI(url)
22
+ http(uri).get(uri.request_uri, header)
23
+ end
24
+
25
+ def post(url, payload, header = {})
26
+ uri = URI(url)
27
+ http(uri).post(uri.request_uri, payload, header)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ require 'line/bot/message/content_type'
2
+ require 'line/bot/message/text'
3
+ require 'line/bot/message/image'
4
+ require 'line/bot/message/video'
5
+ require 'line/bot/message/audio'
6
+ require 'line/bot/message/location'
7
+ require 'line/bot/message/sticker'
8
+ require 'line/bot/message/contact'
9
+ require 'line/bot/message/recipient_type'
@@ -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: recipient_type,
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,35 @@
1
+ require 'line/bot/message/recipient_type'
2
+
3
+ module Line
4
+ module Bot
5
+ module Message
6
+ class Base
7
+ attr_reader :attrs, :recipient_type
8
+
9
+ def initialize(attrs = {})
10
+ @attrs = attrs
11
+ end
12
+
13
+ def recipient_type
14
+ @attrs[:recipient_type] ||= Line::Bot::Message::RecipientType::USER
15
+ end
16
+
17
+ def [](key)
18
+ @attrs[key]
19
+ end
20
+
21
+ def event_type
22
+ Line::Bot::EventType::MESSAGE
23
+ end
24
+
25
+ def content
26
+ raise NotImplementedError, "Implement this method in a child class"
27
+ end
28
+
29
+ def valid?
30
+ raise NotImplementedError, "Implement this method in a child class"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ require 'line/bot/message/base'
2
+
3
+ module Line
4
+ module Bot
5
+ module Message
6
+ class Contact < Line::Bot::Message::Base
7
+
8
+ def content
9
+ raise Line::Bot::API::NotSupportedError
10
+ end
11
+
12
+ def valid?
13
+ raise Line::Bot::API::NotSupportedError
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module Line
2
+ module Bot
3
+ module Message
4
+ module ContentType
5
+ TEXT = 1
6
+ IMAGE = 2
7
+ VIDEO = 3
8
+ AUDIO = 4
9
+ LOCATION = 7
10
+ STICKER = 8
11
+ CONTACT = 10
12
+ RICH_MESSAGE = 12
13
+ end
14
+ end
15
+ end
16
+ 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: recipient_type,
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: recipient_type,
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,9 @@
1
+ module Line
2
+ module Bot
3
+ module Message
4
+ module RecipientType
5
+ USER = 1
6
+ end
7
+ end
8
+ end
9
+ 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: recipient_type,
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: recipient_type,
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: recipient_type,
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,3 @@
1
+ require 'line/bot/operation/op_type'
2
+ require 'line/bot/operation/added_as_friend'
3
+ require 'line/bot/operation/blocked_account'
@@ -0,0 +1,10 @@
1
+ require 'line/bot/operation/base'
2
+
3
+ module Line
4
+ module Bot
5
+ module Operation
6
+ class AddedAsFriend < Line::Bot::Operation::Base
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Line
2
+ module Bot
3
+ module Operation
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
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'line/bot/operation/base'
2
+
3
+ module Line
4
+ module Bot
5
+ module Operation
6
+ class BlockedAccount < Line::Bot::Operation::Base
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Line
2
+ module Bot
3
+ module Operation
4
+ module OpType
5
+ ADDED_AS_FRIEND = 4
6
+ BLOCKED_ACCOUNT = 8
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,69 @@
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, :created_time, :content
6
+
7
+ def initialize(attrs)
8
+ @id = attrs['content']['id']
9
+ @from_mid = attrs['content']['from']
10
+ @to_mid = attrs['content']['to']
11
+
12
+ @from_channel_id = attrs['fromChannel']
13
+ @to_channel_id = attrs['toChannel']
14
+
15
+ @event_type = attrs['eventType']
16
+
17
+ (time, usec) = attrs['content']['createdTime'].to_i.divmod(1000)
18
+ @created_time = Time.at(time, usec)
19
+
20
+ @content = create_content(attrs['content'])
21
+ end
22
+
23
+ def create_content(attrs)
24
+ case attrs['contentType']
25
+ when Line::Bot::Message::ContentType::TEXT
26
+ return Line::Bot::Message::Text.new(
27
+ text: attrs['text'],
28
+ )
29
+ when Line::Bot::Message::ContentType::IMAGE
30
+ return Line::Bot::Message::Image.new(
31
+ image_url: attrs['originalContentUrl'],
32
+ preview_url: attrs['previewImageUrl'],
33
+ )
34
+ when Line::Bot::Message::ContentType::VIDEO
35
+ return Line::Bot::Message::Video.new(
36
+ video_url: attrs['originalContentUrl'],
37
+ preview_url: attrs['previewImageUrl'],
38
+ )
39
+ when Line::Bot::Message::ContentType::AUDIO
40
+ return Line::Bot::Message::Audio.new(
41
+ audio_url: attrs['originalContentUrl'],
42
+ duration: attrs['contentMetadata']['duration'].to_i,
43
+ )
44
+ when Line::Bot::Message::ContentType::LOCATION
45
+ return Line::Bot::Message::Location.new(
46
+ title: attrs['location']['title'],
47
+ address: attrs['location']['address'],
48
+ latitude: attrs['location']['latitude'],
49
+ longitude: attrs['location']['longitude'],
50
+ )
51
+ when Line::Bot::Message::ContentType::STICKER
52
+ return Line::Bot::Message::Sticker.new(
53
+ stkpkgid: attrs['contentMetadata']['STKPKGID'],
54
+ stkid: attrs['contentMetadata']['STKID'],
55
+ stkver: attrs['contentMetadata']['STKVER'],
56
+ )
57
+ when Line::Bot::Message::ContentType::CONTACT
58
+ return Line::Bot::Message::Contact.new(
59
+ mid: attrs['contentMetadata']['mid'],
60
+ display_name: attrs['contentMetadata']['displayName'],
61
+ )
62
+ else
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end
69
+ 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(attrs)
10
+ @id = attrs['id']
11
+ @from_mid = attrs['content']['params'].first
12
+ @to_mid = attrs['to']
13
+
14
+ @from_channel_id = attrs['fromChannel']
15
+ @to_channel_id = attrs['toChannel']
16
+
17
+ @event_type = attrs['eventType']
18
+ @content = create_content(attrs['content'])
19
+ end
20
+
21
+ def create_content(attrs)
22
+ case attrs['opType']
23
+ when Line::Bot::Operation::OpType::ADDED_AS_FRIEND
24
+ return Line::Bot::Operation::AddedAsFriend.new(
25
+ revision: attrs['revision'],
26
+ op_type: attrs['opType'],
27
+ params: attrs['params'],
28
+ )
29
+ when Line::Bot::Operation::OpType::BLOCKED_ACCOUNT
30
+ return Line::Bot::Operation::BlockedAccount.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