
Getting started with the Relay SDK | by Ibraheem Khalifa | Feb, 2021
Hey folks! Letâs get started with a quick example showing how you can use the Relay node.js SDK to create a simple number game. For this example, weâll use Heroku to host a Websocket that will maintain a connection with the Relay Server.
First, create a new app using Heroku. Weâll be naming ours ârelay-wfâ:
Next, letâs setup our environment with git and Heroku CLI (instructions for windows/linux):
$ brew tap heroku/brew && brew install heroku
$ heroku login
Next, letâs initialize our environment:
$ cd relay-wf
$ git init
$ npm init
$ npm install relay-js
Next, weâll setup our example interaction & deploy it to heroku. Here we are creating a basic app that letâs users guess two numbers and returns the one who guest closest
1. Case Study: Building Appointment Booking Chatbot
2. IBM Watson Assistant provides better intent classification than other commercial products according to published study
3. Testing Conversational AI
4. How intelligent and automated conversational systems are driving B2C revenue and growth.
# workflow.jsimport relay from 'relay-js'const app = relay()app.workflow(`numbers`, workflow => {
relay.on(`start`, async () => {
const user = await relay.getDeviceName()
const random = Math.floor(Math.random() * 10) + 1
await relay.say(`Player One, pick a number between 1 and 10`)
const numberOne = await relay.listen(["$DIGIT_SEQUENCE"])
await relay.say(`Player Two, pick a number between 1 and 10`)
const numberTwo = await relay.listen(["$DIGIT_SEQUENCE"])
if (Math.abs(numberOne - random) < Math.abs(numberTwo - random)) {
await relay.say(`Player One wins! ${numberOne} was closest to ${random}!`)
} else {
await relay.say(`Player Two wins! ${numberTwo} was closest to ${random}!`)
}
await relay.terminate()
})
})
Next, weâll add our workflow configuration to Relay servers. First, fire up Dash by going to api-dash.relaygo.com (for production, this is dash.relaygo.com) and navigate to the Workflows section and select the Create button for âCustom Workflowâ :
From there, you can enter your workflow configuration. This includes the name of your workflow (here weâve named ours ânumbersâ), the devices youâd like it on (weâll push to just one device here, âIbraheemâ) and URI hosting the workflow (relay-wf.herokuapp.com). Weâre also using the spoken phrase âpick a numberâ to initiate our workflow from the device.
Save your workflow and then letâs deploy our workflow node.js app to heroku:
$ git commit -am 'Initial deploy'
$ export HEROKU_APP=relay-wf
$ git push master heroku
$ heroku logs --tail
And thatâs it! If everything worked, you should now be able to speak âpick a numberâ into the Relay assistant and trigger your number game!
Ready to start developing with Relay? Click here to signup for our Relay SDK beta.
Credit: Source link