ruboty-line_bot 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd9cfd072d11e2014a753b46c4276acaf0718af4
4
+ data.tar.gz: 0513a4fff707692bb896e5fc1bc1a244727357b8
5
+ SHA512:
6
+ metadata.gz: 8280e03f66aa54298162d69cbd825caf394b3abb16d59de6cc85eb4f95f59ed8c53617fc5560b6ece8ead60726208c2e82456324f5564861062c966506f52a12
7
+ data.tar.gz: 9fdd71fa0e1da33db9b76dd9b5da86a68787b581871a78e56863fb80e357d92acd2e6c7e9db0638b7bbdaa05f48e8553381ba08b3e8b4cff6e00abb481e2bc66
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.10.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-line_bot.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 meganemura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Ruboty::LineBot
2
+
3
+ LINE BOT API Adapter for [Ruboty](https://github.com/r7kamura/ruboty).
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ gem "ruboty-line_bot"
9
+ ```
10
+
11
+ ## ENV
12
+ ```
13
+ LINE_CHANNEL_ID - LINE Channel ID
14
+ LINE_CHANNEL_SECRET - LINE Channel Secret
15
+ LINE_CHANNEL_MID - LINE Channel MID
16
+ LINE_SERVER_BIND_ADDRESS - Bind address (default: 0.0.0.0)
17
+ LINE_SERVER_PORT - Port (default: 4567)
18
+ LINE_SERVER_ENDPOINT - Endpoint (default: /callback)
19
+ RESPOND_TO_ALL - Pass 1 to force ruboty to respond all message without mention
20
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty/line_bot"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/ruboty ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty"
5
+
6
+ Ruboty::CommandBuilder.new(ARGV).build.call
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ module Ruboty
2
+ class Action
3
+ # return true to respond every message without robot name.
4
+ def all?
5
+ true
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,129 @@
1
+ require "rack"
2
+ require 'rack/handler/webrick'
3
+ require 'line/bot'
4
+
5
+ module Ruboty
6
+ module Adapters
7
+ class LineBot < Base
8
+ env :LINE_CHANNEL_ID, "LINE Channel ID"
9
+ env :LINE_CHANNEL_SECRET, "LINE Channel Secret"
10
+ env :LINE_CHANNEL_MID, "LINE Channel MID"
11
+ env :LINE_SERVER_BIND_ADDRESS, "Bind address (default: 0.0.0.0)", :optional => true
12
+ env :LINE_SERVER_PORT, "Port (default: 4567)", :optional => true
13
+ env :LINE_SERVER_ENDPOINT, "Endpoint (default: /callback)", :optional => true
14
+
15
+ def run
16
+ init
17
+ serve
18
+ main_loop
19
+ end
20
+
21
+ def init
22
+ @queue = Queue.new
23
+ end
24
+
25
+ def say(message)
26
+ client.send_text(
27
+ to_mid: message[:original][:from_mid],
28
+ text: message[:body],
29
+ )
30
+ end
31
+
32
+ private
33
+
34
+ def client
35
+ @client ||= Line::Bot::Client.new do |config|
36
+ config.channel_id = channel_id
37
+ config.channel_secret = channel_secret
38
+ config.channel_mid = channel_mid
39
+ end
40
+ end
41
+
42
+ def serve
43
+ Thread.new do
44
+ server.start
45
+ end
46
+ Thread.abort_on_exception = true
47
+ end
48
+
49
+ def server
50
+ return @server if @server
51
+
52
+ @server = WEBrick::HTTPServer.new({
53
+ BindAddress: bind_address,
54
+ Port: port,
55
+ Logger: Ruboty.logger,
56
+ })
57
+ @server.mount(endpoint, Rack::Handler::WEBrick, lambda(&method(:callback)))
58
+
59
+ @server
60
+ end
61
+
62
+ def callback(env)
63
+ req = Rack::Request.new(env)
64
+
65
+ unless req.request_method == 'POST'
66
+ return [404, {}, ['Not found']]
67
+ end
68
+
69
+ signature = req.env['HTTP_X_LINE_CHANNELSIGNATURE']
70
+
71
+ unless client.validate_signature(req.body.read, signature)
72
+ return [400, {}, ['Bad Request']]
73
+ end
74
+
75
+ @queue.enq(req.env)
76
+
77
+ [204, {}, []]
78
+ end
79
+
80
+ def main_loop
81
+ loop do
82
+ request_from_queue.data.each do |event|
83
+ next unless event.content.is_a?(Line::Bot::Message::Text)
84
+
85
+ keys = [:id, :from_mid, :to_mid, :from_channel_id, :to_channel_id, :event_type, :created_time, :content]
86
+ message = keys.each_with_object({}) {|key, hash| hash[key] = event.send(key) }
87
+ message.update(
88
+ body: message[:content].content[:text],
89
+ from: message[:from_mid],
90
+ to: message[:to_mid],
91
+ type: message[:event_type],
92
+ )
93
+ Ruboty.logger.debug('Received:' + message.inspect)
94
+ robot.receive(message)
95
+ end
96
+ end
97
+ end
98
+
99
+ def request_from_queue
100
+ rack_raw_env = @queue.deq
101
+ Line::Bot::Receive::Request.new(rack_raw_env)
102
+ end
103
+
104
+ def channel_id
105
+ ENV['LINE_CHANNEL_ID']
106
+ end
107
+
108
+ def channel_secret
109
+ ENV['LINE_CHANNEL_SECRET']
110
+ end
111
+
112
+ def channel_mid
113
+ ENV['LINE_CHANNEL_MID']
114
+ end
115
+
116
+ def bind_address
117
+ ENV['WEBHOOK_BIND_ADDRESS'] || '0.0.0.0'
118
+ end
119
+
120
+ def port
121
+ ENV['WEBHOOK_PORT'] || '4567'
122
+ end
123
+
124
+ def endpoint
125
+ ENV['WEBHOOK_ENDPOINT'] || '/callback'
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,5 @@
1
+ require "ruboty/line_bot/version"
2
+ require "ruboty/adapters/line_bot"
3
+ if ENV['RESPOND_TO_ALL'] == '1'
4
+ require "ruboty/action_tweaks"
5
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module LineBot
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruboty/line_bot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruboty-line_bot"
8
+ spec.version = Ruboty::LineBot::VERSION
9
+ spec.authors = ["meganemura"]
10
+ spec.email = ["meganemura@users.noreply.github.com"]
11
+
12
+ spec.summary = "LINE BOT API adapter for Ruboty."
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/meganemura/ruboty-line_bot"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "ruboty"
23
+ spec.add_dependency "line-bot-api"
24
+ spec.add_dependency "rack"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-line_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - meganemura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
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: line-bot-api
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: rack
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: LINE BOT API adapter for Ruboty.
98
+ email:
99
+ - meganemura@users.noreply.github.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/ruboty
113
+ - bin/setup
114
+ - lib/ruboty/action_tweaks.rb
115
+ - lib/ruboty/adapters/line_bot.rb
116
+ - lib/ruboty/line_bot.rb
117
+ - lib/ruboty/line_bot/version.rb
118
+ - ruboty-line_bot.gemspec
119
+ homepage: https://github.com/meganemura/ruboty-line_bot
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.4.5.1
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: LINE BOT API adapter for Ruboty.
143
+ test_files: []