使用node.js启动twilio Flex聊天

时间:2019-03-13 00:53:34

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

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

我正在创建频道,添加成员,使用该成员在该频道上创建消息。

flex仪表板在任务列表中显示传入的聊天请求,我可以回答该聊天请求,但是我保存到频道的消息均未显示。

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

我比较了使用twilio-flex-webchat.min.js Web库进行聊天对话的任务,频道,预订,成员和消息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',
        });
})();

谢谢

1 个答案:

答案 0 :(得分:0)

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

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