dialogflow-gateway
v1.0.1
Published
Dialogflow V2 JS SDK for node and browser. Written in TypeScript
Downloads
25
Readme
Dialogflow Gateway JavaScript SDK
Dialogflow Gateway enables third-party integrations to securely access the Dialogflow V2 API
This is a JavaScript Client, that is compatitable with Dialogflow Gateway backends.
It can be used both in browser and node as a drop-in replacement for the deprecated dialogflow-javascript-client
library, by Dialogflow
Attention: v1.0 is no longer using promises to retrieve messages and relies on events instead
Installation
npm:
npm install dialogflow-gateway
yarn:
yarn add dialogflow-gateway
Browser:
<script src="https://unpkg.com/dialogflow-gateway@latest/dist/bundle.js"></script>
Usage
Import the library and connect to your Dialogflow Gateway Endpoint:
import { Client } from 'dialogflow-gateway'
new Client('<YOUR ENDPOINT HERE>')
Note: Endpoint is a URL (example: https://dialogflow-web-v2.core.ushaflow.io)
Events
error
, returns errormessage
, returns the message body
Examples
With Async/Await and ES Modules on Dialogflow Gateway Hosted by Ushakov
import { Client } from 'dialogflow-gateway'
async () => {
/* Connect Dialogflow Gateway Client */
const client = new Client('https://dialogflow-web-v2.core.ushaflow.io')
/* Send text request */
await client.send({
session: 'test',
queryInput: {
text: {
text: 'Hello',
languageCode: 'en'
}
}
})
client.on('message', console.log)
client.error('message', console.error)
/* Retrieve the Agent */
try {
const agent = await client.get())
console.log(agent)
}
catch (error){
// Handle error
}
}
Same code with require and promises
const { Client } = require('dialogflow-gateway')
/* Connect Dialogflow Gateway Client */
const client = new Client('https://dialogflow-web-v2.core.ushaflow.io')
/* Send text request */
client.send({
session: 'test',
queryInput: {
text: {
text: 'Hello',
languageCode: 'en'
}
}
})
client.on('message', console.log)
client.error('message', console.error)
/* Retrieve the Agent */
client.get()
.then(agent => {
console.log(agent)
})
.catch(error => {
// Handle Error
})
Same code in Browser. Notice, that we are using the df
scope
/* Connect Dialogflow Gateway Client */
const client = new df.Client('https://dialogflow-web-v2.core.ushaflow.io')
/* Send text request */
client.send({
session: 'test',
queryInput: {
text: {
text: 'Hello',
languageCode: 'en'
}
}
})
client.on('message', console.log)
client.error('message', console.error)
/* Retrieve the Agent */
client.get()
.then(agent => {
console.log(agent)
})
.catch(error => {
// Handle Error
})
For more examples see examples directory