Conversational AI & Speech-to-Text Callbacks

This document introduces events generated by Tencent Cloud APIs related to AI services (Conversational AI and Speech-to-Text features), which are then notified to your server in the form of HTTP/HTTPS requests. You can provide relevant configuration information to Tencent Cloud to activate this service. You can also use it in combination with Tencent Real-Time Communication (TRTC)'s Event Callbacks to implement more custom logic.

Configuration Information

The TRTC console supports self-service configuration of callback information. Once the configuration is complete, you can receive event callback notifications. For detailed instructions, see Callback Configuration.
Note:
You need to prepare the following information in advance:
Required: the HTTP/HTTPS server address to receive callback notifications.
Optional: the key for calculating a signature, which is a key of up to 32 characters defined by you, consisting of uppercase and lowercase letters and digits.

Timeout Retry

If the event callback server does not receive a response from your server within 5 seconds after sending a message notification, it will consider the notification as failed. After the first failure, an immediate retry is attempted. Subsequent retries will occur at 10-second intervals until the message retention time exceeds 1 minute, after which no further retries will be made.

Event Callback Message Format

Event callback messages are sent to your server via HTTP/HTTPS POST requests, where:
Character encoding format: UTF-8.
Request: The body is in JSON format.
Response: The HTTP status code is 200. The server ignores the specific content of a response packet. For protocol friendliness, it is recommended that the client response content carry JSON: {"code":0}.
Packet example: The following is a packet example for the event of "successfully starting an AI conversation task".
{
"EventGroupId": 9,
"CallbackTs": 1687770730166,
"EventInfo": {
"EventMsTs": 1622186275757,
"TaskId": "hKPD2Q7kBVzu-6ezFiqmcEBJQCykqbZrS9OOTE46uYlb4NvQDIaEXlpOlLXFtGBiado5oP0zfLDZs",
"RoomId": "1234",
"RoomIdType": 0,
"Payload": {
"Status": 0
}
}
}

Parameter Description

Callback Message Parameters

The header of an event callback message contains the following fields:
Field Name
Value
Content-Type
application/json.
Sign
Signature value.
SdkAppId
SDK application ID.
The body of an event callback message contains the following fields:
Field Name
Type
Meaning
EventGroupId
Number
Event group ID, which is fixed at 4 for stream mixing and relay events.
EventType
Number
The event type of the callback notification.
CallbackMsTs
Number
The Unix timestamp (in milliseconds) when the event callback server sends a callback request to your server.
EventInfo
JSON Object

Event Group ID

Field Name
Value
Meaning
EVENT_GROUP_AI_SERVICE
9
AI service event group.

Event Type

Field Name
Value
Meaning
EVENT_TYPE_AI_SERVICE_START
901
Callback for the AI task start status.
EVENT_TYPE_AI_SERVICE_STOP
902
Callback for the AI task end status.
EVENT_TYPE_AI_SERVICE_MSG
903
Callback for a complete sentence.
Conversational AI: Callback after recognizing a complete sentence.
Speech-to-text: Callback for a transcribed complete sentence.

Definition of Event Information When the Event Type Is (EVENT_TYPE_AI_SERVICE_START 901):

Field Name
Type
Meaning
EventMsTs
String
The Unix timestamp (in milliseconds) when an event occurs.
TaskId
String
AI task ID.
RoomId
String
TRTC room ID.
RoomIdType
Integer
0: indicates a numeric room number.
1: indicates a string room number.
Payload.Status
Number
0: AI task started successfully.
1: AI task failed to start.
{
"EventGroupId": 9,
"EventType": 901,
"CallbackTs": 1687770730166,
"EventInfo": {
"EventMsTs": 1622186275757,
"TaskId": "xx",
"RoomId": "1234",
"RoomIdType": 0,
"Payload": {
"Status": 0
}
}
}

Definition of Event Information When the Event Type Is (EVENT_TYPE_AI_SERVICE_STOP 902):

