Quick Start
The following example shows a basic example of creating an assistant to join a room and send a chat message
Getting Started
Create your assistant in the Whereby dashboard
Visit Configure > Assistant and create an assistant
Grab your assistant key

Import polyfills and initialize assistant
import "@whereby.com/assistant-sdk/polyfills";
import { Assistant } from "@whereby.com/assistant-sdk";
const assistant = new Assistant({ assistantKey: "my-assistant-key" });
Have the assistant start to join the room and wait until the room is connected. We should also wrap this call in a try/catch block in case any errors happen during the join room flow:
try {
await assistant.joinRoom("https://your-subdomain.whereby.com/your-room-name");
} catch(error) {
console.error("An error occurred joining the room", error);
};
Once the room is connected start using your assistant!
const roomConnection = assistant.getRoomConnection();
roomConnection.sendChatMessage("Hello! I'm your assistant, how can I help you?");
The above steps can collectively be written in promise-formatted code as follows:
const assistant = new Assistant({ assistantKey: "my-assistant-key" });
assistant
.joinRoom("https://your-subdomain.whereby.com/your-room-name")
.then(() => {
const roomConnection = assistant.getRoomConnection();
roomConnection.sendChatMessage("Hello! I'm your assistant, how can I help you?");
})
.catch((error) => {
console.error("An error occurred joining the room", error);
});
Using Trigger API
If you'd like the assistant to join the room automatically, you can make some small tweaks to our first example to use our webhook trigger API. This way, you can listen for certain webhook events and create your assistant whenever those webhooks are received! You should still follow step 1 in the above tutorial to obtain the assistant key.
Create your assistant in the Whereby dashboard
Visit Configure > Assistant and create an assistant
Grab your assistant key
❗Make sure to set the endpoint URL in the configuration steps to receive webhooks
Set up start trigger
import { Trigger } from "@whereby.com/assistant-sdk";
let hasAssistantJoined = false;
const trigger = new Trigger({
webhookTriggers: {
"room.client.joined": () => !hasAssistantJoined,
},
port: 3000,
});
Start the trigger server
trigger.start();
Listen for the
TRIGGER_EVENT_SUCCESS
event - this indicates that the trigger predicate has been a match, and that your assistant should now join the room. Here, you can create the assistant and join the room before sending a chat message in to the connected room.
trigger.on(TRIGGER_EVENT_SUCCESS, async ({ roomUrl }) => {
console.log("Webhook trigger has been matched!");
const assistant = new Assistant({ assistantKey: "my-assistant-key" });
try {
await assistant.joinRoom("https://your-subdomain.whereby.com/your-room-name");
console.log("Connected to room");
hasAssistantJoined = true;
} catch(error) {
console.error("An error occurred joining the room", error);
return;
};
const roomConnection = assistant.getRoomConnection();
roomConnection.sendChatMessage("Hello! I'm your assistant, how can I help you?");
});
Finally, clean up any open listeners when the assistant eventually leaves the room (e.g. reset the webhook trigger for this room).
assistant.on(ASSISTANT_LEFT_ROOM, () => {
console.log("Disconnected from room");
hasAssistantJoined = false;
});
Start building! Here’s a minimal example, using the trigger API and creating an assistant that will send a chat message into the room session.
import "@whereby.com/assistant-sdk/polyfills";
import {
Trigger,
Assistant,
TRIGGER_EVENT_SUCCESS,
ASSISTANT_LEFT_ROOM,
} from "@whereby.com/assistant-sdk";
let hasAssistantJoined = false;
const trigger = new Trigger({
webhookTriggers: {
"room.client.joined": () => !hasAssistantJoined,
},
port: 3000,
});
trigger.start();
trigger.on(TRIGGER_EVENT_SUCCESS, async ({ roomUrl }) => {
console.log("Webhook trigger has been matched!");
const assistant = new Assistant({ assistantKey: "my-assistant-key" });
try {
await assistant.joinRoom("https://your-subdomain.whereby.com/your-room-name");
console.log("Connected to room");
hasAssistantJoined = true;
} catch(error) {
console.error("An error occurred joining the room", error);
return;
}
const roomConnection = assistant.getRoomConnection();
roomConnection.sendChatMessage("Hello! I'm your assistant, how can I help you?");
assistant.on(ASSISTANT_LEFT_ROOM, () => {
console.log("Disconnected from room");
hasAssistantJoined = false;
});
});
This demonstrates the most basic functionality of the library. From this example, you can go on to implement more advanced functionality that is explained in more detail in the next sections of the documentation: API Reference.
Additionally, you can take a look at the source code for an example Whereby Assistant that is published on Github here: https://github.com/whereby/whereby-assistant-audio-recorder.
Last updated
Was this helpful?