lita-talk 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 38b90ebfb227ce96dc0b0c89dac125a6cf00cf72
4
- data.tar.gz: 84be5598b89b6341efefaafc42e1945a89f59442
2
+ SHA256:
3
+ metadata.gz: 9136884bc2b6ed86bba33251a4f6efd971289944805071cbbfe3f8c9da6e6ab6
4
+ data.tar.gz: 60a5adef28a1056efe4790823712549d99274d394e368634d789b2f42df68a83
5
5
  SHA512:
6
- metadata.gz: 1f903894538c30335b9e82557994acd2025e8484bfb8633c4a1f6e85c5620480e24cbf6ad99bb70ac71a7daa978c471c4e7ae931e4716dc5f5dddc77b147a215
7
- data.tar.gz: 08a43863c82d660bf6d497a28504506a64c0e2afcc5c0eef1dfcbaab0d16aa094364082767c69b2ba74a21f8069fdc297d73dae12c332acb74d68d7cba1e337c
6
+ metadata.gz: c1b2b9b686aee1a3c93e4713da95de29bf79cfdbecbb8293a62320b4cd41fd0aff1a433753fd4362d98c1a0920ef6d8a784d244a18368993a4c51b0c413206c1
7
+ data.tar.gz: 05c381b60282bad8af320a945e827e3c1f016d9aa96f601056c09ab560f250c045f4058eb79de8e5675c805f145f39e98013c75714df22ef8aa57f47331baaf7
data/README.md CHANGED
@@ -19,17 +19,19 @@ gem "lita-talk"
19
19
  Lita.configure do |config|
20
20
  ...
21
21
  # required
22
- config.handlers.talk.docomo_api_key = 'xxx'
23
-
24
- # optional (https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=dialogue&p_name=api_1#tag01)
25
- # 20 : 関西弁キャラ
26
- # 30 : 赤ちゃんキャラ
27
- # 指定なし : デフォルトキャラ
28
- config.handlers.talk.docomo_character_id = 20
29
- # config.handlers.talk.docomo_character_id = [nil, 20, 30] # at random in array
22
+ config.handlers.talk.docomo_api_key = 'xxx'
30
23
  end
