kamigo 0.2.6 → 0.3.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 +2 -0
- data/app/controllers/line_controller.rb +7 -28
- data/lib/kamigo.rb +5 -1
- data/lib/kamigo/clients/line_client.rb +17 -0
- data/lib/kamigo/event_parsers/line_event_parser.rb +29 -0
- data/lib/kamigo/event_responsers/line_event_responser.rb +13 -0
- data/lib/kamigo/events/basic_event.rb +13 -0
- data/lib/kamigo/events/line_event.rb +7 -0
- data/lib/kamigo/version.rb +1 -1
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e7b37bfed45e62c0e1d58e1194c725819ee3f95553349970eeb2ca2b85365e0
|
4
|
+
data.tar.gz: 44a294d0412bc531a5274a2fe039d4c486f499335363ec94b39aa1423189deec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9bc9e189b091e4f1db09155796ce68db4e51144a9a2c4cd9a4d928dfe15246f5023cfec052271b158005e1faa036248d0242f3334001e1b59828c6198a6f12e5
|
7
|
+
data.tar.gz: 5bde7f7851c7a5c18db339d26de3812a5be08d475235fd9749d5788ff4718c684c4aaf6c90900e174b91c24c372e87575e8eea367931eed128779421b9dda9c8
|
data/README.md
CHANGED
@@ -71,6 +71,8 @@ Kamigo 預設使用基本的語意理解模型,會將使用者輸入視為在
|
|
71
71
|
只要和你的聊天機器人說 `/`,就能看見首頁的樣子。
|
72
72
|
|
73
73
|
# 使用 kamigo 製作的聊天機器人
|
74
|
+
- [kamigo demo](https://github.com/etrex/kamigo_demo)
|
75
|
+
![](/doc/images/kamigo_demo_qrcode.png)
|
74
76
|
- [健身紀錄機器人: Muscle-Man](https://github.com/louis70109/muscle_man)
|
75
77
|
![](https://camo.githubusercontent.com/c0d2233e3182c822ffc0816fe0cbb5610fe67914/68747470733a2f2f692e696d6775722e636f6d2f4f7930423257326d2e706e67)
|
76
78
|
|
@@ -5,8 +5,8 @@ class LineController < ApplicationController
|
|
5
5
|
protect_from_forgery with: :null_session
|
6
6
|
|
7
7
|
def entry
|
8
|
-
|
9
|
-
events =
|
8
|
+
parser = Kamigo::EventParsers::LineEventParser.new
|
9
|
+
events = parser.parse_events(request)
|
10
10
|
events.each do |event|
|
11
11
|
process_event(event)
|
12
12
|
end
|
@@ -16,17 +16,16 @@ class LineController < ApplicationController
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def process_event(event)
|
19
|
-
|
20
|
-
http_method, path, request_params = language_understanding(event.message['text'])
|
21
|
-
inject_event(event, to: request_params)
|
19
|
+
http_method, path, request_params = language_understanding(event.message)
|
22
20
|
encoded_path = URI.encode(path)
|
23
21
|
output = reserve_route(encoded_path, http_method: http_method, request_params: request_params, format: :line)
|
24
|
-
|
22
|
+
responser = Kamigo::EventResponsers::LineEventResponser.new
|
23
|
+
response = responser.response_event(event, output)
|
25
24
|
puts response.body
|
26
|
-
|
27
25
|
rescue NoMethodError => e
|
28
26
|
puts e.full_message
|
29
|
-
|
27
|
+
responser = Kamigo::EventResponsers::LineEventResponser.new
|
28
|
+
response = responser.response_event(event, {
|
30
29
|
type: "text",
|
31
30
|
text: "404 not found"
|
32
31
|
})
|
@@ -51,24 +50,4 @@ class LineController < ApplicationController
|
|
51
50
|
JSON.parse(string)
|
52
51
|
end
|
53
52
|
|
54
|
-
def client
|
55
|
-
@client ||= Line::Bot::Client.new do |config|
|
56
|
-
config.channel_secret = ENV['LINE_CHANNEL_SECRET']
|
57
|
-
config.channel_token = ENV['LINE_CHANNEL_TOKEN']
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def validate_signature(request, body)
|
62
|
-
signature = request.env['HTTP_X_LINE_SIGNATURE']
|
63
|
-
client.validate_signature(body, signature)
|
64
|
-
end
|
65
|
-
|
66
|
-
def inject_event(event, to: {})
|
67
|
-
event_hash = JSON.parse(event.to_json, symbolize_names: true)[:src]
|
68
|
-
to[:event] = event_hash
|
69
|
-
to[:platform_type] = 'line'
|
70
|
-
to[:source_type] = event_hash.dig(:source, :type)
|
71
|
-
to[:source_group_id] = event_hash.dig(:source, :groupId) || event_hash.dig(:source, :roomId) || event_hash.dig(:source, :userId)
|
72
|
-
to[:source_user_id] = event_hash.dig(:source, :userId) || event_hash.dig(:source, :groupId) || event_hash.dig(:source, :roomId)
|
73
|
-
end
|
74
53
|
end
|
data/lib/kamigo.rb
CHANGED
@@ -2,7 +2,11 @@ require "kamigo/engine"
|
|
2
2
|
require "line-bot-api"
|
3
3
|
require "kamiflex"
|
4
4
|
require "kamiliff"
|
5
|
-
|
5
|
+
require "kamigo/clients/line_client"
|
6
|
+
require "kamigo/events/basic_event"
|
7
|
+
require "kamigo/events/line_event"
|
8
|
+
require "kamigo/event_parsers/line_event_parser"
|
9
|
+
require "kamigo/event_responsers/line_event_responser"
|
6
10
|
module Kamigo
|
7
11
|
# Your code goes here...
|
8
12
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Kamigo
|
2
|
+
module Clients
|
3
|
+
module LineClient
|
4
|
+
def client
|
5
|
+
@client ||= Line::Bot::Client.new do |config|
|
6
|
+
config.channel_secret = ENV['LINE_CHANNEL_SECRET']
|
7
|
+
config.channel_token = ENV['LINE_CHANNEL_TOKEN']
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_signature(request, body)
|
12
|
+
signature = request.env['HTTP_X_LINE_SIGNATURE']
|
13
|
+
client.validate_signature(body, signature)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Kamigo
|
2
|
+
module EventParsers
|
3
|
+
class LineEventParser
|
4
|
+
include Kamigo::Clients::LineClient
|
5
|
+
|
6
|
+
def parse_events(request)
|
7
|
+
body = request.body.read
|
8
|
+
events = client.parse_events_from(body)
|
9
|
+
events.map do |event|
|
10
|
+
parse(event)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse(event)
|
15
|
+
payload = JSON.parse(event.to_json, symbolize_names: true)[:src]
|
16
|
+
line_event = Kamigo::Events::LineEvent.new
|
17
|
+
line_event.payload = payload
|
18
|
+
line_event.platform_type = 'line'
|
19
|
+
line_event.reply_token = event['replyToken']
|
20
|
+
line_event.source_type = payload.dig(:source, :type)
|
21
|
+
line_event.source_group_id = payload.dig(:source, :groupId) || payload.dig(:source, :roomId) || payload.dig(:source, :userId)
|
22
|
+
line_event.source_user_id = payload.dig(:source, :userId) || payload.dig(:source, :groupId) || payload.dig(:source, :roomId)
|
23
|
+
line_event.message_type = payload.dig(:message, :type) || payload.dig(:type)
|
24
|
+
line_event.message = payload.dig(:message, :text) || payload.dig(:message, :address) || line_event.message_type
|
25
|
+
line_event
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Kamigo
|
2
|
+
module EventResponsers
|
3
|
+
class LineEventResponser
|
4
|
+
include Kamigo::Clients::LineClient
|
5
|
+
|
6
|
+
def response_event(event, message)
|
7
|
+
message = JSON.parse(message) if message.is_a? String
|
8
|
+
response = client.reply_message(event.reply_token, message)
|
9
|
+
response
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Kamigo
|
2
|
+
module Events
|
3
|
+
class BasicEvent
|
4
|
+
attr_accessor :platform_type
|
5
|
+
attr_accessor :message
|
6
|
+
attr_accessor :message_type
|
7
|
+
attr_accessor :source_type
|
8
|
+
attr_accessor :source_group_id
|
9
|
+
attr_accessor :source_user_id
|
10
|
+
attr_accessor :payload
|
11
|
+
end
|
12
|
+
end
|
13
|
+
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- etrex
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.12.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.12.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: kamiflex
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -103,7 +103,12 @@ files:
|
|
103
103
|
- lib/generators/rails/scaffold_controller_generator.rb
|
104
104
|
- lib/generators/rails/templates/controller.rb
|
105
105
|
- lib/kamigo.rb
|
106
|
+
- lib/kamigo/clients/line_client.rb
|
106
107
|
- lib/kamigo/engine.rb
|
108
|
+
- lib/kamigo/event_parsers/line_event_parser.rb
|
109
|
+
- lib/kamigo/event_responsers/line_event_responser.rb
|
110
|
+
- lib/kamigo/events/basic_event.rb
|
111
|
+
- lib/kamigo/events/line_event.rb
|
107
112
|
- lib/kamigo/railtie.rb
|
108
113
|
- lib/kamigo/version.rb
|
109
114
|
homepage: https://github.com/etrex/kamigo
|