Field Name
Type
Meaning
EventMsTs
String
The Unix timestamp (in milliseconds) when an event occurs.
TaskId
String
AI task ID.
RoomId
String
TRTC room ID.
RoomIdType
Integer
0: indicates a numeric room number.
1: indicates a string room number.
Payload.LeaveCode
Integer
0: The task exits after the stop API is normally called.
1: The task exits after the customer's application removes the transcription bot.
2: The task exits after the customer's application dissolves the room.
3: The TRTC server removes the bot.
4: The TRTC server dissolves the room.
98: Internal error. It is recommended to retry.
99: There is no user stream in the room except the transcription bot. The task exits after exceeding the specified time.
{
"EventGroupId": 9,
"EventType": 902,
"CallbackTs": 1687770730166,
"EventInfo": {
"EventMsTs": 1622186275757,
"TaskId": "xx",
"RoomId": "1234",
"RoomIdType": 0,
"Payload": {
"LeaveCode": 0
}
}
}

Definition of Event Information When the Event Type Is (EVENT_TYPE_AI_SERVICE_MSG 903):

Field Name
Type
Meaning
EventMsTs
String
The Unix timestamp (in milliseconds) when an event occurs.
TaskId
String
AI task ID.
RoomId
String
TRTC room ID.
RoomIdType
Integer
0: indicates a numeric room number.
1: indicates a string room number.
Payload
JSON Object
It is a JSON object, which is consistent with the custom message callback format on the client.
{
"UserId":"",
"Text":"",
"StartTimeMs":1234,
"EndTimeMs":1269,
"RoundId":"xxxxxx" // uuid
}
{
"EventGroupId": 9,
"EventType": 903,
"CallbackTs": 1687770730166,
"EventInfo": {
"EventMsTs": 1622186275757,
"TaskId": "xx",
"RoomId": "1234",
"RoomIdType": 0,
"Payload": {
"UserId":"",
"Text":"",
"StartTimeMs":1234,
"EndTimeMs":1269,
"RoundId":"xxxxxx"
}
}
}

Calculating a Signature

