首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用此函数创建的Twilio消息将发出会话错误,无法回复。

使用此函数创建的Twilio消息将发出会话错误,无法回复。
EN

Stack Overflow用户
提问于 2021-04-24 15:32:29
回答 1查看 302关注 0票数 1

我已经用这个sms插件从flex创建了一个出站短信。我可以成功地发送出站短信,并创建一个会话和聊天窗口进行双向聊天。当从这个聊天窗口回复时,它创建了一个对给定的web钩子的非会话调用。

在发送短信时,以下功能中缺少什么与会话有关?

问题2:两个节点中是否有通道API可用?

启动聊天的代码如下所示,详细代码链接

代码语言:javascript
复制
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;
   }
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-26 00:19:18

两位开发人员在这里传道。

Flex通道API现在可在Twilio节点库中使用。。请确保将最新的twilio包安装到您的package.json上,您可以如下所示:

代码语言:javascript
复制
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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67244498

复制
相关文章

相似问题

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