【问题标题】:How to send rich text card using Microsoft Bot Framework and Facebook Messenger?如何使用 Microsoft Bot Framework 和 Facebook Messenger 发送富文本卡片?
【发布时间】:2023-03-21 02:53:01
【问题描述】:

我正在使用 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)) })

我已尝试将类型更改为文本、卡片和附件,但均无效。什么是正确的使用类型以及如何声明它以便发送卡?

【问题讨论】:

    标签: node.js botframework facebook-messenger botconnector


    【解决方案1】:

    所以经过大量试验和错误并阅读此处的文档:https://docs.botframework.com/en-us/node/builder/chat/session/#sending-message,我想出了如何使这项工作。本质上,使用的类型是附件,发送富卡的正确方法如下:

    attachment: (session, elem) => session.send(new builder.Message(session).sourceEvent(elem.content))
    

    【讨论】:

    • 感谢您回答您自己的问题 :) 这也帮助了我!
    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 2016-08-30
    • 2017-12-05
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多