kamigo 0.14.0 → 0.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/line_controller.rb +9 -64
- data/lib/generators/kamigo/install_generator.rb +17 -0
- data/lib/generators/templates/kamigo.rb +14 -0
- data/lib/kamigo.rb +19 -1
- data/lib/kamigo/event_parsers/line_event_parser.rb +2 -0
- data/lib/kamigo/event_processors/rails_router_processor.rb +87 -0
- data/lib/kamigo/event_responsers/line_event_responser.rb +1 -1
- data/lib/kamigo/events/basic_event.rb +3 -1
- data/lib/kamigo/version.rb +1 -1
- metadata +9 -7
- data/app/controllers/concerns/reverse_route.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0286bef7f7c96549005475052dd8346f44fe4e010a917322a6cdd3cbf654bee
|
4
|
+
data.tar.gz: d7d456254873fa6e5ae5c15e8e27801068ee7cf96c7caa0cedf61f9f996e2ceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5aacaf96b30ea7c065c141fd279ace76d2e89ef087da6d609a95d8a24cf877fb40e25557e4903de0e2e37859f5a321a08c4a1dc3fab8bdb1e0f35fdbc20b6aa
|
7
|
+
data.tar.gz: d02be888e22755e6484caa313bda70384cf8bd7e2c503da4586aa0cb3394dfbbb293e9edf119f7c47f986d902958a742d12e6d81a74712b80c82e61527880372
|
@@ -1,14 +1,15 @@
|
|
1
1
|
require 'uri'
|
2
2
|
|
3
3
|
class LineController < ApplicationController
|
4
|
-
include ReverseRoute
|
5
4
|
protect_from_forgery with: :null_session
|
6
5
|
|
7
6
|
def entry
|
8
7
|
parser = Kamigo::EventParsers::LineEventParser.new
|
9
8
|
events = parser.parse_events(request)
|
10
9
|
events.each do |event|
|
11
|
-
process_event(event)
|
10
|
+
output = process_event(event) || Kamigo.line_default_message
|
11
|
+
responser = Kamigo::EventResponsers::LineEventResponser.new
|
12
|
+
response = responser.response_event(event, output)
|
12
13
|
end
|
13
14
|
head :ok
|
14
15
|
end
|
@@ -16,68 +17,12 @@ class LineController < ApplicationController
|
|
16
17
|
private
|
17
18
|
|
18
19
|
def process_event(event)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
responser = Kamigo::EventResponsers::LineEventResponser.new
|
25
|
-
response = responser.response_event(event, output)
|
26
|
-
puts response&.body
|
27
|
-
rescue NoMethodError => e
|
28
|
-
puts e.full_message
|
29
|
-
responser = Kamigo::EventResponsers::LineEventResponser.new
|
30
|
-
response = responser.response_event(event, {
|
31
|
-
type: "text",
|
32
|
-
text: "404 not found"
|
33
|
-
})
|
34
|
-
end
|
35
|
-
|
36
|
-
def slot_filling(event)
|
37
|
-
begin
|
38
|
-
Kamiform
|
39
|
-
rescue StandardError
|
40
|
-
return [nil, nil, nil]
|
41
|
-
end
|
42
|
-
|
43
|
-
form = Kamiform.find_by(
|
44
|
-
platform_type: event.platform_type,
|
45
|
-
source_group_id: event.source_group_id
|
46
|
-
)
|
47
|
-
return [nil, nil, nil] if form.nil?
|
48
|
-
|
49
|
-
http_method = form.http_method
|
50
|
-
path = form.path
|
51
|
-
request_params = form.params
|
52
|
-
|
53
|
-
# fill
|
54
|
-
if form.field['.'].nil?
|
55
|
-
request_params[form.field] = event.message
|
56
|
-
else
|
57
|
-
*head, tail = form.field.split('.')
|
58
|
-
request_params.dig(*head)[tail] = event.message
|
20
|
+
Kamigo.line_event_processors.each do |processor|
|
21
|
+
processor.request = request
|
22
|
+
processor.form_authenticity_token = form_authenticity_token
|
23
|
+
output = processor.process(event)
|
24
|
+
return output if output.present?
|
59
25
|
end
|
60
|
-
|
61
|
-
[http_method.upcase, path, request_params]
|
26
|
+
nil
|
62
27
|
end
|
63
|
-
|
64
|
-
def language_understanding(text)
|
65
|
-
http_method = %w[GET POST PUT PATCH DELETE].find do |http_method|
|
66
|
-
text.start_with? http_method
|
67
|
-
end
|
68
|
-
text = text[http_method.size..-1] if http_method.present?
|
69
|
-
text = text.strip
|
70
|
-
lines = text.split("\n").compact
|
71
|
-
path = lines.shift
|
72
|
-
request_params = parse_json(lines.join(""))
|
73
|
-
request_params[:authenticity_token] = form_authenticity_token
|
74
|
-
http_method = request_params["_method"]&.upcase || http_method || "GET"
|
75
|
-
[http_method, path, request_params]
|
76
|
-
end
|
77
|
-
|
78
|
-
def parse_json(string)
|
79
|
-
return {} if string.strip.empty?
|
80
|
-
JSON.parse(string)
|
81
|
-
end
|
82
|
-
|
83
28
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/base'
|
4
|
+
|
5
|
+
module Kamigo
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
source_root File.expand_path("../../templates", __FILE__)
|
9
|
+
|
10
|
+
desc "Creates a Kamigo initializer and copy locale files to your application."
|
11
|
+
|
12
|
+
def copy_initializer
|
13
|
+
template "kamigo.rb", "config/initializers/kamigo.rb"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Kamigo.setup do |config|
|
2
|
+
# When user input doesn't match the route, Kamigo will pass the reuqest to default_path with default_http_method.
|
3
|
+
# config.default_path = "/"
|
4
|
+
# config.default_http_method = "GET"
|
5
|
+
|
6
|
+
# When Kamigo don't know what message to reply, then Kamigo reply the line_default_message.
|
7
|
+
# config.line_default_message = {
|
8
|
+
# type: "text",
|
9
|
+
# text: "Sorry, I don't understand your message."
|
10
|
+
# }
|
11
|
+
|
12
|
+
# When line_default_message is nil, then Kamigo don't reply message.
|
13
|
+
# config.line_default_message = nil
|
14
|
+
end
|
data/lib/kamigo.rb
CHANGED
@@ -7,6 +7,24 @@ require "kamigo/events/basic_event"
|
|
7
7
|
require "kamigo/events/line_event"
|
8
8
|
require "kamigo/event_parsers/line_event_parser"
|
9
9
|
require "kamigo/event_responsers/line_event_responser"
|
10
|
+
require "kamigo/event_processors/rails_router_processor"
|
10
11
|
module Kamigo
|
11
|
-
|
12
|
+
mattr_accessor :line_default_message
|
13
|
+
@@line_default_message = {
|
14
|
+
type: "text",
|
15
|
+
text: "Sorry, I don't understand your message."
|
16
|
+
}
|
17
|
+
|
18
|
+
mattr_accessor :default_path
|
19
|
+
@@default_path = nil
|
20
|
+
|
21
|
+
mattr_accessor :default_http_method
|
22
|
+
@@default_http_method = "GET"
|
23
|
+
|
24
|
+
mattr_accessor :line_event_processors
|
25
|
+
@@line_event_processors = [EventProcessors::RailsRouterProcessor.new]
|
26
|
+
|
27
|
+
def self.setup
|
28
|
+
yield self
|
29
|
+
end
|
12
30
|
end
|
@@ -13,9 +13,11 @@ module Kamigo
|
|
13
13
|
|
14
14
|
def parse(event)
|
15
15
|
payload = JSON.parse(event.to_json, symbolize_names: true)[:src]
|
16
|
+
response = client.get_profile(payload[:source][:userId])
|
16
17
|
line_event = Kamigo::Events::LineEvent.new
|
17
18
|
line_event.payload = payload
|
18
19
|
line_event.reply_token = event['replyToken']
|
20
|
+
line_event.profile = JSON.parse(response.body)
|
19
21
|
line_event.source_type = payload.dig(:source, :type)
|
20
22
|
line_event.source_group_id = payload.dig(:source, :groupId) || payload.dig(:source, :roomId) || payload.dig(:source, :userId)
|
21
23
|
line_event.source_user_id = payload.dig(:source, :userId) || payload.dig(:source, :groupId) || payload.dig(:source, :roomId)
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Kamigo
|
2
|
+
module EventProcessors
|
3
|
+
class RailsRouterProcessor
|
4
|
+
attr_accessor :request
|
5
|
+
attr_accessor :form_authenticity_token
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
end
|
9
|
+
|
10
|
+
def process(event)
|
11
|
+
http_method, path, request_params = kamiform_context(event)
|
12
|
+
http_method, path, request_params = language_understanding(event.message) if http_method.nil?
|
13
|
+
encoded_path = URI.encode(path)
|
14
|
+
request_params = event.platform_params.merge(request_params)
|
15
|
+
output = reserve_route(encoded_path, http_method: http_method, request_params: request_params, format: :line)
|
16
|
+
return output if output.present?
|
17
|
+
|
18
|
+
return nil if Kamigo.default_path.nil?
|
19
|
+
reserve_route(URI.encode(Kamigo.default_path), http_method: Kamigo.default_http_method, request_params: request_params, format: :line)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def kamiform_context(event)
|
25
|
+
begin
|
26
|
+
Kamiform
|
27
|
+
rescue StandardError
|
28
|
+
return [nil, nil, nil]
|
29
|
+
end
|
30
|
+
|
31
|
+
form = Kamiform.find_by(
|
32
|
+
platform_type: event.platform_type,
|
33
|
+
source_group_id: event.source_group_id
|
34
|
+
)
|
35
|
+
return [nil, nil, nil] if form.nil?
|
36
|
+
|
37
|
+
http_method = form.http_method
|
38
|
+
path = form.path
|
39
|
+
request_params = form.params
|
40
|
+
|
41
|
+
# fill
|
42
|
+
if form.field['.'].nil?
|
43
|
+
request_params[form.field] = event.message
|
44
|
+
else
|
45
|
+
*head, tail = form.field.split('.')
|
46
|
+
request_params.dig(*head)[tail] = event.message
|
47
|
+
end
|
48
|
+
form.destroy
|
49
|
+
[http_method.upcase, path, request_params]
|
50
|
+
end
|
51
|
+
|
52
|
+
def language_understanding(text)
|
53
|
+
http_method = %w[GET POST PUT PATCH DELETE].find do |http_method|
|
54
|
+
text.start_with? http_method
|
55
|
+
end
|
56
|
+
text = text[http_method.size..-1] if http_method.present?
|
57
|
+
text = text.strip
|
58
|
+
lines = text.split("\n").compact
|
59
|
+
path = lines.shift
|
60
|
+
request_params = parse_json(lines.join(""))
|
61
|
+
request_params[:authenticity_token] = @form_authenticity_token
|
62
|
+
http_method = request_params["_method"]&.upcase || http_method || "GET"
|
63
|
+
[http_method, path, request_params]
|
64
|
+
end
|
65
|
+
|
66
|
+
def parse_json(string)
|
67
|
+
return {} if string.strip.empty?
|
68
|
+
JSON.parse(string)
|
69
|
+
end
|
70
|
+
|
71
|
+
def reserve_route(path, http_method: "GET", request_params: nil, format: nil)
|
72
|
+
path = "/#{path}" unless path.start_with? "/"
|
73
|
+
|
74
|
+
@request.request_method = http_method
|
75
|
+
@request.path_info = path
|
76
|
+
@request.format = format if format.present?
|
77
|
+
@request.request_parameters = request_params if request_params.present?
|
78
|
+
|
79
|
+
# req = Rack::Request.new
|
80
|
+
# env = {Rack::RACK_INPUT => StringIO.new}
|
81
|
+
|
82
|
+
res = Rails.application.routes.router.serve(@request)
|
83
|
+
res[2].body if res[2].respond_to?(:body)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -5,7 +5,7 @@ module Kamigo
|
|
5
5
|
|
6
6
|
def response_event(event, message)
|
7
7
|
return nil if message.blank?
|
8
|
-
message = JSON.parse(message) if message.is_a?(String) && message.start_with?("{")
|
8
|
+
message = JSON.parse(message) if message.is_a?(String) && (message.start_with?("{") || message.start_with?("["))
|
9
9
|
return nil if message.blank?
|
10
10
|
response = client.reply_message(event.reply_token, message)
|
11
11
|
response
|
@@ -7,6 +7,7 @@ module Kamigo
|
|
7
7
|
attr_accessor :source_type
|
8
8
|
attr_accessor :source_group_id
|
9
9
|
attr_accessor :source_user_id
|
10
|
+
attr_accessor :profile
|
10
11
|
attr_accessor :payload
|
11
12
|
|
12
13
|
def platform_params
|
@@ -14,7 +15,8 @@ module Kamigo
|
|
14
15
|
platform_type: platform_type,
|
15
16
|
source_type: source_type,
|
16
17
|
source_group_id: source_group_id,
|
17
|
-
source_user_id: source_user_id
|
18
|
+
source_user_id: source_user_id,
|
19
|
+
profile: profile
|
18
20
|
}
|
19
21
|
end
|
20
22
|
end
|
data/lib/kamigo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kamigo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.19.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- etrex
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,20 +30,20 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.25'
|
34
34
|
- - ">="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.
|
36
|
+
version: 0.25.0
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - "~>"
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '0.
|
43
|
+
version: '0.25'
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.
|
46
|
+
version: 0.25.0
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: kamiflex
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -103,10 +103,10 @@ files:
|
|
103
103
|
- README.md
|
104
104
|
- Rakefile
|
105
105
|
- app/assets/config/kamigo_manifest.js
|
106
|
-
- app/controllers/concerns/reverse_route.rb
|
107
106
|
- app/controllers/kamigo_controller.rb
|
108
107
|
- app/controllers/line_controller.rb
|
109
108
|
- config/routes.rb
|
109
|
+
- lib/generators/kamigo/install_generator.rb
|
110
110
|
- lib/generators/rails/kamigo/USAGE
|
111
111
|
- lib/generators/rails/kamigo/kamigo_generator.rb
|
112
112
|
- lib/generators/rails/kamigo/templates/edit.liff.erb
|
@@ -115,10 +115,12 @@ files:
|
|
115
115
|
- lib/generators/rails/kamigo/templates/show.line.erb
|
116
116
|
- lib/generators/rails/scaffold_controller_generator.rb
|
117
117
|
- lib/generators/rails/templates/controller.rb
|
118
|
+
- lib/generators/templates/kamigo.rb
|
118
119
|
- lib/kamigo.rb
|
119
120
|
- lib/kamigo/clients/line_client.rb
|
120
121
|
- lib/kamigo/engine.rb
|
121
122
|
- lib/kamigo/event_parsers/line_event_parser.rb
|
123
|
+
- lib/kamigo/event_processors/rails_router_processor.rb
|
122
124
|
- lib/kamigo/event_responsers/line_event_responser.rb
|
123
125
|
- lib/kamigo/events/basic_event.rb
|
124
126
|
- lib/kamigo/events/line_event.rb
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module ReverseRoute
|
2
|
-
def reserve_route(path, http_method: "GET", request_params: nil, format: nil)
|
3
|
-
path = "/#{path}" unless path.start_with? "/"
|
4
|
-
|
5
|
-
request.request_method = http_method
|
6
|
-
request.path_info = path
|
7
|
-
request.format = format if format.present?
|
8
|
-
request.request_parameters = request_params if request_params.present?
|
9
|
-
|
10
|
-
# req = Rack::Request.new
|
11
|
-
# env = {Rack::RACK_INPUT => StringIO.new}
|
12
|
-
|
13
|
-
res = Rails.application.routes.router.serve(request)
|
14
|
-
res[2].body
|
15
|
-
end
|
16
|
-
end
|