From 7a2944cf99043d63f6aeeadbde39a1895331a883 Mon Sep 17 00:00:00 2001 From: Matheus Poleza Date: Sat, 14 Aug 2021 14:03:38 -0300 Subject: [PATCH] feat: adds ruby example structure --- ruby/Gemfile | 5 +++++ ruby/Gemfile.lock | 33 +++++++++++++++++++++++++++++ ruby/README.md | 46 ++++++++++++++++++++++++++++++++++++++++ ruby/main.rb | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 ruby/Gemfile create mode 100644 ruby/Gemfile.lock create mode 100644 ruby/README.md create mode 100644 ruby/main.rb diff --git a/ruby/Gemfile b/ruby/Gemfile new file mode 100644 index 0000000..3a03463 --- /dev/null +++ b/ruby/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem 'faraday' diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock new file mode 100644 index 0000000..96944dc --- /dev/null +++ b/ruby/Gemfile.lock @@ -0,0 +1,33 @@ +GEM + remote: https://rubygems.org/ + specs: + faraday (1.7.0) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0.1) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.1) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + multipart-post (>= 1.2, < 3) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) + multipart-post (2.1.1) + ruby2_keywords (0.0.5) + +PLATFORMS + universal-darwin-20 + +DEPENDENCIES + faraday + +BUNDLED WITH + 2.2.25 diff --git a/ruby/README.md b/ruby/README.md new file mode 100644 index 0000000..5fad710 --- /dev/null +++ b/ruby/README.md @@ -0,0 +1,46 @@ +# Voiceflow API Node.js Example + +Set up your Voiceflow project with Ruby in a matter of minutes. This example can be adapted to run as a server. + +This example enables you to chat with your project on the terminal. + +## Documentation + +For additional information about the Voiceflow API, visit the [documentation](https://www.voiceflow.com/api/dialog-manager). + +## Setup + +1. If you do not have ruby, install ruby and [bundler](https://bundler.io/) (ruby gems/dependencies manager) from [ruby-lang.org](https://www.ruby-lang.org/en/documentation/installation/), or follow an equivalent guide. +2. In this folder, run `bundle install`. +3. Replace `'YOUR_API_KEY_HERE'` and `'YOUR_VERSION_ID_HERE'` in `index.rb` with your API Key and Voiceflow project version ID. You can find them under the integrations tab: + + + +4. run `bundle exect ruby main.rb` to start your chat! + +## Example + +What it looks like in action: + +``` +➜ nodeJS git:(master) ✗ npm run start + +> voiceflow-api-nodejs-example@1.0.0 start +> node index.js + +> What is your name?: tyler +what can I do for you? +... +> Say something: send email +who is the recipient? +... +> Say something: tyler@voiceflow.com +what is the title of your email? +... +> Say something: How was your day? +sending the email for tyler@voiceflow.com called "How was your day?". Is that correct? +... +> Say something: yes +successfully sent the email for tyler@voiceflow.com called "How was your day?" +The end! Start me again with `npm run start` +``` diff --git a/ruby/main.rb b/ruby/main.rb new file mode 100644 index 0000000..55caa2d --- /dev/null +++ b/ruby/main.rb @@ -0,0 +1,54 @@ +require "faraday" +require "json" + +API_KEY = "YOUR_API_KEY_HERE" # it should look like this: VF.XXXXXXX.XXXXXX... keep this a secret! +VERSION_ID = "YOUR_VERSION_ID_HERE" # your voiceflow project versionID, find it under the "integrations" tab + +def interact(user_id, request) + puts "..." + puts user_id, request.to_json + + # call the voiceflow api with the user's name & request, get back a response + response = Faraday.post( + "https://general-runtime.voiceflow.com/state/#{VERSION_ID}/user/#{user_id}/interact", + { + headers: {"Content-Type" => "application/json", "Authorization": API_KEY }, + body: request + } + ) + + if response.nil? + return false + end + + # loop through the response + response.data.each do |trace| + # if trace.type == "speak" + # puts trace.payload.message + # elsif trace.type == 'text' + # puts trace.payload.text + # elsif trace.type == 'end' + # # an end trace means the the voiceflow dialog has ended + # return false + end + + return true +end + +def main() + puts "> What is your name?" + user_id = gets.chomp.split(',')[0] + + is_running = interact(user_id, { type: 'launch' }) + + while is_running do + puts "> Say something" + next_input = gets.chomp.split(',')[0] + # send a simple text type request with the user input + is_running = interact(user_id, { type: 'text', payload: next_input }); + end + + puts "The end! Start me again with bundle exect ruby main.rb" +end + +main() \ No newline at end of file