首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django / React中的Twilio TaskRouter

Django / React中的Twilio TaskRouter
EN

Stack Overflow用户
提问于 2020-08-30 09:08:03
回答 1查看 212关注 0票数 0

我正在用Twilio构建一个简单的联系中心应用程序。我正在后端创建TaskRouter辅助功能令牌,如下所示:序列化程序:

代码语言:javascript
复制
class TwilioTokenSerializer(serializers.BaseSerializer):
    def to_representation(self, instance):
        return {
            'token': instance,
        }

查看:

代码语言:javascript
复制
class TwilioWorkerView(generics.RetrieveAPIView):
    serializer_class = TwilioTokenSerializer
    def get_object(self):
        current_user = self.request.user.worker
        worker_sid   = current_user.worker_sid
        # Returns a Twilio Worker token for current agent
        capability = WorkerCapabilityToken(
            account_sid=TWILIO_ACCOUNT_SID,
            auth_token=TWILIO_AUTH_TOKEN,
            workspace_sid=TWILIO_WORKSPACE_SID,
            worker_sid=worker_sid
        )

        capability.allow_fetch_subresources()
        capability.allow_update_activities()
        capability.allow_update_reservations()
        token = capability.to_jwt()

        token = capability.to_jwt(ttl=28800)
        return token

在反应中:

代码语言:javascript
复制
import { Worker } from 'twilio-taskrouter';

componentDidMount(){
  axios.get('https://myURL.com/api/twilio-worker-token')
    .then(res =>{
        console.log(res.data.token)
        const worker = new Worker(res.data.token)
    }).catch(error =>{
      console.log(error)
    })

但是我得到403的web套接字错误:

代码语言:javascript
复制
index.window.js:24 WebSocket connection to 'wss://event-bridge.twilio.com/v1/wschannels?token=(here the token)&closeExistingSessions=false&clientVersion=0.5.2' failed: Error during WebSocket handshake: Unexpected response code: 403
createWebSocket @ index.window.js:24
(anonymous) @ index.window.js:24
index.js:1 WebSocket error occurred:  Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …}
console.<computed> @ index.js:1
<computed> @ index.window.js:17
webSocket.onerror @ index.window.js:24
error (async)
createWebSocket @ index.window.js:24
(anonymous) @ index.window.js:24
setTimeout (async)
webSocket.onclose @ index.window.js:24
index.window.js:17 Uncaught n {_errorData: {…}, name: "GATEWAY_CONNECTION_FAILED", message: "Could not connect to Twilio's servers."}
r.emit @ index.window.js:17
(anonymous) @ index.window.js:17
r.emit @ index.window.js:17
webSocket.onerror @ index.window.js:24
error (async)
createWebSocket @ index.window.js:24
(anonymous) @ index.window.js:24
setTimeout (async)
webSocket.onclose @ index.window.js:24
index.js:1 Uncaught Error: The error you provided does not contain a stack trace.
    at S (index.js:1)
    at V (index.js:1)
    at index.js:1
    at index.js:1
    at u (index.js:1)

同样的设置对任务路由器的cdn也很好,当我使用cdn和react twilio-taskrouter库之间的区别是web套接字url,而后者既不包括帐户SID,也不包括worker SID。

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-31 20:57:51

我明白了,我使用的是错误令牌,与twilio任务路由器库相比,我应该使用带有TaskRouter Grant访问令牌,而不是使用TaskRouter Worker capability token

代码语言:javascript
复制
class TwilioWorkerView(generics.RetrieveAPIView):
    serializer_class = TwilioTokenSerializer
    def get_object(self):
        current_user   = self.request.user
        username = current_user.username
        agent_sid      = current_user.agent.agent_sid
        access = AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY, TWILIO_API_SECRET)
        taskrouter_grant = TaskRouterGrant(workspace_sid=TWILIO_WORKSPACE_SID, worker_sid=agent_sid, role='worker')
        access.add_grant(taskrouter_grant)
        access.identity = username
        token = access.to_jwt()
        return token
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63655801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档