AI対話サーバーコールバック
This document describes the events generated by the cloud APIs related to the AI service ( Conversational AI). The events are notified to your server via HTTP/HTTPS requests. You can provide the relevant configuration information to Tencent Cloud to enable this service. You can also use it together with Tencent RTC's Event Callbacks to implement more custom logic.
Configuration Information
The Tencent RTC console supports configuring the callback information. Once it is configured, 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 the signature, which can contain up to 32 characters consisting of uppercase and lowercase letters and digits.
Timeout and Retry
If the event callback server does not receive a response from your server within 5 seconds after sending a message notification, the notification is considered to have failed. Retry is performed when a notification fails for the first time. If it still fails after a retry, it will be retried every 10 seconds. If it still fails within 1 minute after it is sent, no further retries will be performed.
Event Callback Message Format
Event callback messages are sent to your server via HTTP/HTTPS POST requests, in which:
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 the response package. To be compatible with the protocol, it is recommended that the response content contains the JSON object {"code":0}.
Package example: The following code is a package example for the event of "AI conversation task started successfully".
{"EventGroupId": 9,"EventType": 901,"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 | Value |
Content-Type | Value: application/json. |
Sign | Signature value |
SdkAppId | SDK application ID |
The body of an event callback message includes the following fields:
Field | Type | Description |
EventGroupId | Number | Event group ID. The value is fixed at 4 for mix-stream relay events. |
EventType | Number | Type of the event corresponding to the callback notification. |
CallbackMsTs | Number | Unix timestamp in milliseconds when the event callback server sends a callback request to your server. |
EventInfo | JSON Object |
Event Group ID
Field | Value | Description |
EVENT_GROUP_AI_SERVICE | 9 | AI service event group. |
Event Type
Field | Value | Description |
EVENT_TYPE_AI_SERVICE_START | 901 | Callback of an AI task when it is started. |
EVENT_TYPE_AI_SERVICE_STOP | 902 | Callback of an AI task when it is stopped. |
EVENT_TYPE_AI_SERVICE_MSG | 903 | Callback of the complete sentence recognized by ASR (STT) or the complete content returned by LLM. |
EVENT_TYPE_AI_START_OF_SPEECH | 904 | Callback for the start of a sentence recognized by ASR (STT). |
EVENT_TYPE_AI_SPEAKING_FINISHED | 905 | Callback for the end of AI speaking in a conversation round. |
EVENT_TYPE_AI_METRIC_MESSAGE | 906 | Callback of a LLM/TTS metric call by the AI service. |
EVENT_TYPE_AI_ERROR_METRIC_CALLBACK | 908 | Callback of a metric call error of the AI service. |
EVENT_TYPE_AI_SESSION_STATUS_CALLBACK | 909 | Callback of AI session readiness, indicating that the audio and video channels are established and ready for conversation. |
Event Information Definition for Event Type 901:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload.Status | Number | 0: AI task started successfully. 1: Failed to start an AI task. |
{"EventGroupId": 9,"EventType": 901,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"Status": 0}}}
Event Information Definition for Event Type 902:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload.LeaveCode | Integer | 0: The task exits after the stop API is called normally. 1: The task exits after the user removes the transcription bot. 2: The task exits after the user dissolves the room. 3: The RTC Engine server removes the transcription bot. 4: The RTC Engine server dissolves the room. 98: Internal error. It is recommended that the user perform a 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}}}
Event Information Definition for Event Type 903:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload | JSON Object | JSON object. Its format is consistent with the callback format of custom messages in the client. { "UserId":"", "Text":"", "StartTimeMs":1234, "EndTimeMs":1269, "RoundId":"xxxxxx" // Unique ID of a conversation round. } |
{"EventGroupId": 9,"EventType": 903,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"UserId":"","Text":"","StartTimeMs":1234,"EndTimeMs":1269,"RoundId":"xxxxxx"}}}
Event Information Definition for Event Type 904:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload | JSON Object | for JSON object { "UserId": "xxx", "RoundId": "xxxxx" // Unique ID of a conversation round. } |
{"EventGroupId": 9,"EventType": 904,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"UserId": "xxx","RoundId": "xxxxx"}}}
Event Information Definition for Event Type 905:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload | JSON Object | JSON object. { "UserId": "UserId", // User ID of the AI chatbot. "RoundId": "RoundId", // Round ID of the current conversation. "Text": "Text" // Text of the content spoken by the AI chatbot in the current conversation round. } |
{"EventGroupId": 9,"EventType": 905,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"UserId": "UserId","RoundId": "RoundId","Text": "Text"}}}
Event Information Definition for Event Type 906:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload | JSON Object | JSON object. { "Metric": "llm_network_latency", // Metric name. "Value": 218, // Metric. "Tag": { "RoundId": "070c4908-105", // Conversation round ID. } } The names of called metrics are as follows: asr_latency llm_network_latency llm_first_token tts_network_latency tts_first_frame_latency tts_discontinuity interruption |
{"EventGroupId": 9,"EventType": 906,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"Metric": "llm_first_token","Value": 218,"Tag": {"RoundId": "070c4908-1057-4ced-a949-356bf11848bc"}}}}
Event Information Definition for Event Type 908:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload | JSON Object | JSON object. { "Metric": "llm_error", // Metric name. "Tag": { "RoundId": "070c4908-1057-4ced-a949-356bf11848bc", "Code": 0, // Service error code. "Message": "" // Detailed description of the error message. } } The names of metrics with an error are as follows: asr_latency llm_network_latency llm_first_token tts_network_latency tts_first_frame_latency tts_discontinuity interruption |
{"EventGroupId": 9,"EventType": 908,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"Metric": "llm_error","Tag": {"RoundId": "070c4908-1057-4ced-a949-356bf11848bc","Code": 0,"Message": ""}}}}
Event Information Definition for Event Type 909:
Field | Type | Description |
EventMsTs | String | Unix timestamp when an event occurred, in milliseconds. |
TaskId | String | AI task ID. |
RoomId | String | RTC Engine room ID. |
RoomIdType | Integer | 0: Indicates that the room ID is a number. 1: Indicates that the room ID is a string. |
Payload | JSON Object | JSON object. { "Status": "session_ready" // Indicates that the audio and video channels are established and ready for conversation. } |
{"EventGroupId": 9,"EventType": 909,"CallbackTs": 1687770730166,"EventInfo": {"EventMsTs": 1622186275757,"TaskId": "xx","RoomId": "1234","RoomIdType": 0,"Payload": {"Status": "session_ready"}}}
Signature Calculation
The signature is calculated by using the HMAC SHA256 encryption algorithm. After your event callback server receives a callback message, it calculates the signature in the same way. If the two signatures are the same, the message is an event callback message of Tencent RTC and is not forged. The signature calculation is as illustrated below:
//key in the signature (Sign) calculation formula indicates the encryption key used for calculating the signature (Sign).Sign = base64(hmacsha256(key, body))
Note:
body refers to the original package body of the received callback request. Do not transform it. Example:
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
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 the Tencent Cloud callback//# sign: Signature returned by the Tencent Cloud callback//# Returned values://# Status: OK indicates a successful verification, and FAIL indicates a failed verification. See Info for details.//# Info: Information about a successful/failed verificationpublic class checkSign {public static String getResultSign(String key, String body) throws Exception {Mac hmacSha256 = Mac.getInstance("HmacSHA256");SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");hmacSha256.init(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 = getResultSign(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 hmacimport base64from hashlib import sha256# Feature: Third-Party callback signature verification# The parameters are as follows:# key: Key configured in the console.# body: Body returned by the Tencent Cloud callback.# sign: Signature returned by the Tencent Cloud callback.# Returned values:# Status: OK indicates a successful verification, and FAIL indicates a failed verification. See Info for details.# Info: Information about a successful/failed verification.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 == sign:temp_dict['Status'] = 'OK'temp_dict['Info'] = 'Verification successful.'return temp_dictelse:temp_dict['Status'] = 'FAIL'temp_dict['Info'] = 'Verification failed.'return temp_dictif __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 = checkSign(key, body, sign)print(result)
<?phpclass 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 mainimport "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. It supports up to 30 languages and fully simulated input/output for online interaction.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))}