@hamsa-ai/voice-agents-sdk
Version:
Hamsa AI - Voice Agents JavaScript SDK
99 lines (67 loc) • 2.72 kB
Markdown
Hamsa Voice Agents Web SDK is a JavaScript library for integrating voice agents from <https://dashboard.tryhamsa.com>. This SDK provides a seamless way to incorporate voice interactions into your web applications.
Install the SDK via npm:
```bash
npm i @hamsa-ai/voice-agents-sdk
```
First, import the package in your code:
```javascript
import { HamsaVoiceAgent } from '@hamsa-ai/voice-agents-sdk'
```
Initialize the SDK with your API key.
To obtain your first API key, visit <https://dashboard.tryhamsa.com>
```javascript
const agent = new HamsaVoiceAgent(API_KEY);
```
Start a conversation with an existing agent by calling the "start" function. You can create and manage agents in our Dashboard or using our API (see: <https://docs.tryhamsa.com>):
```javascript
agent.start({
agentId: YOUR_AGENT_ID,
params: {
param1: "NAME",
param2: "NAME2"
}
});
```
When creating an agent, you can add parameters to your pre-defined values. For example, you can set your Greeting Message to: "Hello {{name}}, how can I help you today?" and pass the "name" as a parameter to use the correct name of the user.
To pause the conversation, call the "pause" function. This will prevent the SDK from sending or receiving new data until you resume the conversation:
```javascript
agent.pause();
```
To resume the conversation:
```javascript
agent.resume();
```
To end a conversation, simply call the "end" function:
```javascript
agent.end();
```
During the conversation, the SDK emits events to update your application about the conversation status.
```javascript
agent.on("callStarted", () => { console.log("Conversation has started!"); });
agent.on("callEnded", () => { console.log("Conversation has ended!"); });
agent.on("callPaused", () => { console.log("The conversation is paused"); });
agent.on("callResumed", () => { console.log("Conversation has resumed"); });
```
```javascript
agent.on("speaking", () => { console.log("The agent is speaking"); });
agent.on("listening", () => { console.log("The agent is listening"); });
```
```javascript
agent.on("transcriptionReceived", (text) => { console.log("User speech transcription received", text); });
agent.on("answerReceived", (text) => { console.log("Agent answer received", text); });
```
```javascript
agent.on("closed", () => { console.log("Conversation was closed"); });
agent.on("error", (e) => { console.log("Error was received", e); });
```