watson-assistant 1.0.2 → 1.0.3
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 +4 -4
- data/Gemfile.lock +53 -0
- data/README.md +20 -7
- data/lib/watson/assistant.rb +1 -123
- data/lib/watson/assistant/dialog.rb +49 -0
- data/lib/watson/assistant/manage.rb +86 -0
- data/lib/watson/assistant/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0f42066725c714943c166bbdc840d5daec03fd20d7388112a9923ed659f7a44
|
4
|
+
data.tar.gz: 9ec171368bb3de5f3d5b3e2a0bdefde0d7d646ee44540f4d24b38f1f25a11482
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a141a7e63bbb8e0dc0b42bf1b702d94283ad4d0534c5d56cc498197fd3ba17250bc45b8ce0235a4b67516c9e2f420adaadb793c866f006b4d4043d204d2c8d7f
|
7
|
+
data.tar.gz: 3e43bee62e27afd4280881124a261debd3711092b91701ea40290e547b802ee6d65465054b8ec47fcaea5cbb3196af633496664fb0577076c576c91b237dc349
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
watson-assistant (1.0.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
domain_name (0.5.20180417)
|
11
|
+
unf (>= 0.0.5, < 1.0.0)
|
12
|
+
http-cookie (1.0.3)
|
13
|
+
domain_name (~> 0.5)
|
14
|
+
mime-types (3.1)
|
15
|
+
mime-types-data (~> 3.2015)
|
16
|
+
mime-types-data (3.2016.0521)
|
17
|
+
netrc (0.11.0)
|
18
|
+
rake (10.5.0)
|
19
|
+
redis (3.3.3)
|
20
|
+
rest-client (2.0.0)
|
21
|
+
http-cookie (>= 1.0.2, < 2.0)
|
22
|
+
mime-types (>= 1.16, < 4.0)
|
23
|
+
netrc (~> 0.8)
|
24
|
+
rspec (3.7.0)
|
25
|
+
rspec-core (~> 3.7.0)
|
26
|
+
rspec-expectations (~> 3.7.0)
|
27
|
+
rspec-mocks (~> 3.7.0)
|
28
|
+
rspec-core (3.7.1)
|
29
|
+
rspec-support (~> 3.7.0)
|
30
|
+
rspec-expectations (3.7.0)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.7.0)
|
33
|
+
rspec-mocks (3.7.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.7.0)
|
36
|
+
rspec-support (3.7.1)
|
37
|
+
unf (0.1.4)
|
38
|
+
unf_ext
|
39
|
+
unf_ext (0.0.7.5)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
bundler (~> 1.16)
|
46
|
+
rake (~> 10.0)
|
47
|
+
redis (~> 3.3)
|
48
|
+
rest-client (~> 2.0)
|
49
|
+
rspec (~> 3.0)
|
50
|
+
watson-assistant!
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
1.16.1
|
data/README.md
CHANGED
@@ -19,18 +19,31 @@ Or install it yourself as:
|
|
19
19
|
$ gem install watson-assistant
|
20
20
|
|
21
21
|
## Usage
|
22
|
+
```sh
|
23
|
+
export USERNAME="***"
|
24
|
+
export PASSWORD="***"
|
25
|
+
export WORKSPACE_ID="***"
|
26
|
+
|
27
|
+
# Optional
|
28
|
+
# Default region is "gateway.watsonplatform.net"
|
29
|
+
export REGION="gateway.watsonplatform.net"
|
30
|
+
|
31
|
+
# Optional
|
32
|
+
# Default storage is ruby hash.
|
33
|
+
# You can select ruby hash or a redis server for managing users.
|
34
|
+
export STORAGE="hash" # Default
|
35
|
+
#export STORAG="redis://127.0.0.1:6379"
|
36
|
+
```
|
22
37
|
|
23
38
|
```ruby
|
24
39
|
require 'watson/assistant'
|
25
40
|
|
26
41
|
manage = Watson::Assistant::ManageDialog.new(
|
27
|
-
username: [
|
28
|
-
password: [
|
29
|
-
workspace_id: [
|
30
|
-
|
31
|
-
storage:
|
32
|
-
# OR
|
33
|
-
storage: 'redis://127.0.0.1:6379' #means that you use exteranl database like redis(This gem currently supports redis only).
|
42
|
+
username: ENV["USERNAME"],
|
43
|
+
password: ENV["PASSWORD"],
|
44
|
+
workspace_id: ENV["WORKSPACE_ID"],
|
45
|
+
region: ENV["REGION"],
|
46
|
+
storage: ENV["STORAGE"]
|
34
47
|
)
|
35
48
|
|
36
49
|
# Get a greet message from a assistant service.
|
data/lib/watson/assistant.rb
CHANGED
@@ -1,128 +1,6 @@
|
|
1
|
-
require "watson/assistant/version"
|
2
1
|
require 'rest-client'
|
3
2
|
require "json"
|
4
3
|
require "thread"
|
5
4
|
require "redis"
|
5
|
+
require "watson/assistant/manage"
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
module Watson
|
10
|
-
module Assistant
|
11
|
-
|
12
|
-
class Dialog
|
13
|
-
def initialize(username: "", password: "", workspace_id: "")
|
14
|
-
url = "https://#{username}:#{password}@gateway.watsonplatform.net/assistant/api"
|
15
|
-
version="2018-02-16"
|
16
|
-
@endpoint = "#{url}/v1/workspaces/#{workspace_id}/message?version=#{version}"
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
def talk(question, context)
|
21
|
-
if context == ""
|
22
|
-
body = {}.to_json
|
23
|
-
else
|
24
|
-
body = {
|
25
|
-
input: {
|
26
|
-
text: question
|
27
|
-
},
|
28
|
-
alternate_intents: true,
|
29
|
-
context: context,
|
30
|
-
}.to_json
|
31
|
-
end
|
32
|
-
|
33
|
-
begin
|
34
|
-
response = RestClient.post @endpoint, body, content_type: :json, accept: :json
|
35
|
-
code = response.code
|
36
|
-
body = JSON.parse(response.body)
|
37
|
-
rescue RestClient::ExceptionWithResponse => e
|
38
|
-
code = e.response.code
|
39
|
-
body = JSON.parse(e.response.body)
|
40
|
-
end
|
41
|
-
|
42
|
-
return code, body
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
class Redis < ::Redis
|
49
|
-
def fetch(user)
|
50
|
-
JSON.parse(get(user))
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
def store(user, data)
|
55
|
-
set(user, data.to_json)
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
def delete(user)
|
60
|
-
del(user)
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
def has_key?(user)
|
65
|
-
exists(user)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
class ManageDialog
|
72
|
-
def initialize(username: "", password: "", workspace_id: "", storage: "hash")
|
73
|
-
@cnv = Dialog.new(
|
74
|
-
username: username,
|
75
|
-
password: password,
|
76
|
-
workspace_id: workspace_id
|
77
|
-
)
|
78
|
-
|
79
|
-
if storage == "hash"
|
80
|
-
@users = Hash.new
|
81
|
-
else
|
82
|
-
@users = Redis.new(:url => storage)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
def users()
|
88
|
-
@users
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
def has_key?(user)
|
93
|
-
@users.has_key?(user)
|
94
|
-
end
|
95
|
-
|
96
|
-
|
97
|
-
def delete(user)
|
98
|
-
@users.delete(user)
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
def talk(user, question)
|
103
|
-
future_data = nil
|
104
|
-
|
105
|
-
if @users.has_key?(user) == false
|
106
|
-
code, body = @cnv.talk("", "")
|
107
|
-
else
|
108
|
-
code, body = @cnv.talk(question, context = @users.fetch(user))
|
109
|
-
end
|
110
|
-
|
111
|
-
if code == 200
|
112
|
-
context = body["context"]
|
113
|
-
output = body["output"]["text"]
|
114
|
-
else
|
115
|
-
output = body["error"]
|
116
|
-
end
|
117
|
-
|
118
|
-
if code == 200
|
119
|
-
@users.store(user, context)
|
120
|
-
else
|
121
|
-
@users.delete(user)
|
122
|
-
end
|
123
|
-
|
124
|
-
return {user: user, status_code: code, output: output}.to_json
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Watson
|
5
|
+
module Assistant
|
6
|
+
|
7
|
+
class Dialog
|
8
|
+
def initialize(config)
|
9
|
+
username = config[:username]
|
10
|
+
password = config[:password]
|
11
|
+
workspace_id = config[:workspace_id]
|
12
|
+
region = config[:region] || "gateway.watsonplatform.net"
|
13
|
+
|
14
|
+
url = "https://#{username}:#{password}@#{region}/assistant/api"
|
15
|
+
version="2018-02-16"
|
16
|
+
@endpoint = "#{url}/v1/workspaces/#{workspace_id}/message?version=#{version}"
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def talk(question, context)
|
21
|
+
if context == ""
|
22
|
+
body = {}.to_json
|
23
|
+
else
|
24
|
+
body = {
|
25
|
+
input: {
|
26
|
+
text: question
|
27
|
+
},
|
28
|
+
alternate_intents: true,
|
29
|
+
context: context,
|
30
|
+
}.to_json
|
31
|
+
end
|
32
|
+
|
33
|
+
begin
|
34
|
+
response = RestClient.post @endpoint, body, content_type: :json, accept: :json
|
35
|
+
code = response.code
|
36
|
+
body = JSON.parse(response.body)
|
37
|
+
rescue RestClient::ExceptionWithResponse => e
|
38
|
+
code = e.response.code
|
39
|
+
body = JSON.parse(e.response.body)
|
40
|
+
end
|
41
|
+
|
42
|
+
return code, body
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require "json"
|
3
|
+
require "watson/assistant/dialog"
|
4
|
+
|
5
|
+
module Watson
|
6
|
+
module Assistant
|
7
|
+
class Manage
|
8
|
+
def initialize(config)
|
9
|
+
storage = config[:storage] || "hash"
|
10
|
+
@cnv = Dialog.new(
|
11
|
+
config
|
12
|
+
)
|
13
|
+
|
14
|
+
if storage == "hash"
|
15
|
+
@users = Hash.new
|
16
|
+
else
|
17
|
+
@users = Redis.new(:url => storage)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def users()
|
23
|
+
@users
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def has_key?(user)
|
28
|
+
@users.has_key?(user)
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def delete(user)
|
33
|
+
@users.delete(user)
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def talk(user, question)
|
38
|
+
future_data = nil
|
39
|
+
|
40
|
+
if @users.has_key?(user) == false
|
41
|
+
code, body = @cnv.talk("", "")
|
42
|
+
else
|
43
|
+
code, body = @cnv.talk(question, context = @users.fetch(user))
|
44
|
+
end
|
45
|
+
|
46
|
+
if code == 200
|
47
|
+
context = body["context"]
|
48
|
+
output = body["output"]["text"]
|
49
|
+
else
|
50
|
+
output = body["error"]
|
51
|
+
end
|
52
|
+
|
53
|
+
if code == 200
|
54
|
+
@users.store(user, context)
|
55
|
+
else
|
56
|
+
@users.delete(user)
|
57
|
+
end
|
58
|
+
|
59
|
+
return {user: user, status_code: code, output: output}.to_json
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
class Redis < ::Redis
|
65
|
+
def fetch(user)
|
66
|
+
JSON.parse(get(user))
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
def store(user, data)
|
71
|
+
set(user, data.to_json)
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def delete(user)
|
76
|
+
del(user)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def has_key?(user)
|
81
|
+
exists(user)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: watson-assistant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alpha.netzilla
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,12 +92,15 @@ files:
|
|
92
92
|
- ".travis.yml"
|
93
93
|
- CODE_OF_CONDUCT.md
|
94
94
|
- Gemfile
|
95
|
+
- Gemfile.lock
|
95
96
|
- LICENSE.txt
|
96
97
|
- README.md
|
97
98
|
- Rakefile
|
98
99
|
- bin/console
|
99
100
|
- bin/setup
|
100
101
|
- lib/watson/assistant.rb
|
102
|
+
- lib/watson/assistant/dialog.rb
|
103
|
+
- lib/watson/assistant/manage.rb
|
101
104
|
- lib/watson/assistant/version.rb
|
102
105
|
- watson-assistant.gemspec
|
103
106
|
homepage: https://github.com/alpha-netzilla/watson-assistant.git
|