TaskRouter.js TaskQueue: ブラウザーでTaskQueueリソースを管理する
SDKにより、開発者はシンプルなJS APIでTaskRouterの全REST APIを使用できます。
アプリケーションに SDK を追加する
以下のように、TaskRouter JS SDK を JavaScript アプリケーションに取り込みます。
<script type="text/javascript" src="//media.twiliocdn.com/taskrouter/js/v1.14/taskrouter.min.js"></script>
TaskRouterのケイパビリティートークンの作成
TaskRouterはTwilioケイパビリティートークンを使って、TaskRouterリソースへのスコープ宣言されたアクセスをJavaScriptアプリケーションに委託します。 Twilioケイパビリティートークンは、JSON Web Token(一般にJWTと呼ばれ、「jot」と発音)規格に準拠し、第三者がクレデンシャルを一定期間使うことができます。TaskRouterを登録するには、WebサーバーはTwilioケイパビリティートークンを生成し、それをJavaScriptアプリケーションに提供する必要があります。
アクセスケイパビリティーを実現するために、3 つの関連するヘルパーメソッドを提供しています。
Capability | 認証 |
---|---|
AllowFetchSubresources | A TaskQueue can fetch any subresource (statistics) |
AllowUpdates | A TaskQueue can update its properties |
AllowDelete | A TaskQueue can delete itself |
さらに、これらのケイパビリティーを越え、特定のリソースへのアクセスをもっと細かく利用できます。これは、「JWT の構築」で参照できます。
Twilioのどのヘルパーライブラリーを使用しても、TaskRouterケイパビリティートークンを生成できます。 TwilioのAccountSidとAuthToken、TaskQueueが属するWorkspaceSid、登録したいTaskQueueSidを渡す必要があります。 たとえば、PHPヘルパーライブラリーを使用する場合、次のようにしてトークンを作成し、ケイパビリティーを追加できます。
TaskRouterケイパビリティートークンを生成したら、それをフロントエンドウェブアプリケーションに渡して、次のようにJavaScriptライブラリを初期化できます。
var taskQueue = new Twilio.TaskRouter.TaskQueue(TASKQUEUE_TOKEN);
このライブラリがTaskRouterに接続されると、「ready」Eventが発生します。
taskQueue.on("ready", function(taskQueue) { console.log(taskQueue.sid) // 'WQxxx' console.log(taskQueue.friendlyName) // 'Simple FIFO Queue' console.log(taskQueue.targetWorkers) // '1==1' console.log(taskQueue.maxReservedWorkers) // 20 });
このオブジェクトで公開されているメソッドとイベントについては、下記をご覧ください。
TaskQueue API
TaskRouter.js TaskQueue は、以下の API を公開しています。
Twilio.TaskRouter.TaskQueue
Twilio.TaskRouter.TaskQueue is the top-level class you'll use for managing a TaskQueue.
new Twilio.TaskRouter.TaskQueue(taskQueueToken)
taskQueueToken
で提供されているケイパビリティーで新しい Twilio.TaskRouter.TaskQueue を登録します。
パラメーター
名前 | Type | 概要 |
---|---|---|
taskQueueToken | 文字列 | Twilio TaskRouterケイパビリティートークン。詳細については、TaskRouterケイパビリティートークンの作成をご覧ください。 |
debug | boolean | (オプション)JS SDKがEventメッセージをコンソールに出力するかどうかを示します。初期値は true です。 |
使用例
var taskQueue = new Twilio.TaskRouter.TaskQueue(TASKQUEUE_TOKEN);
デバッグを無効にします。
var taskQueue = new Twilio.TaskRouter.TaskQueue(TASKQUEUE_TOKEN, false);
メソッド
update([args...], [resultCallback])
Updates a single or list of properties on a TaskQueue.
パラメーター
名前 | Type | 概要 |
---|---|---|
args... | 文字列または JSON | 単一の API パラメーターおよび値、または複数の値を含む JSON オブジェクト。 |
resultCallback | 関数 | (optional) A JavaScript Function that will be called with the result of the update. If an error occurs, the first argument passed to this function will be an Error. If the update is successful, the first argument will be null and the second argument will contain the updated TaskQueue. |
単一の属性の例
taskQueue.update("MaxReservedWorkers", "20", function(error, taskQueue) { if(error) { console.log(error.code); console.log(error.message); } else { console.log(taskQueue.maxReservedWorkers); // 20 } });
複数の属性の例
var targetWorkers = "languages HAS \"english\""; var props = {"MaxReservedWorkers", "20", "TargetWorkers":targetWorkers}; taskQueue.update(props, function(error, workspace) { if(error) { console.log(error.code); console.log(error.message); } else { console.log(taskQueue.maxReservedWorkers); // "20" console.log(taskQueue.targetWorkers); // "languages HAS "english"" } });
delete([resultCallback])
Deletes a TaskQueue
パラメーター
名前 | Type | 概要 |
---|---|---|
resultCallback | 関数 | (オプション)削除の結果、呼び出される JavaScript 関数。エラーが発生した場合は、この関数に最初に渡される引数がエラーになります。削除が成功した場合、最初の引数は null になります。 |
例
taskQueue.delete(function(error) { if(error) { console.log(error.code); console.log(error.message); } else { console.log("taskQueue deleted"); } });
updateToken(taskQueueToken)
Workspaceに対するTaskRouterケイパビリティートークンを更新します。
パラメーター
名前 | Type | 概要 |
---|---|---|
taskQueueToken | 文字列 | 有効なTaskRouterケイパビリティートークンです。 |
例
var token = refreshJWT(); // your method to retrieve a new capability token taskQueue.updateToken(token);
統計
Retrieves the object to retrieve the statistics for a TaskQueue.
取得
パラメーター
名前 | Type | 概要 |
---|---|---|
params | JSON | (オプション)クエリーパラメーターの JSON オブジェクト。 |
コールバック | 関数 | 統計オブジェクトが返されたときに呼び出される関数。リストの取得中にエラーが発生した場合は、この関数に最初に渡されるパラメーターにエラーオブジェクトが渡されます。取得が成功した場合は、最初のパラメーターは null になり、2 番目のパラメーターには統計オブジェクトが渡されます。 |
var queryParams = {"Minutes":"240"}; // 4 hours taskQueue.statistics.fetch( queryParams, function(error, statistics) { if(error) { console.log(error.code); console.log(error.message); return; } console.log("fetched taskQueue statistics: "+JSON.stringify(statistics)); console.log("avg task acceptance time: "+statistics.cumulative.avgTaskAcceptanceTime; } );
累積の統計
If you only care about the cumulative stats for a TaskQueue for a given time period, you can utilize this instead of the above for a smaller payload and faster response time.
取得
パラメーター
名前 | Type | 概要 |
---|---|---|
params | JSON | (オプション)クエリーパラメーターの JSON オブジェクト。 |
コールバック | 関数 | 統計オブジェクトが返されたときに呼び出される関数。リストの取得中にエラーが発生した場合は、この関数に最初に渡されるパラメーターにエラーオブジェクトが渡されます。取得が成功した場合は、最初のパラメーターは null になり、2 番目のパラメーターには統計オブジェクトが渡されます。 |
var queryParams = {"Minutes":"240"}; // 4 hours taskQueue.cumulativeStats.fetch( queryParams, function(error, statistics) { if(error) { console.log(error.code); console.log(error.message); return; } console.log("fetched taskQueue statistics: "+JSON.stringify(statistics)); console.log("avg task acceptance time: "+statistics.avgTaskAcceptanceTime; } );
リアルタイムの統計
If you only care about the realtime stats for a TaskQueue for a given time period, you can utilize this instead of the above for a smaller payload and faster response time.
取得
パラメーター
名前 | Type | 概要 |
---|---|---|
params | JSON | (オプション)クエリーパラメーターの JSON オブジェクト。 |
コールバック | 関数 | 統計オブジェクトが返されたときに呼び出される関数。リストの取得中にエラーが発生した場合は、この関数に最初に渡されるパラメーターにエラーオブジェクトが渡されます。取得が成功した場合は、最初のパラメーターは null になり、2 番目のパラメーターには統計オブジェクトが渡されます。 |
taskQueue.realtimeStats.fetch( function(error, statistics) { if(error) { console.log(error.code); console.log(error.message); return; } console.log("fetched taskQueue statistics: "+JSON.stringify(statistics)); console.log("total available workers: "+statistics.totalAvailableWorkers; console.log("total eligible workers: "+statistics.totalEligibleWorkers; } );
on(event, callback)
リスナーを指定されたEventに接続します。サポートされているすべてのEventのリストについては、Eventをご覧ください。
パラメーター
名前 | Type | 概要 |
---|---|---|
イベント | 文字列 | Event名です。サポートされているすべてのEventのリストについては、Eventをご覧ください。 |
コールバック | 関数 | 指定されたEventが発生したときに呼び出される関数です。 |
例
taskQueue.on("ready", function(taskQueue) { console.log(taskQueue.friendlyName) // My TaskQueue });
Events
TaskRouter's JS library currently raises the following events to the registered TaskQueue:
ready
TaskQueueがTaskRouterとの接続を確立し、初期化を完了しました。
パラメーター
名前 | Type | 概要 |
---|---|---|
taskQueue | TaskQueue | The created TaskQueue. |
例
taskQueue.on("ready", function(taskQueue) { console.log(taskQueue.friendlyName) // My TaskQueue });
connected
TaskQueue は TaskRouter への接続を確立させました。
例
taskQueue.on("connected", function() { console.log("Websocket has connected"); });
disconnected
TaskQueue は TaskRouter からの接続を切断しました。
例
taskQueue.on("disconnected", function() { console.log("Websocket has disconnected"); });
token.expired
このTaskQueueを作成するために使用したTaskRouterケイパビリティートークンの有効期限が切れた場合に発行されます。
例
taskQueue.on("token.expired", function() { console.log("updating token"); var token = refreshJWT(); // your method to retrieve a new capability token taskQueue.updateToken(token); });
ヘルプが必要ですか?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.