我已经用这个sms插件从flex创建了一个出站短信。我可以成功地发送出站短信,并创建一个会话和聊天窗口进行双向聊天。当从这个聊天窗口回复时,它创建了一个对给定的web钩子的非会话调用。
在发送短信时,以下功能中缺少什么与会话有关?
问题2:两个节点中是否有通道API可用?
启动聊天的代码如下所示,详细代码链接
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;
}
}发布于 2021-04-26 00:19:18
两位开发人员在这里传道。
Flex通道API现在可在Twilio节点库中使用。。请确保将最新的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_SID和FLEX_FLOW_SID设置了正确的Sids。
https://stackoverflow.com/questions/67244498
复制相似问题