共用方式為


適用於 JavaScript 的 Azure 通訊通話自動化用戶端連結庫 - 1.3.0 版

此套件包含適用於 Azure 通訊通話自動化的 JavaScript SDK。 通話自動化可讓開發人員建置伺服器型、智慧型手機工作流程,以及語音和 PSTN 通道的通話錄製。

呼叫自動化概觀 | 產品檔

開始

先決條件

安裝

npm install @azure/communication-call-automation

瀏覽器支援

JavaScript 套件組合

若要在瀏覽器中使用此用戶端連結庫,您必須先使用配套程式。 如需如何執行這項操作的詳細資訊,請參閱我們的 組合檔

重要概念

名字 描述
CallAutomationClient CallAutomationClient 是使用此用戶端連結庫的開發人員的主要介面。 它可用來透過 createCallanswerCall來起始呼叫。
CallConnection CallConnection 代表進行中的呼叫。 一旦使用 createCallanswerCall建立呼叫之後,就可以針對呼叫執行進一步的動作,例如 transferaddParticipant
CallMedia CallMedia 可用來執行媒體相關動作,例如 play,以播放媒體檔案。 這可以從已建立的 CallConnection擷取。
CallRecording CallRecording 可用來執行錄製相關動作,例如 startRecording。 這可以從 CallAutomationClient擷取。
回呼事件 回呼事件是呼叫期間傳回的事件。 它會提供呼叫的資訊和狀態,例如 CallConnectedCallbackUrl 必須在 createCall 期間提供,answerCall,且回呼事件會傳送至此 URL。 您可以使用 callAutomationEventParser 在到達時剖析這些事件。
連入通話事件 當來電發生時(可使用 answerCall接聽),將會傳送來電 eventgrid 事件。 這與上述的回呼事件不同,應該在 Azure 入口網站上設定。 如需詳細資訊,請參閱 來電
CallAutomationEventProcessor CallAutomationEventProcessor 是處理 CallConnected等中呼叫回呼事件的一種方便方式。 這可確保呼叫與事件之間的相互關聯更容易。 請參閱下列範例以瞭解其用法。

例子

初始化 CallAutomationClient

import { CallAutomationClient } from '@azure/communication-call-automation';
import { DefaultAzureCredential } from "@azure/identity"; 

// Your unique Azure Communication service endpoint
const credential = new DefaultAzureCredential(); 
const endpointUrl = '<ENDPOINT>' 
const callAutomationClient = new CallAutomationClient(endpointUrl, credential); 

建立通話

import { CommunicationUserIdentifier } from "@azure/communication-common";
import { CallAutomationClient, CallInvite } from '@azure/communication-call-automation';

// target endpoint for ACS User
const target: CommunicationUserIdentifier = {
  communicationUserId:
    "8:acs:...",
}

// make invitation
const callInvite: CallInvite = {
   targetParticipant:target
};

// callback url to recieve callback events
const callbackUrl = "https://<MY-EVENT-HANDLER-URL>/events";

// send out the invitation, creating call
const response = callAutomationClient.createCall(callInvite, callbackUrl);

播放媒體

// from callconnection of response above, play media of media file
const myFile: FileSource = { uri: "https://<FILE-SOURCE>/<SOME-FILE>.wav" }
const response = callConnection.getCallMedia().playToAll(myFile);

處理 Mid-Connection 回呼事件

為了輕鬆處理中連線事件,呼叫自動化的 SDK 提供更容易的方式來處理這些事件。 看看 CallAutomationEventProcessor。 這可確保呼叫與事件之間的相互關聯更容易。

const eventProcessor: CallAutomationEventProcessor = await callAutomationClient.getEventProcessor();
eventProcessor.processEvents(incomingEvent);

EventProcessor 需要 ProcessEvents 才能運作。 EventProcessor 取用事件之後,您就可以開始使用其功能。

如需範例,請參閱下列範例:您要使用 CreateCall 進行通話,並等候呼叫的 CallConnected 事件。

// send out the invitation, creating call
const callInvite = new CallInvite(target);
const callbackUrl = "https://<MY-EVENT-HANDLER-URL>/events";
const callResult = callAutomationClient.createCall(callInvite, callbackUrl);

// giving 30 seconds timeout for waiting on createCall's event, 'CallConnected'
const createCallEventResult : CreateCallEventResult = await callResult.waitForEventProcessor(undefined, 30000);
// once this returns, call is now established!

// check if it was successful
if (createCallEventResult.isSuccess)
{
  // work with callConnected event
  const callConnectedEvent : CallConnected = createCallEventResult.successResult!;
}

故障排除

後續步驟

貢獻

如果您想要參與此連結庫,請閱讀 參與指南,以深入瞭解如何建置和測試程序代碼。