Add custom event
Custom events can be used to add metadata to your conversation. There are some restrictions when using custom events:
- Event type must begin with
custom: - Event type must not exceed 100 characters
- Event type must contain ONLY alphanumeric,
-and_characters - Event body must not exceed 4096 bytes
Example
Ensure the following variables are set to your required values using any convenient method:
| Key | Description |
|---|---|
CONVERSATION_ID | The ID of the Conversation. |
MEMBER_ID | The unique ID of the Member. |
Prerequisites
You will need to use an existing Application that contains a Conversation in order to be able to add a custom event. See the Create Conversation code snippet for information on how to create an Application and some sample Conversations.
Write the code
Add the following to create-custom-event.sh:
# `from` is only required if you're using a JWT with a `sub` claim
curl -X "POST" "https://api.nexmo.com/v1/conversations/$CONV_ID/events" \
-H 'Authorization: Bearer '$JWT\
-H 'Content-Type: application/json' \
-d $'{
"type": "custom:YOUR_EVENT_NAME",
"from": "'$CONV_EVENT_FROM'",
"body": {
"your": "data"
}
}'Run your code
Save this file to your machine and run it:
Prerequisites
You will need to use an existing Application that contains a Conversation in order to be able to add a custom event. See the Create Conversation code snippet for information on how to create an Application and some sample Conversations.
npm install @vonage/server-sdkCreate a file named create-custom-event.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Write the code
Add the following to create-custom-event.js:
vonage.conversations.createEvent(
CONV_ID,
{
'type': 'custom:YOUR_EVENT_NAME',
'from': CONV_EVENT_FROM,
'body': {
'your': 'data',
},
},
)
.then((event) => console.log(event))
.catch((error) => console.error(error));Run your code
Save this file to your machine and run it:
Prerequisites
You will need to use an existing Application that contains a Conversation in order to be able to add a custom event. See the Create Conversation code snippet for information on how to create an Application and some sample Conversations.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named CreateCustomEvent and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the CreateCustomEvent class:
var event = client.getConversationsClient().createEvent(
CONV_ID, CustomEvent.builder("my_custom_event_name")
.from(CONV_MEMBER_ID)
.body(Map.of("your", "data"))
.build()
);
System.out.println(event);Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.conversation with the package containing CreateCustomEvent:
Prerequisites
You will need to use an existing Application that contains a Conversation in order to be able to add a custom event. See the Create Conversation code snippet for information on how to create an Application and some sample Conversations.
Install-Package VonageCreate a file named CreateCustomEvent.cs and add the following code:
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Conversations.CreateEvent;
using Vonage.Request;Add the following to CreateCustomEvent.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);Write the code
Add the following to CreateCustomEvent.cs:
var response = await client.ConversationsClient.CreateEventAsync(CreateEventRequest.Build()
.WithConversationId(CONV_ID)
.WithType($"custom:{YOUR_EVENT_NAME}")
.WithBody(new
{
your = "data",
})
.WithFrom(CONV_EVENT_FROM)
.Create());Prerequisites
You will need to use an existing Application that contains a Conversation in order to be able to add a custom event. See the Create Conversation code snippet for information on how to create an Application and some sample Conversations.
composer require vonage/clientCreate a file named create-custom-event.php and add the following code:
use Vonage\Conversation\ConversationObjects\CreateConversationRequest;
use Vonage\Conversation\ConversationObjects\UpdateConversationRequest;
require_once __DIR__ . '../../config.php';
require_once __DIR__ . '../../vendor/autoload.php';
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);Write the code
Add the following to create-custom-event.php:
$eventRequest = new \Vonage\Conversation\ConversationObjects\EventRequest(
CONVERSATION_ID,
'custom:' . YOUR_EVENT_NAME,
MEMBER_ID,
[
'your' => 'data'
]
);
$client->conversation()->createEvent($eventRequest);Run your code
Save this file to your machine and run it:
Try it out
When you run the code you'll see a custom event in your event list