├── .gitignore ├── .rspec ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── lib └── watson │ ├── conversation.rb │ └── conversation │ └── version.rb ├── spec ├── spec_helper.rb └── watson │ └── conversation_spec.rb └── watson-conversation.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.3.1 5 | before_install: gem install bundler -v 1.13.6 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at alpha.netzilla@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in watson-conversation.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 TODO: Write your name 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Watson::Conversation 2 | DEPRICATED 3 | 4 | Watson Conversation has been renamed to Watson Assistant. 5 | 6 | I stop this library maintenance, and migrate it to a "Watson Assistant" library. 7 | https://github.com/alpha-netzilla/watson-assistant 8 | 9 | ## Installation 10 | 11 | Add this line to your application's Gemfile: 12 | 13 | ```ruby 14 | gem 'watson-conversation' 15 | ``` 16 | 17 | And then execute: 18 | 19 | $ bundle 20 | 21 | Or install it yourself as: 22 | 23 | $ gem install watson-conversation 24 | 25 | ## Usage 26 | 27 | ```ruby 28 | require 'watson/conversation' 29 | 30 | manage = Watson::Conversation::ManageDialog.new( 31 | username: [username], 32 | password: [password], 33 | workspace_id: [workspace_id], 34 | # Where to link the freely-selected user name with the conversation_id 35 | storage: 'hash' #means that you use Ruby hash. If you restart the app, the info will be disappeared. 36 | # OR 37 | storage: 'redis://127.0.0.1:6379' #means that you use exteranl database like redis(This gem currently supports redis only). 38 | ) 39 | 40 | # Get a greet message from a conversation service. 41 | puts response1 = manage.talk("user1", "") 42 | #=> {user: user1, status_code: 200, output: [\"What would you like me to do?\"]} 43 | 44 | # Get a response to a user's input. 45 | puts response2 = manage.talk("user1", "I would like you to ...") 46 | #=> {user: user1, status_code: 200, output: [\"I help you ...\"]} 47 | 48 | # Check if the user exists 49 | puts manage.has_key?("user1") 50 | 51 | # Delete the user 52 | puts manage.delete("user1") 53 | ``` 54 | 55 | ## Development 56 | 57 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 58 | 59 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 60 | 61 | ## Contributing 62 | 63 | Bug reports and pull requests are welcome on GitHub at https://github.com/alpha-netzilla/watson-conversation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 64 | 65 | 66 | ## License 67 | 68 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 69 | 70 | ## Authors 71 | * http://alpha-netzilla.blogspot.com/ 72 | 73 | [wc]: http://www.ibm.com/watson/developercloud/doc/conversation/index.html 74 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "watson/conversation" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /lib/watson/conversation.rb: -------------------------------------------------------------------------------- 1 | require "watson/conversation/version" 2 | require 'rest-client' 3 | require "json" 4 | require "thread" 5 | require "redis" 6 | 7 | 8 | 9 | module Watson 10 | module Conversation 11 | 12 | class Dialog 13 | def initialize(username: "", password: "", workspace_id: "") 14 | url = "https://#{username}:#{password}@gateway.watsonplatform.net/conversation/api" 15 | version="2017-02-03" 16 | @endpoint = "#{url}/v1/workspaces/#{workspace_id}/message?version=#{version}" 17 | end 18 | 19 | 20 | def talk(question, context) 21 | future_data = FutureData.new() 22 | 23 | if context == "" 24 | body = {}.to_json 25 | else 26 | body = { 27 | input: { 28 | text: question 29 | }, 30 | alternate_intents: true, 31 | context: context, 32 | }.to_json 33 | end 34 | 35 | Thread.start do 36 | begin 37 | response = RestClient.post @endpoint, body, content_type: :json, accept: :json 38 | code = response.code 39 | body = JSON.parse(response.body) 40 | rescue RestClient::ExceptionWithResponse => e 41 | code = e.response.code 42 | body = JSON.parse(e.response.body) 43 | end 44 | future_data.set_real_data(code, body) 45 | end 46 | 47 | return future_data 48 | end 49 | 50 | 51 | def get_data() 52 | return code, body 53 | end 54 | end 55 | 56 | 57 | class FutureData 58 | def initialize() 59 | @is_ready = false 60 | @real_data = nil 61 | 62 | @mutex = Mutex.new 63 | @cv = ConditionVariable.new 64 | end 65 | 66 | 67 | def set_real_data(code, body) 68 | @mutex.synchronize do 69 | if @is_ready == true 70 | return 71 | end 72 | end 73 | 74 | @real_data = code, body 75 | @is_ready = true 76 | 77 | @cv.broadcast 78 | end 79 | 80 | 81 | def get_data() 82 | @mutex.synchronize do 83 | while @is_ready == false 84 | @cv.wait(@mutex) 85 | end 86 | end 87 | return @real_data 88 | end 89 | end 90 | 91 | 92 | 93 | 94 | class Redis < ::Redis 95 | def fetch(user) 96 | JSON.parse(get(user)) 97 | end 98 | 99 | 100 | def store(user, data) 101 | set(user, data.to_json) 102 | end 103 | 104 | 105 | def delete(user) 106 | del(user) 107 | end 108 | 109 | 110 | def has_key?(user) 111 | exists(user) 112 | end 113 | end 114 | 115 | 116 | 117 | class ManageDialog 118 | def initialize(username: "", password: "", workspace_id: "", storage: "hash") 119 | 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." 120 | @cnv = Dialog.new( 121 | username: username, 122 | password: password, 123 | workspace_id: workspace_id 124 | ) 125 | 126 | if storage == "hash" 127 | @users = Hash.new 128 | else 129 | @users = Redis.new(:url => storage) 130 | end 131 | 132 | @mutex = Mutex.new 133 | end 134 | 135 | 136 | def users() 137 | @users 138 | end 139 | 140 | 141 | def has_key?(user) 142 | @users.has_key?(user) 143 | end 144 | 145 | 146 | def delete(user) 147 | @users.delete(user) 148 | end 149 | 150 | 151 | def talk(user, question) 152 | future_data = nil 153 | 154 | @mutex.synchronize do 155 | if @users.has_key?(user) == false 156 | future_data = @cnv.talk("", "") 157 | else 158 | future_data = @cnv.talk(question, context = @users.fetch(user)) 159 | end 160 | end 161 | 162 | code, response = future_data.get_data() 163 | 164 | output_texts = [] 165 | if code == 200 166 | context = response["context"] 167 | response["output"]["text"].each do | text | 168 | output_texts.push(text) 169 | end 170 | else 171 | #response["error"]["error"]["input.text"].each do | text | 172 | #end 173 | text = response["error"] 174 | output_texts.push(text) 175 | end 176 | 177 | @mutex.synchronize do 178 | if code == 200 179 | @users.store(user, context) 180 | else 181 | @users.delete(user) 182 | end 183 | end 184 | 185 | return {user: user, status_code: code, output: output_texts}.to_json 186 | end 187 | end 188 | end 189 | end 190 | -------------------------------------------------------------------------------- /lib/watson/conversation/version.rb: -------------------------------------------------------------------------------- 1 | module Watson 2 | module Conversation 3 | VERSION = "1.0.7" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) 2 | require "watson/conversation" 3 | -------------------------------------------------------------------------------- /spec/watson/conversation_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | 4 | 5 | describe "Version" do 6 | it "has a version number" do 7 | expect(Watson::Conversation::VERSION).not_to be nil 8 | end 9 | end 10 | 11 | 12 | 13 | shared_examples_for "conversation" do |storage| 14 | manage = Watson::Conversation::ManageDialog.new( 15 | username: [username], 16 | password: [password], 17 | workspace_id: [workspace_id], 18 | storage: 'hash' 19 | ) 20 | 21 | 22 | describe "#talk" do 23 | let(:user) {"user1"} 24 | it "responds a greet message" do 25 | expect(manage.talk(user, "")).to match(/status_code/) 26 | end 27 | 28 | it "responds to a user's input" do 29 | expect(manage.talk(user, "bar")).to match(/status_code/) 30 | end 31 | 32 | end 33 | 34 | 35 | describe "#has_key?" do 36 | let(:user1) {"user1"} 37 | let(:user2) {"user2"} 38 | it "checkes if the the user exists" do 39 | expect(manage.users.has_key?(user1)).to eq true 40 | end 41 | 42 | it "checkes if the the user doesnot exist" do 43 | expect(manage.users.has_key?(user2)).to eq false 44 | end 45 | end 46 | 47 | 48 | describe "#delete" do 49 | let(:user1) {"user1"} 50 | let(:user2) {"user2"} 51 | it "checkes if the the user exists" do 52 | expect(manage.users.delete(user1)).to include(a_string_starting_with("conversation")).or eq(1) 53 | end 54 | 55 | it "checkes if the the user doesnot exist" do 56 | expect(manage.users.delete(user2)).to eq(nil).or eq(0) 57 | end 58 | end 59 | end 60 | 61 | 62 | 63 | describe "Hash" do 64 | it_behaves_like "conversation", "hash" 65 | end 66 | 67 | 68 | describe "Redis" do 69 | it_behaves_like "conversation", "redis://127.0.0.1:6379" 70 | end 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /watson-conversation.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'watson/conversation/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "watson-conversation" 8 | spec.version = Watson::Conversation::VERSION 9 | spec.authors = ["alpha.netzilla"] 10 | spec.email = ["alpha.netzilla@gmail.com"] 11 | 12 | spec.summary = %q{Client library to use the IBM Watson Conversation service} 13 | spec.description = %q{Client library to use the IBM Watson Conversation service} 14 | spec.homepage = "https://github.com/alpha-netzilla/watson-conversation.git" 15 | spec.license = "MIT" 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject do |f| 18 | f.match(%r{^(test|spec|features)/}) 19 | end 20 | spec.bindir = "exe" 21 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 22 | spec.require_paths = ["lib"] 23 | 24 | spec.add_development_dependency "bundler", "~> 1.13" 25 | spec.add_development_dependency "rake", ~> 12.3.3 26 | spec.add_development_dependency "rspec", "~> 3.0" 27 | spec.add_development_dependency "rest-client", "~> 2.0" 28 | spec.add_development_dependency "json", "~> 2.0" 29 | spec.add_development_dependency "redis", "~> 3.3" 30 | 31 | spec.post_install_message = <<-MESSAGE 32 | ! The 'watson-conversation' gem has been deprecated and has been replaced by 'watson-assistant'. 33 | ! See: https://rubygems.org/gems/watson-assistant 34 | ! And: https://github.com/alpha-netzilla/watson-assistant 35 | MESSAGE 36 | end 37 | --------------------------------------------------------------------------------