kamigo 0.2.6 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc8d59418cce03e5e57afafe5d8ea1a7a8b966fadcaa45015f5019f453e7d48c
4
- data.tar.gz: fd5b7a54099975c076280619c1dd8d5758659f164323f760adb6f41ba92a6803
3
+ metadata.gz: 3e7b37bfed45e62c0e1d58e1194c725819ee3f95553349970eeb2ca2b85365e0
4
+ data.tar.gz: 44a294d0412bc531a5274a2fe039d4c486f499335363ec94b39aa1423189deec
5
5
  SHA512:
6
- metadata.gz: 7fc6c0a5ca1f7407faa23e2127119a9c1f5d1b69ca8c9e9aa798e00fc7cbea3d99911f321b81e78fc2a0bdeb591b22883db868ba98aa1397a8b8a342e5206183
7
- data.tar.gz: 82622d9db23498669245ac69c157bd8070fddfea19f406d07e66f1fe53362e35c15a4c5c5fbf88621573db207df87b23286ba8247e730df4898af0587ecc9e94
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
- body = request.body.read
9
- events = client.parse_events_from(body)
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
- reply_token = event['replyToken']
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
- response = client.reply_message(reply_token, JSON.parse(output))
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
- response = client.reply_message(reply_token, {
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
@@ -0,0 +1,7 @@
1
+ module Kamigo
2
+ module Events
3
+ class LineEvent < BasicEvent
4
+ attr_accessor :reply_token
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Kamigo
2
- VERSION = '0.2.6'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.6
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-08-18 00:00:00.000000000 Z
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.11.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.11.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