31
24
  ```
32
25
 
33
26
  ## Usage
34
27
 
35
- ![ss 2015-04-15 at 16 24 51](https://cloud.githubusercontent.com/assets/1041857/7153973/25f15966-e38c-11e4-9c26-3aef61e4e7fa.png)
28
+ ```
29
+ Lita > lita type
30
+ default
31
+ Lita > lita おはよう
32
+ おはありりー
33
+ Lita > lita type hakata
34
+ ok
35
+ Lita > lita おはよう
36
+ おはようだの
37
+ ```
@@ -0,0 +1,7 @@
1
+ version: '3'
2
+
3
+ services:
4
+ redis:
5
+ image: redis:latest
6
+ ports:
7
+ - "6379:6379"
@@ -1,38 +1,148 @@
1
- require 'docomoru'
1
+ require 'faraday'
2
+ require 'faraday_middleware'
2
3
 
3
4
  module Lita
4
5
  module Handlers
5
6
  class Talk < Handler
6
- config :docomo_api_key, type: String, required: true
7
- config :docomo_character_id, required: false
7
+ # @see: https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=natural_dialogue&p_name=api_4#tag01
8
+ # @see: https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=natural_dialogue&p_name=api_4_user_registration#tag01
9
+ API_ENDPOINT = 'https://api.apigw.smt.docomo.ne.jp'.freeze
10
+ API_DIALOGUE_PATH = '/naturalChatting/v1/dialogue'.freeze
11
+ API_REGISTRATION_PATH = '/naturalChatting/v1/registration'.freeze
12
+
13
+ # @see: https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=natural_dialogue&p_name=api_6#tag01
14
+ CHARACTER_TYPES = %w[default ehime1 ehime2 ehime3 kansai hakata fukushima mie maiko ojo bushi gyaru burikko akachan]
15
+
16
+ config :docomo_api_key, type: String, required: true
17
+
18
+ route(
19
+ /^(.+)/,
20
+ :talk,
21
+ command: true,
22
+ help: {
23
+ 'talk' => "Talk with you if given message didn't match any other handlers."
24
+ }
25
+ )
26
+
27
+ route(
28
+ /^type\s(.+)$/,
29
+ :update_character_type,
30
+ command: true,
31
+ help: {
32
+ 'type (default|ehime1|ehime2|ehime3|kansai|hakata|fukushima|mie|maiko|ojo|bushi|gyaru|burikko|akachan)' => "Change type of bot"
33
+ }
34
+ )
35
+
36
+ route(
37
+ /^type$/,
38
+ :show_type,
39
+ command: true,
40
+ help: {
41
+ 'type' => 'Show type of bot'
42
+ }
43
+ )
8
44
 
9
- route(/^(.+)/, :talk, command: true, help: {
10
- "talk" => "Talk with you if given message didn't match any other handlers."
11
- })
12
45
  def talk(response)
13
- @robot.handlers.map do |handler|
14
- next nil if handler == self.class
15
- next nil unless handler.respond_to?(:routes)
16
- handler.routes
17
- end.flatten.compact.each do |route|
18
- next if !route.command
19
- return if response.matches.flatten[0].match(route.pattern)
46
+ return unless command_missing?(response)
47
+
48
+ response.reply create_dialogue(response)
49
+ end
50
+
51
+ def show_type(response)
52
+ response.reply character_type
53
+ end
54
+
55
+ def update_character_type(response)
56
+ type = response.matches.dig(0, 0)
57
+
58
+ if CHARACTER_TYPES.include?(type)
59
+ redis.set('character_type', type)
60
+ response.reply 'ok'
61
+ else
62
+ response.reply("Invalid type: #{type}.\nValid types: #{CHARACTER_TYPES.join(', ')}")
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def character_type
69
+ redis.get('character_type') || 'default'
70
+ end
71
+
72
+ def client_data
73
+ type = character_type
74
+ return {} if type == 'default'
75
+
76
+ {
77
+ option: {
78
+ t: type
79
+ }
80
+ }
81
+ end
82
+
83
+ def create_dialogue(response)
84
+ context = fetch_context(response)
85
+
86
+ input_text = response.matches.dig(0, 0)
87
+
88
+ resp = faraday.post do |req|
89
+ req.url API_DIALOGUE_PATH
90
+ req.body = {
91
+ language: 'ja-JP',
92
+ appId: context,
93
+ voiceText: input_text,
94
+ botId: 'Chatting',
95
+ clientData: client_data
96
+ }
20
97
  end
21
98
 
22
- if response.matches.flatten[0].match(/ところで|それはそうと|そういえば|BTW/)
23
- Lita.redis.del context_key(response)
99
+ resp.body.dig('systemText', 'expression')
100
+ end
101
+
102
+ def command_missing?(response)
103
+ all_handler_routes.each do |route|
104
+ next unless route.command
105
+ return false if response.matches.flatten[0].match(route.pattern)
24
106
  end
25
107
 
26
- context = Lita.redis.get context_key(response)
27
- api_response = client.create_dialogue(response.message.body, params(context))
28
- Lita.redis.setex context_key(response), 600, api_response.body["context"]
29
- response.reply api_response.body["utt"]
108
+ true
30
109
  end
31
110
 
32
- private
111
+ def all_handler_routes
112
+ robot.handlers.map do |handler|
113
+ next nil unless handler.respond_to?(:routes)
114
+ next handler.routes.slice(1..-1) if handler == self.class
33
115
 
34
- def client
35
- @client ||= Docomoru::Client.new(api_key: config.docomo_api_key)
116
+ handler.routes
117
+ end.flatten.compact
118
+ end
119
+
120
+ def faraday
121
+ @faraday ||= Faraday.new(API_ENDPOINT) do |conn|
122
+ conn.request :json
123
+ conn.headers['Content-Type'] = 'application/json;charset=UTF-8'
124
+ conn.params['APIKEY'] = config.docomo_api_key
125
+ conn.response :json
126
+ conn.adapter Faraday.default_adapter
127
+ end
128
+ end
129
+
130
+ def fetch_context(response)
131
+ key = context_key(response)
132
+ context = redis.get(key)
133
+ return context if context
134
+
135
+ resp = faraday.post do |req|
136
+ req.url API_REGISTRATION_PATH
137
+ req.body = {
138
+ botId: 'Chatting',
139
+ appKind: 'bot'
140
+ }
141
+ end
142
+
143
+ context = resp.body['appId']
144
+ redis.set(key, context)
145
+ context
36
146
  end
37
147
 
38
148
  def context_key(response)
@@ -42,15 +152,6 @@ module Lita
42
152
  "lita-talk:#{response.message.source.room}"
43
153
  end
44
154
  end
45
-
46
- def params(context)
47
- {
48
- context: context,
49
- t: Array(config.docomo_character_id).sample,
50
- }.reject do |key, value|
51
- value.nil?
52
- end
53
- end
54
155
  end
55
156
 
56
157
  Lita.register_handler(Talk)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-talk"
3
- spec.version = "0.1.5"
3
+ spec.version = "0.1.6"
4
4
  spec.authors = ["fukayatsu"]
5
5
  spec.email = ["fukayatsu@gmail.com"]
6
6
  spec.description = "Talk with you if given message didn't match any other handlers."
@@ -15,7 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.require_paths = ["lib"]
16
16
 
17
17
  spec.add_runtime_dependency "lita", "~> 4.3"
18
- spec.add_runtime_dependency "docomoru", "~> 0.1.3 "
18
+ spec.add_runtime_dependency "faraday"
19
+ spec.add_runtime_dependency 'faraday_middleware'
19
20
 
20
21
  spec.add_development_dependency "bundler", "~> 1.3"
21
22
  spec.add_development_dependency "pry-byebug"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-talk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - fukayatsu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-15 00:00:00.000000000 Z
11
+ date: 2018-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -25,19 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.3'
27
27
  - !ruby/object:Gem::Dependency
28
- name: docomoru
28
+ name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.3
33
+ version: '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.1.3
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
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'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -120,6 +134,7 @@ files:
120
134
  - LICENSE
121
135
  - README.md
122
136
  - Rakefile
137
+ - docker-compose.yml
123
138
  - lib/lita-talk.rb
124
139
  - lib/lita/handlers/talk.rb
125
140
  - lita-talk.gemspec
@@ -148,11 +163,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
163
  version: '0'
149
164
  requirements: []
150
165
  rubyforge_project:
151
- rubygems_version: 2.4.5
166
+ rubygems_version: 2.7.6
152
167
  signing_key:
153
168
  specification_version: 4
154
169
  summary: Talk with you if given message didn't match any other handlers.
155
170
  test_files:
156
171
  - spec/lita/handlers/talk_spec.rb
157
172
  - spec/spec_helper.rb
158
- has_rdoc: