如何使用 Microsoft Bot Framework 和 Facebook Messenger 发送富文本卡片?
How to send rich text card using Microsoft Bot Framework and Facebook Messenger?
我正在使用 Facebook Messenger Platform 和 Microsoft Bot Framework 创建一个发送富文本附件的机器人。
到目前为止,我已尝试在我的意图文件中使用通用模板:
messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "First card",
"subtitle": "Element #1 of an hscroll",
"image_url": "http://messengerdemo.parseapp.com/img/rift.png",
"buttons": [{
"type": "web_url",
"url": "https://www.messenger.com",
"title": "web url"
}, {
"type": "postback",
"title": "Postback",
"payload": "Payload for first element in a generic bubble",
}],
}, {
"title": "Second card",
"subtitle": "Element #2 of an hscroll",
"buttons": [{
"type": "postback",
"title": "Postback",
}],
}]
}
}
}
return resolve([toCard(messageData)])
}
})
})
}
const toText = message => {return {type: 'text', content: message }}
const toCard = message => {return {type: 'attachment', content: message}}
我的 index.js 中还有一个 sendMessageByType 函数,它应该根据从各种意图接收到的响应类型发送消息。
const sendMessageByType = {
text: (session, elem) => session.send(elem.content),
attachment: (session, elem) => session.send(elem.content)
}
上面的函数调用如下:
.then(res => { res.forEach( (message) => sendMessageByType[message.type](session, message)) })
我曾尝试将类型更改为文本、卡片和附件,但 none 成功了。应该使用什么类型以及如何声明以便发送卡片?
因此,经过大量试验和错误并阅读此处的文档:https://docs.botframework.com/en-us/node/builder/chat/session/#sending-message,我弄清楚了如何进行这项工作。本质上,使用的类型是附件,发送富卡的正确方式如下:
attachment: (session, elem) => session.send(new builder.Message(session).sourceEvent(elem.content))
我正在使用 Facebook Messenger Platform 和 Microsoft Bot Framework 创建一个发送富文本附件的机器人。
到目前为止,我已尝试在我的意图文件中使用通用模板:
messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "First card",
"subtitle": "Element #1 of an hscroll",
"image_url": "http://messengerdemo.parseapp.com/img/rift.png",
"buttons": [{
"type": "web_url",
"url": "https://www.messenger.com",
"title": "web url"
}, {
"type": "postback",
"title": "Postback",
"payload": "Payload for first element in a generic bubble",
}],
}, {
"title": "Second card",
"subtitle": "Element #2 of an hscroll",
"buttons": [{
"type": "postback",
"title": "Postback",
}],
}]
}
}
}
return resolve([toCard(messageData)])
}
})
})
}
const toText = message => {return {type: 'text', content: message }}
const toCard = message => {return {type: 'attachment', content: message}}
我的 index.js 中还有一个 sendMessageByType 函数,它应该根据从各种意图接收到的响应类型发送消息。
const sendMessageByType = {
text: (session, elem) => session.send(elem.content),
attachment: (session, elem) => session.send(elem.content)
}
上面的函数调用如下:
.then(res => { res.forEach( (message) => sendMessageByType[message.type](session, message)) })
我曾尝试将类型更改为文本、卡片和附件,但 none 成功了。应该使用什么类型以及如何声明以便发送卡片?
因此,经过大量试验和错误并阅读此处的文档:https://docs.botframework.com/en-us/node/builder/chat/session/#sending-message,我弄清楚了如何进行这项工作。本质上,使用的类型是附件,发送富卡的正确方式如下:
attachment: (session, elem) => session.send(new builder.Message(session).sourceEvent(elem.content))