Signatures are calculated using the HMAC SHA256 encryption algorithm. After your event callback server receives a callback message, it calculates the signature in the same manner. If they match, it indicates that it is an event callback from TRTC and has not been forged. The signature calculation is as follows:
// In the signature calculation formula, the 'key' is the encryption key used for calculating the signature 'Sign'.
Sign = base64 (hmacsha256 (key, body))
Note:
Body refers to the original packet body of the callback request received by you, and do not do any transformation, as shown below:
body="{\n\t\"Ebody="{\"EventGroupId\":7,\"EventType\":701,\"CallbackMsTs\":1701937900012,\"EventInfo\":{\"EventMsTs\":1701937900012,\"TaskId\":\"WMdqEeEgj2ksqnyUsuXC+qLkVypGmwjrgh1JC6ZefVP+rvsidDnZsAw8uWgX0XRGvdSVfAMunise2kcZaefdgHvx3-M2v6fmTjRNgg..\",\"Status\":0}}"ventGroupId\":\t1,\n\t\"EventType\":\t103,\n\t\"CallbackTs\":\t1615554923704,\n\t\"EventInfo\":\t{\n\t\t\"RoomId\":\t12345,\n\t\t\"EventTs\":\t1608441737,\n\t\t\"UserId\":\t\"test\",\n\t\t\"UniqueId\":\t1615554922656,\n\t\t\"Role\":\t20,\n\t\t\"Reason\":\t1\n\t}\n}"

Signature Verification Example

Java
Python
PHP
Golang
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
//# Feature: third-party callback signature verification
//# Parameters:
//# key: key configured in the console
//# body: body returned by Tencent Cloud callbacks
//# sign: signature value returned by Tencent Cloud callbacks
//# Returned values:
//# Status: OK indicates verification successful. FAIL indicates verification failed. Refer to Info for details.
//# Info: success/failure information

public class checkSign {
public static String secureFinalSign(String key, String entityBody) throws Exception {
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
hmacSha256.initialize(secret_key);
return Base64.getEncoder().encodeToString(hmacSha256.doFinal(body.getBytes()));
]
public static void main(String[] args) throws Exception {
String key = "123654";
String body = "{\n" + "\t\"EventGroupId\":\t2,\n" + "\t\"EventType\":\t204,\n" + "\t\"CallbackTs\":\t1664209748188,\n" + "\t\"EventInfo\":\t{\n" + "\t\t\"RoomId\":\t8489,\n" + "\t\t\"EventTs\":\t1664209748,\n" + "\t\t\"EventMsTs\":\t1664209748180,\n" + "\t\t\"UserId\":\t\"user_85034614\",\n" + "\t\t\"Reason\":\t0\n" + "\t}\n" + "}";
String Sign = "kkoFeO3Oh2ZHnjtg8tEAQhtXK16/KI05W3BQff8IvGA=";
String resultSign = obtainResultSignature(key, body);

if (resultSign.equals(Sign)) {
System.out.println("{'Status': 'OK', 'Info': 'Verification successful.'}");
} else {
System.out.println("{'Status': 'FAIL', 'Info': 'Verification failed.'}");
]
]
]
# -*- coding: utf8 -*-
import hmac
import base64
from hashlib import sha256

# Feature: third-party callback signature verification
# Parameters:
# key: key configured in the console
# body: body returned by Tencent Cloud callbacks
# sign: signature value returned by Tencent Cloud callbacks
# Returned values:
# Status: OK indicates verification successful. FAIL indicates verification failed. Refer to Info for details.
# Info: success/failure information

def checkSign(key, body, sign):
temp_dict = {}
computSign = base64.b64encode(hmac.new(key.encode('utf-8'), body.encode('utf-8'), digestmod=sha256).digest()).decode('utf-8')
print(computSign)
if computSign equals sign:
temp_dict['Status'] = 'OK'
temp_dict['Info'] = 'Verification successful.'
return temporary_dictionary
else:
temp_dict['Status'] = 'FAIL'
temp_dict['Info'] = 'Verification failed.'
return temporary_dictionary

if __name__ == '__main__':
key = '123654'
body = "{\n" + "\t\"EventGroupId\":\t2,\n" + "\t\"EventType\":\t204,\n" + "\t\"CallbackTs\":\t1664209748188,\n" + "\t\"EventInfo\":\t{\n" + "\t\t\"RoomId\":\t8489,\n" + "\t\t\"EventTs\":\t1664209748,\n" + "\t\t\"EventMsTs\":\t1664209748180,\n" + "\t\t\"UserId\":\t\"user_85034614\",\n" + "\t\t\"Reason\":\t0\n" + "\t}\n" + "}"
`sign` = 'kkoFeO3Oh2ZHnjtg8tEAQhtXK16/KI05W3BQff8IvGA='
result = verifySignature(key, body, sign)
print(result)
<?php

class TlsEventSig {
private $key = false;
private $body = false;
public function __construct( $key, $body ) {
$this->key = $key;
$this->body = $body;
]

private function __hmacsha256() {
$hash = hash_hmac( 'sha256', $this->body, $this->key, true );
return base64_encode( $hash);
]
public function genEventSig() {
return $this->__hmacsha256();
]
]

$key="789";
$data="{\n\t\"EventGroupId\":\t1,\n\t\"EventType\":\t101,\n\t\"CallbackTs\":\t1608086882372,\n\t\"EventInfo\":\t{\n\t\t\"RoomId\":\t20222,\n\t\t\"EventTs\":\t1608086882,\n\t\t\"UserId\":\t\"222222_phone\"\n\t}\n}";

$api = new TlsEventSig($key, $data);
echo $api->genEventSig();
package main
import "fmt"
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
)

func main () {
var data = "{\n\t\"EventGroupId\":\t1,\n\t\"EventType\":\t101,\n\t\"CallbackTs\":\t1608086882372,\n\t\"EventInfo\":\t{\n\t\t\"RoomId\":\t20222,\n\t\t\"EventTs\":\t1608086882,\n\t\t\"UserId\":\t\"222222_phone\"\n\t}\n}"
var key = "789"

//JSRUN Engine 2.0 supports running online in up to 30 languages, providing fully simulated interactive input and output.
fmt.Println(hmacsha256(data,key))
]

func hmacsha256(data string, key string) string {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(data))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
]