Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mishushakov/dialogflow-gateway-js

🔌 Dialogflow V2 JS SDK for node and browser
https://github.com/mishushakov/dialogflow-gateway-js

client dialogflow dialogflow-v2 nlu

Last synced: 4 days ago
JSON representation

🔌 Dialogflow V2 JS SDK for node and browser

Awesome Lists containing this project

README

        

# Dialogflow Gateway JavaScript SDK

Dialogflow Gateway enables third-party integrations to securely access the Dialogflow V2 API

- [Documentation](https://github.com/mishushakov/dialogflow-gateway-docs)
- [Implementations](https://github.com/mishushakov/dialogflow-gateway-docs#implementations)

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:

```html

```

## Usage

Import the library and connect to your Dialogflow Gateway Endpoint:

```js
import { Client } from 'dialogflow-gateway'

new Client('')
```

Note: Endpoint is a URL (example: https://dialogflow-web-v2.core.ushaflow.io)

## Events

- `error`, returns error
- `message`, returns the message body

## Examples

With Async/Await and ES Modules on [Dialogflow Gateway Hosted by Ushakov](https://dialogflow.cloud.ushakov.co)

```js
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

```js
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

```js
/* 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](./examples)