Twilioでエージェント可用性の切替えを設定
エージェントを使用可能または使用不可にするには、Twilio SDKのDeviceオブジェクトを使用する必要があります。 Device
オブジェクトは、インバウンドおよびアウトバウンドのオーディオ接続に役立つようにTwilioと通信するソフト・フォンを表します。
概要
メディア・ツールバー・アプリケーションの「エージェントの可用性の切替え」ボタンをクリックして、エージェントが自分が使用可能としてマークされると実行される一連の操作の概要を次に示します:
- エージェントは、メディア・ツールバーのエージェントの可用性の切り替えボタンをクリックします。
- メディア・ツールバー・アプリケーションは、REST APIコールを起動してIDをフェッチし、認証に必要なトークンをフェッチします。
- Twilioサービスは、リクエストを認証し、IDおよびトークンを返します。
- メディア・ツールバー・アプリケーションは、このトークンを使用して、Twilio SDKによって提供されるデバイス・オブジェクトを初期化します。
- デバイス・オブジェクトがTwilioに登録されると、
registered
イベントが起動します。 - メディア・ツールバー・アプリケーションで登録済イベントを受信すると、エージェントの状態を使用可能にするための
invoke agentStateEvent
UEF操作。 - フュージョン・アプリケーションがこのアクションを識別すると、Fusionアプリケーションでもエージェントの状態が「使用可能」になります。
VendorHandlerのmakeAgentAvailableメソッドを更新してサプライヤAPIをコールし、エージェントを使用可能にします
- 次のコードを使用して、
vendorHandler
クラス(src/ts/cti/vendor/vendorHandler.ts
)にメソッドを作成します:private async getIdAndToken(): Promise<any> { const headers: Headers = (new Headers()) as Headers; const url: string = 'https://twilio-node-voice-stream.com/token'; // Replace this url with the url of the deployed node app headers.set('Accept', 'application/json'); const request: Request = new Request(url, { method: 'GET', headers: headers }) as Request; const idAndToken: Response = await fetch(request); this.idAndToken = await idAndToken.json(); return this.idAndToken; }
- 次のimport文を追加します:
import { Call, Device } from '@twilio/voice-sdk'; import { ICtiVendorHandler } from './ICtiVendorHandler'; import { IntegrationEventsHandler } from '../integrationEventsHandler';
- 次に示すように、Twilio、
device
およびidAndToken
に2つのクラス・プロパティを作成します:export class VendorHandler implements ICtiVendorHandler { private twilio: any; private device: Device | null; private integrationEventsHandler: IntegrationEventsHandler; public idAndToken: any; constructor(integrationEventsHandler: IntegrationEventsHandler) { this.twilio = (window as any).Twilio; this.device = null; this.idAndToken = null; this.integrationEventsHandler = integrationEventsHandler; } // ... }
DeviceOptions
を使用してデバイスをインスタンス化します:const idAndToken = await this.getIdAndToken(); // get the id token this.device = new this.twilio.Device(idAndToken.token, { logLevel: 1, codecPreferences: ["opus", "pcmu"], enableRingingState: true });
- エージェントを使用可能にするには、次のようにデバイス・インスタンスでレジスタをコールします:
this.device.register();
- 次のように、
registered
イベントおよびerror
イベントを使用して、登録イベントの結果を取得します:this.device.on("registered", () => { // Do your logic when registration is completed. }); this.device.on("error", (deviceError) => { // Do your logic when the registration fails });
- 次に示すように、
vendorHandler
のmakeAgentAvailable
メソッドを更新します:public async makeAgentAvailable(): Promise<void> { await this.getIdAndToken(); this.device = this.twilio.Device(this.idAndToken.token, { logLevel: 1, codecPreferences: ["opus", "pcmu"], enableRingingState: true }); let resolve: Function; let reject: Function; if (this.device) { this.device.on("registered", () => { console.log("Registration completed ...") resolve(); }); this.device.on("error", (deviceError) => { console.error("Registration Failed ...", deviceError); reject(); }); this.device.register(); } return new Promise((res: Function, rej: Function) => { resolve = res; reject = rej; }); }
- 次に示すように、
VendorHandler
のmakeAgentUnavailable
メソッドを更新します:public async makeAgentUnavailable(): Promise<void> { let resolve: Function; let reject: Function; if (this.device) { this.device.on("unregistered", () => { console.log("Successfully UnRegistered ..."); resolve(); }); this.device.on("error", (deviceError) => { console.error("Failed to unregister ...", deviceError); reject(); }); this.device.unregister(); } return new Promise((res: Function, rej: Function) => { resolve = res; reject = rej; }); }
- 完全なコードを表示します:
import { Call, Device } from '@twilio/voice-sdk'; import { ICtiVendorHandler } from './ICtiVendorHandler'; import { IntegrationEventsHandler } from '../integrationEventsHandler'; export class VendorHandler implements ICtiVendorHandler { private twilio: any; private device: Device | null; private integrationEventsHandler: IntegrationEventsHandler; public idAndToken: any; constructor() { this.twilio = (window as any).Twilio; this.device = null; this.idAndToken = null; this.integrationEventsHandler = integrationEventsHandler; } public async makeAgentAvailable(): Promise<void> { const idAndToken = await this.getIdAndToken(); this.device = new Device(idAndToken.token, { logLevel: 1, codecPreferences: [Call.Codec.Opus, Call.Codec.PCMU], //enableRingingState: true }); let resolve: Function; let reject: Function; if (this.device) { this.device.on("registered", () => { console.log("Registration completed ...") resolve(); }); this.device.on("error", (deviceError) => { console.error("Registration Failed ...", deviceError); reject(); }); this.device.register(); } return new Promise((res: Function, rej: Function) => { resolve = res; reject = rej; }); } public async makeAgentUnavailable(): Promise<void> { let resolve: Function; let reject: Function; if (this.device) { this.device.on("unregister", () => { console.log("Successfully UnRegistered ...") resolve(); }); this.device.on("error", (deviceError) => { console.error("Failed to unregister ...", deviceError); reject(); }); this.device.unregister(); } return new Promise((res: Function, rej: Function) => { resolve = res; reject = rej; }); } public async makeOutboundCall(phoneNumber: string, eventId: string): Promise<void> { throw new Error('Method not implemented.'); } public async acceptCall(): Promise<void> { throw new Error('Method not implemented.'); } public async rejectCall(): Promise<void> { throw new Error('Method not implemented.'); } public async hangupCall(): Promise<void> { throw new Error('Method not implemented.'); } private async getIdAndToken(): Promise<any> { const headers: Headers = (new Headers()) as Headers; headers.set('Accept', 'application/json'); const request: Request = new Request('https://twilio-node-voice-stream.com/token', { method: 'GET', headers: headers }) as Request; const idAndToken: Response = await fetch(request); this.idAndToken = await idAndToken.json(); return this.idAndToken; } }
進捗の確認
Fusionアプリケーションにサインインし、メディア・ツールバーを開きます。 メディア・ツールバー・アプリケーションから「エージェント可用性」ボタンをクリックします。 Fusionアプリケーションのボタンの色が変わり、電話アイコンのステータスが「使用可能」に変更されることがわかります。