全部產品
Search
文件中心

Intelligent Media Services:向用戶端發送自訂訊息

更新時間:Oct 30, 2025

本文將為您介紹在音視訊通話中如何主動向用戶端發送自訂訊息。

功能描述

在與智能體對話過程中,服務端推送商務資訊可以全方位提升使用者的溝通協作體驗與業務處理效率。對於使用者而言,能在專註於音視頻交流的同時,及時擷取關鍵業務動態,無需在通話結束後再另行查詢資訊,極大地節省了時間與精力。從業務角度出發,服務端主動推送資訊可確保重要內容精準觸達使用者,增強使用者對業務的參與度與理解程度,助力商務程序的高效推進。

音視頻智能體

應用情境

在音視訊通話情境中,服務端主動推送同樣可以廣泛應用於商務撮合、虛擬教師、心理諮詢等多個領域。以虛擬教師情境為例:教師與學生進行互動時,服務端可推送課程資料更新、作業提交提醒、考試安排等資訊。比如,當課程資料有新的補充文檔時,服務端在音視訊通話介面推播通知,學生點擊即可下載查看,方便學生及時擷取學習資源。在課程即將結束時,推送作業提交截止時間提醒,確保學生按時完成作業。此外,在考試周前,提前推送考試時間、考試形式等詳細安排,讓學生做好充分準備。

功能實現

服務端發送自訂訊息

SendAIAgentDataChannelMessage - 向智能體通話發送DataChannel訊息

// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.rtc;

import java.util.Arrays;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ice20201109.models.StartAIAgentInstanceResponse;
import com.aliyun.tea.*;

public class Sample {

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化帳號Client</p>
     * @return Client
     * 
     * @throws Exception
     */
    public static com.aliyun.ice20201109.Client createClient() throws Exception {
        // 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("yourak")
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("yoursk");
        // Endpoint 請參考 https://api.aliyun.com/product/ICE
        config.endpoint = "ice.cn-shanghai.aliyuncs.com";
        return new com.aliyun.ice20201109.Client(config);
    }


    private static void sendAIAgentDataChannelMessage() throws Exception {
        com.aliyun.ice20201109.Client client =  createClient();

        com.aliyun.ice20201109.models.SendAIAgentDataChannelMessageRequest request = new com.aliyun.ice20201109.models.SendAIAgentDataChannelMessageRequest()
            .setInstanceId("yourinstanceid")
            .setMessage("{\"key\":\"value\"}");
            try {
                com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
                
                client.sendAIAgentDataChannelMessageWithOptions(request, runtime);
            } catch (TeaException error) {
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            }   
    }
    
    public static void main(String[] args) throws Exception {
        sendAIAgentDataChannelMessage();
    }
}

用戶端處理自訂訊息

您需要對服務端發送的自訂訊息進行解析,範例程式碼如下:

Android

//實現ARTCAICallEngine.IARTCAICallEngineCallback中的回調

@Override
public void onReceivedAgentCustomMessage(String data) {
// 這裡處理收到當前智能體發過來的自訂訊息
}

iOS

// 建立通話engine執行個體
var engine: ARTCAICallEngineInterface = {
    return ARTCAICallEngineFactory.createEngine()
}()

// 給通話引擎設定回調
self.engine.delegate = self

// 實現回調介面
public func onReceivedAgentCustomMessage(data: [String : Any]?) {
  // 這裡處理收到當前智能體發過來的自訂訊息
    debugPrint("onReceivedAgentCustomMessage:\(data ?? [:])")
}

Web

engine.on('receivedAgentCustomMessage', (data) => {
  // 這裡處理收到當前智能體發過來的自訂訊息
  console.log('receivedAgentCustomMessage', data);
})

訊息對話智能體

應用情境

服務端主動推送訊息可以應用在電商、教育以及金融等多個領域。以電商情境為例:在使用者與電商客服的訊息對話過程中,當使用者瀏覽某類商品但未下單時,服務端可推送該商品的限時折扣、庫存預警等資訊。例如,使用者在諮詢某品牌手機的效能時,服務端推送該手機當前進行中的滿減活動資訊,以及該機型庫存僅剩 3 台的預警,刺激使用者下單購買。此外,在購物節期間,服務端還能向使用者推送專屬優惠券、熱門商品推薦等內容,引導使用者消費。

功能實現

服務端發送自訂訊息

SendMessageChatText - 向IM用戶端發送訊息

package com.aliyun.rtc;

import com.aliyun.tea.*;

public class SampleSendMessagetChatText {

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化帳號Client</p>
     * @return Client
     * 
     * @throws Exception
     */
    public static com.aliyun.ice20201109.Client createClient() throws Exception {
        // 工程代碼泄露可能會導致 AccessKey 泄露,並威脅帳號下所有資源的安全性。以下程式碼範例僅供參考。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("yourak")
                // 必填,請確保代碼運行環境設定了環境變數 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("yoursk");
        // Endpoint 請參考 https://api.aliyun.com/product/ICE
        config.endpoint = "ice.cn-shanghai.aliyuncs.com";
        return new com.aliyun.ice20201109.Client(config);
    }


    private static void sendMessageChatText() throws Exception {
        com.aliyun.ice20201109.Client client =  createClient();

        com.aliyun.ice20201109.models.SendMessageChatTextRequest request = new com.aliyun.ice20201109.models.SendMessageChatTextRequest()
            .setAIAgentId("youragentid")
            .setSessionId("yoursessionid")
            .setText("yourtext")
            .setReceiverId("receiverid")
            .setType("announcement") // announcement or custom
            .setNeedArchiving(true);
            try {
                com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
                
                client.sendMessageChatTextWithOptions(request, runtime);
            } catch (TeaException error) {
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            }   
    }
    
    public static void main(String[] args) throws Exception {
        sendMessageChatText();
    }
}

用戶端處理自訂訊息

您需要對服務端發送的自訂訊息進行解析,範例程式碼如下:

Android

//實現ARTCAIChatEngine.IARTCAIChatEngineCallback中的回調

 @Override
public void onReceivedCustomMessage(String  text) {
    // 這裡處理收到當前智能體發過來的自訂訊息
}

iOS

// 建立訊息對話engine執行個體
let engine: ARTCAIChatEngineInterface = {
    return ARTCAICallEngineFactory.createChatEngine()
}()

// 給通話引擎設定回調
self.engine.delegate = self

// 實現回調介面
public func onReceivedCustomMessage(text: String) {
    // 在這裡處理自訂訊息
    debugPrint("onReceivedAgentCustomMessage:\(text)")
}

Web

engine.on('receivedCustomMessage', (text) => {
  // 在這裡處理自訂訊息
  console.log('receivedCustomMessage', text);
});