Skip to content

feat: adds ruby example structure #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem 'faraday'
33 changes: 33 additions & 0 deletions ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
@@ -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:

<img src="https://user-images.githubusercontent.com/5643574/129422436-04d964d3-85a0-402d-ae5e-d6e84723da5e.png" width=800 />

4. run `bundle exect ruby main.rb` to start your chat!

## Example

What it looks like in action:

```
➜ nodeJS git:(master) ✗ npm run start
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to change this to be Ruby specific


> [email protected] 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: [email protected]
what is the title of your email?
...
> Say something: How was your day?
sending the email for [email protected] called "How was your day?". Is that correct?
...
> Say something: yes
successfully sent the email for [email protected] called "How was your day?"
The end! Start me again with `npm run start`
```
54 changes: 54 additions & 0 deletions ruby/main.rb
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you technically don't even need this main function. I only did it in NodeJS because you can't use await in the top level code.

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()