Push speaker events - Telephony API
Follow this example to establish a connection using a phone number through PSTN. Then use the connection to send speaker events, generate insights, and display a summary URL with the output. You can see how to initialize the Voice SDK, connect to the endpoint, push speaker events, and get the summary URL.
This example uses variables which you need to replace with your values:
Variable | Description |
---|---|
<APP_ID> | The application ID you get from the Symbl.ai Platform. |
<APP_SECRET> | The application secret you get from the Symbl.ai Platform. |
<AUTH_TOKEN> | The JSON Web Token (JWT) generated when you Authenticate. |
<DEFAULT_PHONE_NUMBER> | A phone number that you want the API to connect to. Be sure to include the country code. |
<EMAIL_ADDRESS> | The email address where you want to send the summary email. |
<DTMF_MEETING_CODE> | Dual Tone Multi Frequency (DTMF) meeting code provided by Zoom or other meeting platform. |
Symbl.ai Telephony Speaker Events | Github
Getting started
Download and install Node.js and NPM as described in Downloading and installing Node.js and npm.
This example runs on a Node server, so it uses the @symblai/symbl-js
package.
Initialize the SDK
await sdk.init({
appId: <APP_ID>,
appSecret: <APP_SECRET>,
basePath: 'https://api.symbl.ai'
});
Connect to an Endpoint
const connection = await sdk.startEndpoint(endpointConfig);
Set Up Configuration Options
Provide phone number and endpoint type:
endpoint: {
type: 'pstn',
phoneNumber: <DEFAULT_PHONE_NUMBER>
}
If you want to use a SIP connection, you can use type: sip
and provide the
SIP URI dial in. This should be unique for an active call/meeting in your
system.
You can also provide a DTMF code if you have one. You can find this code
on the meeting platform invite. You can leave it blank if not connecting to the meeting
platform.
{
dtmf: "<DTMF_MEETING_CODE>",
type: 'sip',
uri: 'sip:[email protected]'
}
You can also pass a custom audioConfig
configuration object. If not provided, it uses PCMU with an 800
sample rate. If you want to provide it, use the code:
audioConfig: {
encoding: 'OPUS',
sampleRate: 48000
}
Getting The Connection ID
To send speaker events we need a connectionId
unique to each active
connection. You can retrieve it from the connection response:
const connectionId = connection.connectionId;
Sending The Speaker Event
We can send different speaker events to our connection indicating that different
speakers started speaking. That will give us more personalized insights and get a
better meeting summary
In our example, we will do it by calling helper function getScheduleEvent
, which
we will review in a bit. We pass SpeakerEvent type to it by using
SpeakerEvent.types
enum from @symblai/symbl-js
, passing user data and timestamp
const scheduleEvent = getScheduleEvent(sdk, connectionId);
setTimeout(() => {
// Schedule all the events to be sent.
scheduleEvent(SpeakerEvent.types.startedSpeaking, users.john, 0);
scheduleEvent(SpeakerEvent.types.stoppedSpeaking, users.john, 5);
}, 1000);
We retrieve users just from the global array of users but in real-world example
that might be user's data retrieved from the database.
const users = {
john: {
userId: '[email protected]',
name: 'John'
},
mary: {
userId: '[email protected]',
name: 'Mary'
}
};
To push an event to our connection, we create an event as follows:
const speakerEvent = new SpeakerEvent({
type: eventType,
user
});
speakerEvent.timestamp = new Date().toISOString();
And then push it using the pushEventOnConnection
function provided by the SDK:
sdk.pushEventOnConnection(connectionId, speakerEvent.toJSON(), (err) => {
if (err) {
console.error('Error during push event.', err);
} else {
console.log('Event pushed!');
}
});
Full Code Example
const {sdk, SpeakerEvent} = require('@symblai/symbl-js')
const APP_ID = "<APP_ID>";
const APP_SECRET = "<APP_SECRET>";
const DEFAULT_PHONE_NUMBER = "<DEFAULT_PHONE_NUMBER>";
const EMAIL_ADDRESS = "<EMAIL_ADDRESS>";
sdk
.init({
appId: APP_ID,
appSecret: APP_SECRET,
basePath: 'https://api.symbl.ai',
})
.then(() => {
console.log('SDK Initialized')
sdk
.startEndpoint({
endpoint: {
// type: 'sip', // Use this if you're trying to dial in to a SIP trunk.
// uri: 'sip:[email protected]',
type: 'pstn',
phoneNumber: <DEFAULT_PHONE_NUMBER>,
//dtmf: '', // you can find this on the meeting platform invite. Omit or leave blank if not connecting to a meeting platform
},
actions: [
{
invokeOn: 'stop',
name: 'sendSummaryEmail',
parameters: {
emails: [<EMAIL_ADDRESS>], // Add valid email addresses to received email
},
},
],
})
.then((connection) => {
const connectionId = connection.connectionId
console.log('Successfully connected.', connectionId)
const speakerEvent = new SpeakerEvent({
type: SpeakerEvent.types.startedSpeaking,
user: {
userId: '[email protected]',
name: 'John',
},
})
setTimeout(() => {
speakerEvent.timestamp = new Date().toISOString()
sdk.pushEventOnConnection(
connectionId,
speakerEvent.toJSON(),
(err) => {
if (err) {
console.error('Error during push event.', err)
} else {
console.log('Event pushed!')
}
},
)
}, 1000)
setTimeout(() => {
speakerEvent.type = SpeakerEvent.types.stoppedSpeaking
speakerEvent.timestamp = new Date().toISOString()
sdk.pushEventOnConnection(
connectionId,
speakerEvent.toJSON(),
(err) => {
if (err) {
console.error('Error during push event.', err)
} else {
console.log('Event pushed!')
}
},
)
}, 12000)
// Scheduling stop endpoint call after 60 seconds
setTimeout(() => {
sdk
.stopEndpoint({
connectionId: connection.connectionId,
})
.then(() => {
console.log('Stopped the connection')
console.log('Summary Info:', connection.summaryInfo)
console.log('Conversation ID:', connection.conversationId)
})
.catch((err) =>
console.error('Error while stopping the connection.', err),
)
}, 10000)
})
.catch((err) => console.error('Error while starting the connection', err))
})
.catch((err) => console.error('Error in SDK initialization.', err))
Running The Example
Create a JavaScript file named app.js
and copy the preceding code into the file. Fill in the variables with values.
Use NPM to install the required libraries:
npm install @symblai/symbl-js
From a terminal run:
$ node app.js
See the console for a success message.
If you have any questions or concerns about our API, you can join our Support Slack or send us an email at [email protected]
Updated about 2 years ago