kamigo 0.19.0 → 0.24.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -12
- data/app/controllers/line_controller.rb +2 -19
- data/lib/generators/rails/kamigo/state_generator.rb +56 -0
- data/lib/kamigo.rb +2 -0
- data/lib/kamigo/event_parsers/line_event_parser.rb +21 -3
- data/lib/kamigo/events/basic_event.rb +4 -1
- data/lib/kamigo/request_handlers/line_request_handler.rb +32 -0
- data/lib/kamigo/version.rb +1 -1
- metadata +12 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2d0e706b01b63559ccbae86d9028eaabc046f21723f08efbfe4e576cd5b79b1
|
4
|
+
data.tar.gz: 3ef12c974773d236cdbded25773193f46037bb2f97c38db262c360e00567d7cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3b4daa5ef98203bd375844b8d7fc702b4ba8d8116aed180d8c6b992e11958204f6ed8c056b59ee7483c1722113b4b783f0a0e97170c0ee0334fa8bd1702acda
|
7
|
+
data.tar.gz: e934e46ce558668684cbf09cb638ac59fd57ba07e297177d0e4dcf9332f42459ddecd27eecf6a2d96fe1d4558b7eb3244c1d9f3ce8eb1c982efdcbf3c346ece6
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# 詳細的說明文件
|
2
|
+
|
3
|
+
[Kamigo 詳細說明](https://etrex.tw/kamigo/)
|
4
|
+
|
1
5
|
# Kamigo 簡介
|
2
6
|
Kamigo 是一個基於 Rails 的 Chatbot MVC Framework。
|
3
7
|
|
@@ -18,6 +22,8 @@ cd kamigo_demo
|
|
18
22
|
# 安裝套件
|
19
23
|
bundle add kamigo
|
20
24
|
bundle add dotenv-rails
|
25
|
+
# 建立設定檔
|
26
|
+
rails g kamigo:install
|
21
27
|
# 新增 resource controller
|
22
28
|
rails g scaffold todo name desc
|
23
29
|
rails db:create
|
@@ -86,7 +92,7 @@ window.addEventListener("liff_submit", function(event){
|
|
86
92
|
https://你的網域/line
|
87
93
|
```
|
88
94
|
|
89
|
-
第一次開發 LINE Bot 的人可以服用此帖 [Webhook URL 設定 QA](/doc/
|
95
|
+
第一次開發 LINE Bot 的人可以服用此帖 [Webhook URL 設定 QA](/doc/07_setting.md#Webhook-URL-設定-QA)。
|
90
96
|
|
91
97
|
# 設定聊天機器人環境變數
|
92
98
|
請在專案根目錄下新增一個 `.env` 檔並且填入以下內容:
|
@@ -104,7 +110,7 @@ LIFF_FULL=這裡填入你的 FULL_LIFF_URL
|
|
104
110
|
- `COMPACT_LIFF_URL`、`TALL_LIFF_URL` 和 `FULL_LIFF_URL` 需要到 LINE 後台的 LIFF 分頁新增後,即可獲得一組 LIFF URL。
|
105
111
|
|
106
112
|
Kamigo 預設的 LIFF Size 為 Compact,你也可以只新增 Compact LIFF URL。
|
107
|
-
詳細的 LIFF 設定說明可以服用此帖 [LIFF 設定 QA](/doc/
|
113
|
+
詳細的 LIFF 設定說明可以服用此帖 [LIFF 設定 QA](/doc/07_setting.md#LIFF-設定-QA)。
|
108
114
|
|
109
115
|
至此串接完成。
|
110
116
|
|
@@ -137,19 +143,17 @@ Kamigo 預設使用基本的語意理解模型,會將使用者輸入視為在
|
|
137
143
|
- 守護寵物機器人
|
138
144
|
<p><img width="100" height="100" src="/doc/images/pet_loved_qrcode.png"></p>
|
139
145
|
|
140
|
-
# 詳細的說明文件
|
141
|
-
- [Kamigo 架構概觀](/doc/01_intro.md)
|
142
|
-
- [Route 的使用說明](/doc/02_route.md)
|
143
|
-
- [Controller 的使用說明](/doc/03_controller.md)
|
144
|
-
- [View 的使用說明](/doc/04_view.md)
|
145
|
-
- [Form 的使用說明](/doc/05_form.md)
|
146
|
-
- [Kamigo 相關設定與 QA](/doc/06_setting.md)
|
147
|
-
|
148
146
|
# 計畫
|
149
147
|
- 提供多種語意理解模型串接
|
148
|
+
- DialogFlow
|
149
|
+
- LUIS
|
150
150
|
- 網站使用者與聊天機器人使用者綁定
|
151
|
-
-
|
152
|
-
-
|
151
|
+
- Devise
|
152
|
+
- Log / 錯誤追蹤
|
153
|
+
- 支援其他平台
|
154
|
+
- Telegram
|
155
|
+
- Facebook Messenger
|
156
|
+
- Slack
|
153
157
|
|
154
158
|
## License
|
155
159
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -4,25 +4,8 @@ class LineController < ApplicationController
|
|
4
4
|
protect_from_forgery with: :null_session
|
5
5
|
|
6
6
|
def entry
|
7
|
-
|
8
|
-
|
9
|
-
events.each do |event|
|
10
|
-
output = process_event(event) || Kamigo.line_default_message
|
11
|
-
responser = Kamigo::EventResponsers::LineEventResponser.new
|
12
|
-
response = responser.response_event(event, output)
|
13
|
-
end
|
7
|
+
request_handler = Kamigo::RequestHandlers::LineRequestHandler.new(request, form_authenticity_token)
|
8
|
+
request_handler.handle
|
14
9
|
head :ok
|
15
10
|
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def process_event(event)
|
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?
|
25
|
-
end
|
26
|
-
nil
|
27
|
-
end
|
28
11
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
require 'rails/generators/resource_helpers'
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
module Generators
|
6
|
+
class CreateStateGenerator < NamedBase
|
7
|
+
include Rails::Generators::ResourceHelpers
|
8
|
+
namespace "devise"
|
9
|
+
argument :attributes, type: :string, default: 'kamigo_state'
|
10
|
+
|
11
|
+
class_option :timestamps, type: :boolean, default: true
|
12
|
+
|
13
|
+
def create_root_folder
|
14
|
+
path = File.join('app/views', controller_file_path)
|
15
|
+
empty_directory path unless File.directory?(path)
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_view_files
|
19
|
+
filenames.each do |filename|
|
20
|
+
template filename, File.join('app/views', controller_file_path, filename)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def attributes_names
|
27
|
+
[:id] + super
|
28
|
+
end
|
29
|
+
|
30
|
+
def filenames
|
31
|
+
[
|
32
|
+
"index.line.erb",
|
33
|
+
"show.line.erb",
|
34
|
+
"edit.liff.erb",
|
35
|
+
"new.liff.erb",
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
def full_attributes_list
|
40
|
+
if options[:timestamps]
|
41
|
+
attributes_list(attributes_names + %w(created_at updated_at))
|
42
|
+
else
|
43
|
+
attributes_list(attributes_names)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def attributes_list(attributes = attributes_names)
|
48
|
+
if self.attributes.any? {|attr| attr.name == 'password' && attr.type == :digest}
|
49
|
+
attributes = attributes.reject {|name| %w(password password_confirmation).include? name}
|
50
|
+
end
|
51
|
+
|
52
|
+
attributes.map { |a| ":#{a}"} * ', '
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/kamigo.rb
CHANGED
@@ -8,6 +8,8 @@ require "kamigo/events/line_event"
|
|
8
8
|
require "kamigo/event_parsers/line_event_parser"
|
9
9
|
require "kamigo/event_responsers/line_event_responser"
|
10
10
|
require "kamigo/event_processors/rails_router_processor"
|
11
|
+
require "kamigo/request_handlers/line_request_handler"
|
12
|
+
|
11
13
|
module Kamigo
|
12
14
|
mattr_accessor :line_default_message
|
13
15
|
@@line_default_message = {
|
@@ -12,19 +12,37 @@ module Kamigo
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def parse(event)
|
15
|
-
|
16
|
-
|
15
|
+
event_hash = JSON.parse(event.to_json, symbolize_names: true)
|
16
|
+
payload = event_hash[:src] || event_hash
|
17
17
|
line_event = Kamigo::Events::LineEvent.new
|
18
18
|
line_event.payload = payload
|
19
19
|
line_event.reply_token = event['replyToken']
|
20
|
-
line_event.profile = JSON.parse(response.body)
|
21
20
|
line_event.source_type = payload.dig(:source, :type)
|
22
21
|
line_event.source_group_id = payload.dig(:source, :groupId) || payload.dig(:source, :roomId) || payload.dig(:source, :userId)
|
23
22
|
line_event.source_user_id = payload.dig(:source, :userId) || payload.dig(:source, :groupId) || payload.dig(:source, :roomId)
|
24
23
|
line_event.message_type = payload.dig(:message, :type) || payload.dig(:type)
|
25
24
|
line_event.message = payload.dig(:message, :text) || payload.dig(:postback, :data) || payload.dig(:message, :address) || line_event.message_type
|
25
|
+
line_event.profile = get_profile(line_event)
|
26
26
|
line_event
|
27
27
|
end
|
28
|
+
|
29
|
+
def get_profile(line_event)
|
30
|
+
case line_event.source_type
|
31
|
+
when 'group'
|
32
|
+
response = client.get_group_member_profile(
|
33
|
+
line_event.source_group_id,
|
34
|
+
line_event.source_user_id
|
35
|
+
)
|
36
|
+
when 'room'
|
37
|
+
response = client.get_room_member_profile(
|
38
|
+
line_event.source_group_id,
|
39
|
+
line_event.source_user_id
|
40
|
+
)
|
41
|
+
else
|
42
|
+
response = client.get_profile(line_event.source_user_id)
|
43
|
+
end
|
44
|
+
JSON.parse(response.body)
|
45
|
+
end
|
28
46
|
end
|
29
47
|
end
|
30
48
|
end
|
@@ -13,10 +13,13 @@ module Kamigo
|
|
13
13
|
def platform_params
|
14
14
|
{
|
15
15
|
platform_type: platform_type,
|
16
|
+
message: message,
|
17
|
+
message_type: message_type,
|
16
18
|
source_type: source_type,
|
17
19
|
source_group_id: source_group_id,
|
18
20
|
source_user_id: source_user_id,
|
19
|
-
profile: profile
|
21
|
+
profile: profile,
|
22
|
+
payload: payload
|
20
23
|
}
|
21
24
|
end
|
22
25
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Kamigo
|
2
|
+
module RequestHandlers
|
3
|
+
class LineRequestHandler
|
4
|
+
def initialize(request, form_authenticity_token)
|
5
|
+
@request = request
|
6
|
+
@form_authenticity_token = form_authenticity_token
|
7
|
+
end
|
8
|
+
|
9
|
+
def handle
|
10
|
+
parser = EventParsers::LineEventParser.new
|
11
|
+
events = parser.parse_events(@request)
|
12
|
+
events.each do |event|
|
13
|
+
output = process_event(event) || Kamigo.line_default_message
|
14
|
+
responser = EventResponsers::LineEventResponser.new
|
15
|
+
response = responser.response_event(event, output)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def process_event(event)
|
22
|
+
Kamigo.line_event_processors.each do |processor|
|
23
|
+
processor.request = @request
|
24
|
+
processor.form_authenticity_token = @form_authenticity_token
|
25
|
+
output = processor.process(event)
|
26
|
+
return output if output.present?
|
27
|
+
end
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
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.24.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- etrex
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,40 +30,40 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.28'
|
34
34
|
- - ">="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.
|
36
|
+
version: 0.28.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.28'
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.
|
46
|
+
version: 0.28.0
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: kamiflex
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0.
|
53
|
+
version: '0.14'
|
54
54
|
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: 0.
|
56
|
+
version: 0.14.0
|
57
57
|
type: :runtime
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
61
|
- - "~>"
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: '0.
|
63
|
+
version: '0.14'
|
64
64
|
- - ">="
|
65
65
|
- !ruby/object:Gem::Version
|
66
|
-
version: 0.
|
66
|
+
version: 0.14.0
|
67
67
|
- !ruby/object:Gem::Dependency
|
68
68
|
name: line-bot-api
|
69
69
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- lib/generators/kamigo/install_generator.rb
|
110
110
|
- lib/generators/rails/kamigo/USAGE
|
111
111
|
- lib/generators/rails/kamigo/kamigo_generator.rb
|
112
|
+
- lib/generators/rails/kamigo/state_generator.rb
|
112
113
|
- lib/generators/rails/kamigo/templates/edit.liff.erb
|
113
114
|
- lib/generators/rails/kamigo/templates/index.line.erb
|
114
115
|
- lib/generators/rails/kamigo/templates/new.liff.erb
|
@@ -125,6 +126,7 @@ files:
|
|
125
126
|
- lib/kamigo/events/basic_event.rb
|
126
127
|
- lib/kamigo/events/line_event.rb
|
127
128
|
- lib/kamigo/railtie.rb
|
129
|
+
- lib/kamigo/request_handlers/line_request_handler.rb
|
128
130
|
- lib/kamigo/version.rb
|
129
131
|
homepage: https://github.com/etrex/kamigo
|
130
132
|
licenses:
|