使用此函数创建的 Twilio 消息给出了会话错误并且无法回复

Twilio message created with this function gives out of session error and can not reply back

我已经使用此 sms plugin 从 flex 创建了出站短信。 我可以成功发送出站短信,并 flex 创建会话和聊天 window 进行双向聊天。从此聊天中回复时 window 它创建了对给定 webhook 的会话外调用。

以下与发送短信时会话相关的函数缺少什么?

问题 2:通道 API 在 twilio-node 中可用吗?

发起聊天的代码如下,link for detail code

const axios = require('axios');
exports.handler = async function (context, event, callback) {
const response = new Twilio.Response();


const authed = await validateToken(event.Token, context.ACCOUNT_SID, context.AUTH_TOKEN); //Not showing validateToken function here,can be found in plugin link above

const client = context.getTwilioClient();
const to = +13...4724 // outbound "To" number to send sms
const from = +128.....834 // Twilio Number i.e "From" number 

 const channelArgs = {
 
 FlexFlowSid: context.FLEX_FLOW_SID,
 Identity: event.To,
 Target: event.To,
 ChatUserFriendlyName: event.ToFriendlyName || event.To,
 ChatFriendlyName: event.ChatFriendlyName || `${event.To} chat`,
 PreEngagementData: JSON.stringify({ targetWorker: authed.data.identity })
  };

  const channelResponse = await createChannel(channelArgs, context.ACCOUNT_SID,context.AUTH_TOKEN);

client.messages.create({
  body: event.Message,
  to: to,
  from: from
 })

   .then(() => {
    console.log('adding message to', channelResponse.data.sid);
   
    client.chat.services(context.CHAT_SERVICE_SID)
   .channels(channelResponse.data.sid)
    .messages
   .create({
    body: event.Message,
    from: from
    }).then(() => callback(null, response));
}).catch(err => {
  console.error(err);
  callback(err);
});
 }

  

      async function createChannel(channelArgs, accountSid, authToken) {
       const queryString = Object.keys(channelArgs)
       .map(k => `${k}=${encodeURIComponent(channelArgs[k])}`)
       .join('&');

     console.log('sending', queryString);

  try {
     // Channels API not in twilio-node yet... so axios
    return await axios.post(
     'https://flex-api.twilio.com/v1/Channels',
      queryString,
      { auth: { username: accountSid, password: authToken } }
     )
    } catch (e) {
     console.error(e.response.data);
       throw e;
   }
 }

 

这里是 Twilio 开发人员布道者。

Flex Channels API is available in the Twilio Node library now。确保你安装了最新的 twilio 包到你的 package.json 并且你可以这样称呼它:

client.flexApi.channel
              .create({
                 flexFlowSid: context.FLEX_FLOW_SID,
                 identity: event.To,
                 chatUserFriendlyName: event.ToFriendlyName || event.To,
                 chatFriendlyName: event.ChatFriendlyName || `${event.To} chat`,
                 preEngagementData: JSON.stringify({ targetWorker: authed.data.identity })
               })
              .then(channel => console.log(channel.sid));

也许尝试更新到最新的 Twilio 节点包,更新以使用上面的代码,看看它是否仍然有效。此外,请仔细检查您在 Twilio 函数的环境变量中为 CHAT_SERVICE_SIDFLEX_FLOW_SID 设置了正确的 Sids。