node.js - 使用 Node.js 启动 twilio Flex 聊天

标签 node.js twilio twilio-api twilio-programmable-chat twilio-flex

我很难让 twilio flex 显示使用 Node.js API 创建的消息。

我正在创建一个 channel ,添加一个成员,并使用该成员在 channel 上创建一条消息。

Flex 仪表板在任务列表中显示传入的聊天请求,我可以回复聊天请求,但不会显示我保存到 channel 的任何消息。

有趣的是,如果我使用 twilio-flex-webchat.min.js 脚本并从网页发起聊天,然后获取该对话的 ChannelSid(使用 https://chat.twilio.com/v2/Services/ISXXXX/Channels ),我可以使用 API 为该 channel 创建消息,它们会显示在 Flex 仪表板上。但我需要所有这些才能通过 node.js 工作。

我比较了使用 twilio-flex-webchat.min.js Web 库进行聊天对话的任务、 channel 、预订、成员和消息 twilio 对象以及由 node.js 代码创建的对象。我找不到任何显着的差异。

有人有任何见解吗?

这是我的代码。

const accountSid = 'ACXXXXXXXXXX';
const authToken = 'XXXXXXXXXXXXXXXX';
const workspaceSid = 'WSXXXXXXXXXXXXXXXx';
const workFlowSid = 'WWXXXXXXXXXXXXXXXXXXXXXX';
const serviceSid = 'ISXXXXXXXXXXXXXXXXXXXXXX';


const client = require('twilio')(accountSid, authToken);

(async () => {

    //create channel
    let channel = await client.chat.services(serviceSid)
        .channels
        .create({
            attributes: JSON.stringify({
                status: "ACTIVE",
                from: "Some Person",
                channel_type: "web"
            }),
            workflowSid: workFlowSid,
            taskChannel: 'chat',
            friendlyName: 'Flex WebChat',
            type: 'private'
        });

    //create a member in this channel
    let member = await client.chat.services(serviceSid)
        .channels(channel.sid)
        .members
        .create({ identity: 'WEB_CLIENT' });

    //post a message to this channel from the member
    let message = await client.chat.services(serviceSid)
        .channels(channel.sid)
        .messages.create({ body: 'This is a test message', to: channel.sid, from: 'WEB_CLIENT' });

    //create a task for my programable chat channel and associate the channel sid for my current conversation
    let task = await client.taskrouter.workspaces(workspaceSid)
        .tasks
        .create({
            attributes: JSON.stringify({
                channelSid: channel.sid,
                channelType: "web"
            }),
            workflowSid: workFlowSid,
            taskChannel: 'chat',
        });
})();

谢谢

最佳答案

在添加消息和创建任务之前,您需要创建代理 session ,将成员添加为参与者,然后更新 channel 属性:

    {
      "status": "ACTIVE",
      "forwarding": true,
      "twilioNumber": firstParticipant.proxyIdentifier,
      "serviceNumber": someIdentity,
      "from": from,
      "channel_type": "sms",
      "proxySession": session.sid
    }

关于node.js - 使用 Node.js 启动 twilio Flex 聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55132796/

相关文章:

twilio - 如何使用 Twilio Chat Swift SDK 获得 Twilio Chat 成员的邀请

ios - Twilio 错误 - 52134 无效的 APNs 设备 token

twilio - 如何在 twilio 视频通话中静音/取消静音暂停/取消暂停音频和视频

javascript - 检查单元测试中是否调用了函数

node.js - 使用 babel-node 时如何将选项传递给 Node

javascript - 在尝试为另一个应用程序设置新设备时不释放 Twilio.Device 实例

javascript - 多个代理可以同时使用唯一的 Twilio 号码吗?

node.js - 安装 npm body-parser 时出错

node.js - 如何在没有 redis 的情况下扩展 socket.io

coldfusion - 如何通过 CFHTTP 将此 API 调用发送到 Twilio Flow