Class: Watson::Conversation::ManageDialog

Inherits:
Object
  • Object
show all
Defined in:
lib/watson/conversation.rb

Instance Method Summary collapse

Constructor Details

#initialize(username: "", password: "", workspace_id: "", storage: "hash") ⇒ ManageDialog

Returns a new instance of ManageDialog.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/watson/conversation.rb', line 118

def initialize(username: "", password: "", workspace_id: "", storage: "hash")
     warn "[DEPRECATION] This gem has been renamed to `watson-assistant` and will no longer be supported. Please switch to `watson-assistant` as soon as possible."
	@cnv = Dialog.new(
		username: username,
		password: password,
		workspace_id: workspace_id
	)

	if storage == "hash"
		@users = Hash.new
	else
		@users = Redis.new(:url => storage)
	end

	@mutex = Mutex.new
end

Instance Method Details

#delete(user) ⇒ Object



146
147
148
# File 'lib/watson/conversation.rb', line 146

def delete(user)
	@users.delete(user)
end

#has_key?(user) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/watson/conversation.rb', line 141

def has_key?(user) 
	@users.has_key?(user)
end

#talk(user, question) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/watson/conversation.rb', line 151

def talk(user, question)
	future_data = nil

	@mutex.synchronize do
		if @users.has_key?(user) == false
			future_data = @cnv.talk("", "")
		else
			future_data = @cnv.talk(question, context = @users.fetch(user))
		end
	end

	code, response = future_data.get_data()	

	output_texts = []
	if code == 200
		context = response["context"]
		response["output"]["text"].each do | text |
			output_texts.push(text)
		end
	else
		#response["error"]["error"]["input.text"].each do | text |
		#end
		text = response["error"]
		output_texts.push(text)
	end

	@mutex.synchronize do
		if code == 200
			@users.store(user, context)
		else
			@users.delete(user)
		end
	end

	return {user: user, status_code: code, output: output_texts}.to_json
end

#usersObject



136
137
138
# File 'lib/watson/conversation.rb', line 136

def users()
	@users
end