Calling Method
Only POST requests over the HTTPS protocol are supported. Calling via IP address and port is not supported.
Supported interface format:
https://example.com
Signature
If signature verification is enabled during configuration, we include the app_id used for signing in the HTTP request URL and use the corresponding token to generate the signature, ensuring the legitimacy of the request source.
The specific signing method is as follows:
str = token + time.Now.Unix + skill_name + intent_name + query
signature = MD5(str)
Upon receiving the request, the service can recalculate this set of parameters and compare the signature to verify legitimacy. The token and app_id are generated by the dialogue platform and correspond one-to-one. You must store them securely. If leakage occurs, they can be reset.
Encryption
If data encryption is enabled in the configuration, the body data will be symmetrically encrypted using AES, then Base64 encoded before transmission. The encryption algorithm used is AES CBC. After enabling encryption, the response body must also be symmetrically encrypted using the same secret key and Base64 encoded.
For demonstration purposes, the AESKey is a 32-byte value that has been Base64 encoded with the trailing = removed. Therefore, to obtain the actual 32-byte Key, an = must be appended to the AESKey before performing Base64 decoding.
Golang Example
// Encrypt using AES algorithm and encode
func encrypt(encodingAesKey, msg string) (string, error) {
// AES encryption
encryptedMsg, err := AesCBCEncrypt(encodingAesKey, []byte(msg))
if err != nil {
return "", err
}
// Base64 encoding
base64Msg := make([]byte, base64.StdEncoding.EncodedLen(len(encryptedMsg)))
base64.StdEncoding.Encode(base64Msg, encryptedMsg)
return string(base64Msg), nil
}
// Decode and decrypt using AES algorithm
func decrypt(encodingAesKey, msg string) (string, error) {
// Base64 decoding
cipherText, err := base64.StdEncoding.DecodeString(msg)
if err != nil {
return "", err
}
// AES decryption
plainText, err := AesCBCDecrypt(encodingAesKey, []byte(cipherText))
if err != nil {
return "", err
}
return string(plainText), nil
}
func AesCBCEncrypt(encodingKey string, plaintextMsg []byte) ([]byte, error) {
block, aesKey, err := decodeAesKey(encodingKey)
if err != nil {
return []byte{}, err
}
plaintextMsg = pkcs5Padding(plaintextMsg, block.BlockSize())
cipherText := make([]byte, len(plaintextMsg))
iv := aesKey[:aes.BlockSize]
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText, plaintextMsg)
return cipherText, nil
}
func AesCBCDecrypt(encodingKey string, encryptedMsg []byte) ([]byte, error) {
block, aesKey, err := decodeAesKey(encodingKey)
if err != nil {
return []byte{}, err
}
iv := aesKey[:aes.BlockSize]
mode := cipher.NewCBCDecrypter(block, iv)
decryptedMsg := make([]byte, len(encryptedMsg))
mode.CryptBlocks(decryptedMsg, encryptedMsg)
decryptedMsg = pkcs5UnPadding(decryptedMsg)
return decryptedMsg, nil
}
func decodeAesKey(encodingAesKey string) (cipher.Block, []byte, error) {
aesKey, err := base64.StdEncoding.DecodeString(encodingAesKey + "=")
if err != nil {
return nil, aesKey, err
}
block, e := aes.NewCipher(aesKey)
if e != nil {
return nil, aesKey, err
}
return block, aesKey, nil
}
func pkcs5Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func pkcs5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
JavaScript Example
const crypto = require('crypto');
//example only, do not recommend hardcode in code base.
const config = {
appid: 'Gg8HejYTkUsEIlG',
token: 'YV78Pyj1VvqdNGpMJ1pHic0bIBOWMv',
encodingAESKey: 'q1Os1ZMe0nG28KUEx9lg3HjK7V5QyXvi212fzsgDqgz'
}
function pkcs5UnPadding (text) {
let pad = text[text.length - 1];
if (pad < 1 || pad > 32) {
pad = 0;
}
return text.slice(0, text.length - pad);
};
function pkcs5Padding (text) {
const blockSize = 32;
const textLength = text.length;
const amountToPad = blockSize - (textLength % blockSize);
const result = Buffer.alloc(amountToPad);
result.fill(amountToPad);
return Buffer.concat([text, result]);
};
function decrypt(text) {
const { AESKey, iv } = getAESKey();
const decipher = crypto.createDecipheriv('aes-256-cbc', AESKey, iv);
decipher.setAutoPadding(false);
const deciphered = Buffer.concat([decipher.update(text, 'base64'), decipher.final()]);
return pkcs5UnPadding(deciphered).toString();
}
function encrypt(text) {
const { AESKey, iv } = getAESKey();
const msg = Buffer.from(text);
const encoded = pkcs5Padding(msg);
var cipher = crypto.createCipheriv('aes-256-cbc', AESKey, iv);
cipher.setAutoPadding(false);
var cipheredMsg = Buffer.concat([cipher.update(encoded), cipher.final()]);
return cipheredMsg.toString('base64');
}
//
function getAESKey() {
const { encodingAESKey } = config;
const AESKey = Buffer.from(encodingAESKey + '=', 'base64');
if (AESKey.length !== 32) {
throw new Error('encodingAESKey invalid');
}
return {
AESKey,
iv: AESKey.slice(0, 16)
}
}
function requestHandler(request) {
//get full body from your http request context
const { body } = request;
/**
* decrypted data
* {
RequestId: '123123456456789789123456789',
SessionId: '12345678901234567_12345678909876543',
Query: 'What is the license plate restriction tail number in Beijing',
SkillName: 'Restriction',
IntentName: 'Check Restriction Tail Number',
Slots: [
{
SlotName: 'from_loc',
SlotValue: 'Beijing',
NormalizeValue: '{"type":"LOC_CHINA_CITY","city":"Beijing City","city_simple":"Beijing","loc_ori":"Beijing"}'
}
],
Timestamp: 1704135845,
Signature: '96f439043e1f7d2bb38162e35406f173',
ThirdApiId: 1234,
ThirdApiName: 'Vehicle Restriction',
UserId: '97f7e892'
}
*/
const data = JSON.parse((decrypt(body)));
//your product logic...
return encrypt(JSON.stringify(data));
}
//test only
requestHandler({
body: 'rUWkvTY9vRPOeVDSH/IdNXHmvgsUQtPkp7QtBQjSS1tcuTHGPWv8O3PlxbnsjCogsM7+EY+As4yF2kp4yxXpP2U7RmbDsU/luRO/EqkpFFsoxMZZArz2XH1YeSdnDyHYPWzjiicBYjNiqqpTMX8ekrqooN0cCEH7JBcbEe6btmiK8hZkysKTUJfG1DTpbONxON5+YuVPelVpzW5ry9sRYLDcqhImMb9FqI+BlIVAIXt5g+e70rheSqpeXz98pEROx7yPeRi3tXPAibuwg+vKDhoN6LuM0hzvyNzPjwK2gMmQB5yVuBZUalYIIZTVaMNGu4H6RK6MovLyM2cKfMUTphKaBBKpAvsV0o4/QRY0MvxeRYvZAQXEzOG3dJ7BRB2KEqBKttT7jMK8MO5HEXDE0CJxtNI4Rjww9XYmPhBM7lOZSF97YNEg1NhwcXvUc3YcrR334PhWJeu2dZCHaJzBqVXFxq/WprNHM0Gw06o6p5oWb4/nzXKYbpJWDyqTN/aztwo5sppHwlYrzNzF7gERP691qoabTHiCd0H+Ea3t65gTyNo2+ssvS1RVsKubApS4BkbZb/EaZCTKP20pcvDBoJk3QLi8ObyBq8sIcLwVjzelLMUgCDa059gBuao+S9qdHXebEZyS49BqAxngMWjHU5uCRO/x2b9w8nwfCCT8b0Q=' });
### Java Example
```java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
class AESUtil {
public static Base64.Encoder encoder = Base64.getEncoder();
public static Base64.Decoder decoder = Base64.getDecoder();
public static final String aesKey = "q1Os1ZMe0nG28KUEx9lg3HjK7V5QyXvi212fzsgDqgz=";// Note: The key assigned by the platform requires an '=' at the end
// Encrypt the JSON string str using aesKey, then encode with Base64
public static String encrypt(String str) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] secretKey = decoder.decode(aesKey.getBytes());
SecretKey keySpec = new SecretKeySpec(secretKey, "AES");
byte[] iv = new byte[16];
System.arraycopy(secretKey, 0, iv, 0, 16);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] aesData = cipher.doFinal(str.getBytes());
return new String(encoder.encode(aesData), "UTF-8");
}
// Decode the Base64-encoded encrypted str, decrypt it, and obtain the JSON string
public static String decrypt(String str) throws Exception {
byte[] secretKey = decoder.decode(aesKey.getBytes());
byte[] iv = new byte[16];
System.arraycopy(secretKey, 0, iv, 0, 16);
byte[] data = decoder.decode(str.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey keySpec = new SecretKeySpec(secretKey, "AES");
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivps);
return new String(cipher.doFinal(data), "UTF-8");
}
}
public class Main {
public static void main(String[] args) throws Exception {
String resp = "{\n" +
" \"answer_type\": \"text\",\n" +
" \"text_info\": {\n" +
" \"short_answer\": \"answer\"\n" +
" }\n" +
"}\n";
String aesResp = AESUtil.encrypt(resp);
System.out.println(aesResp);
String body = AESUtil.decrypt(aesResp);
System.out.println(body);
String demo = AESUtil.decrypt("rUWkvTY9vRPOeVDSH/IdNXHmvgsUQtPkp7QtBQjSS1tcuTHGPWv8O3PlxbnsjCogsM7+EY+As4yF2kp4yxXpP2U7RmbDsU/luRO/EqkpFFsoxMZZArz2XH1YeSdnDyHYPWzjiicBYjNiqqpTMX8ekrqooN0cCEH7JBcbEe6btmiK8hZkysKTUJfG1DTpbONxON5+YuVPelVpzW5ry9sRYLDcqhImMb9FqI+BlIVAIXt5g+e70rheSqpeXz98pEROx7yPeRi3tXPAibuwg+vKDhoN6LuM0hzvyNzPjwK2gMmQB5yVuBZUalYIIZTVaMNGu4H6RK6MovLyM2cKfMUTphKaBBKpAvsV0o4/QRY0MvxeRYvZAQXEzOG3dJ7BRB2KEqBKttT7jMK8MO5HEXDE0CJxtNI4Rjww9XYmPhBM7lOZSF97YNEg1NhwcXvUc3YcrR334PhWJeu2dZCHaJzBqVXFxq/WprNHM0Gw06o6p5oWb4/nzXKYbpJWDyqTN/aztwo5sppHwlYrzNzF7gERP691qoabTHiCd0H+Ea3t65gTyNo2+ssvS1RVsKubApS4BkbZb/EaZCTKP20pcvDBoJk3QLi8ObyBq8sIcLwVjzelLMUgCDa059gBuao+S9qdHXebEZyS49BqAxngMWjHU5uCRO/x2b9w8nwfCCT8b0Q=");
System.out.println(demo);
}
}
Input
Request Type
POST
Request Parameters
query
In the url query, the following parameters are carried in plaintext:
app_id
If you need to pass other parameters to distinguish the request source, or to implement other logic that must be executed before decrypting the data, you can configure them in the url yourself.
Example:
If the user configures the interface as https://api.example.com/app, the url actually received by the server is https://api.example.com/app?app_id=Gg8HejYTkUsEIlG
If the interface configured by the user contains custom parameters: https://api.example.com/app?bot_id=123abc&key=value, the url actually received by the server is https://api.example.com/app?bot_id=123abc&key=value&app_id=Gg8HejYTkUsEIlG
body
The following data is encrypted and passed in the body:
type XwBrainThirdApiReqInfo struct {
RequestId string
SessionId string
Query string
SkillName string
IntentName string
Slots []*MatchSlotInfo
Timestamp int64
Signature string
ThirdApiId uint64
ThirdApiName string
UserId string
}
type MatchSlotInfo struct {
SlotName string
SlotValue string
NormalizeValue string
}
JSON example:
{
"RequestId": "123123456456789789123456789",
"SessionId": "12345678901234567_12345678909876543",
"Query": "What is the license plate tail number restriction in Beijing?",
"SkillName": "Traffic Restriction",
"IntentName": "Check Traffic Restriction Tail Number",
"Slots": [
{
"SlotName": "from_loc",
"SlotValue": "Beijing",
"NormalizeValue": "{\"type\":\"LOC_CHINA_CITY\",\"city\":\"Beijing\",\"city_simple\":\"Beijing\",\"loc_ori\":\"Beijing\"}"
}
],
"Timestamp": 1704135845,
"Signature": "96f439043e1f7d2bb38162e35406f173",
"ThirdApiId": 1234,
"ThirdApiName": "Vehicle Traffic Restriction",
"UserId": "97f7e892"
}
Field descriptions:
| Field | Description |
|---|---|
| RequestId | Unique request ID |
| SessionId | Request session ID |
| Query | User query |
| SkillName | Skill name |
| IntentName | Intent name |
| Slots | Semantic slot data |
| Timestamp | Unix timestamp |
| Signature | Signature |
| ThirdApiId | Third-party service API ID |
| ThirdApiName | Service API name |
| UserId | User ID |
Slots field descriptions:
| Field | Description |
|---|---|
| SlotName | Semantic slot name |
| SlotValue | Semantic slot value |
| NormalizeValue | Normalized semantic slot value |
Output
After the service interface processes the request, the returned body data must be in JSON format. If encryption is selected, it must be the base64-encoded form of this JSON data after encryption. Additionally, the JSON data must conform to one of the following specified formats:
1. Text
When you need to return simple text data, use the following data format:
{
"answer_type": "text",
"text_info": {
"short_answer": "answer"
}
}
2. Composite Messages
Supports returning up to 3 messages at once.
{
"answer_type": "complex",
"complex_info": {
"view_type": "multi",
"multi": [
{
"view_type": "text",
"text_info": {
"short_answer": "answer 1"
}
},
{
"view_type": "text",
"text_info": {
"short_answer": "answer 2"
}
},
{
"view_type": "text",
"text_info": {
"short_answer": "answer 3"
}
}
]
}
}
Note:
- Your API should return a response within 2 seconds; for asynchronous replies upon timeout, you can implement this via the separate Customer Service Message Push API.
- The data returned by the API should not exceed 2M.
Example
- appid:
Gg8HejYTkUsEIlG - token:
YV78Pyj1VvqdNGpMJ1pHic0bIBOWMv - aeskey:
q1Os1ZMe0nG28KUEx9lg3HjK7V5QyXvi212fzsgDqgz - url:
http://example.com/ - method:
POST
Suppose the intent is matched:
- SkillName: Traffic Restriction
- IntentName: Check Restricted License Plate Numbers
At this time, your API endpoint http://example.com will receive:
http://example.com?app_id=Gg8HejYTkUsEIlG
The body data is:
rUWkvTY9vRPOeVDSH/IdNXHmvgsUQtPkp7QtBQjSS1tcuTHGPWv8O3PlxbnsjCogsM7+EY+As4yF2kp4yxXpP2U7RmbDsU/luRO/EqkpFFsoxMZZArz2XH1YeSdnDyHYPWzjiicBYjNiqqpTMX8ekrqooN0cCEH7JBcbEe6btmiK8hZkysKTUJfG1DTpbONxON5+YuVPelVpzW5ry9sRYLDcqhImMb9FqI+BlIVAIXt5g+e70rheSqpeXz98pEROx7yPeRi3tXPAibuwg+vKDhoN6LuM0hzvyNzPjwK2gMmQB5yVuBZUalYIIZTVaMNGu4H6RK6MovLyM2cKfMUTphKaBBKpAvsV0o4/QRY0MvxeRYvZAQXEzOG3dJ7BRB2KEqBKttT7jMK8MO5HEXDE0CJxtNI4Rjww9XYmPhBM7lOZSF97YNEg1NhwcXvUc3YcrR334PhWJeu2dZCHaJzBqVXFxq/WprNHM0Gw06o6p5oWb4/nzXKYbpJWDyqTN/aztwo5sppHwlYrzNzF7gERP691qoabTHiCd0H+Ea3t65gTyNo2+ssvS1RVsKubApS4BkbZb/EaZCTKP20pcvDBoJk3QLi8ObyBq8sIcLwVjzelLMUgCDa059gBuao+S9qdHXebEZyS49BqAxngMWjHU5uCRO/x2b9w8nwfCCT8b0Q=
After base64 decoding and decrypting the body data, it becomes:
{
"RequestId": "123123456456789789123456789",
"SessionId": "12345678901234567_12345678909876543",
"Query": "What is the license plate restriction in Beijing?",
"SkillName": "Traffic Restriction",
"IntentName": "Check Restricted License Plate Numbers",
"Slots": [
{
"SlotName": "from_loc",
"SlotValue": "Beijing",
"NormalizeValue": "{\"type\":\"LOC_CHINA_CITY\",\"city\":\"Beijing City\",\"city_simple\":\"Beijing\",\"loc_ori\":\"Beijing\"}"
}
],
"Timestamp": 1704135845,
"Signature": "96f439043e1f7d2bb38162e35406f173",
"ThirdApiId": 1234,
"ThirdApiName": "Vehicle Traffic Restriction",
"UserId": "97f7e892"
}
The signature is generated by md5(“YV78Pyj1VvqdNGpMJ1pHic0bIBOWMv1704135845Traffic RestrictionCheck Restricted License Plate NumbersBeijingWhat is the license plate restriction in Beijing?”) and is 96f439043e1f7d2bb38162e35406f173