繁体   English   中英

如何使用Microsoft Bot Framework和Facebook Messenger发送富文本卡?

[英]How to send rich text card using Microsoft Bot Framework and Facebook Messenger?

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

我尝试将类型更改为文本,卡片和附件,但这些都不起作用。 什么是正确使用的类型以及如何声明以发送卡?

因此,经过大量的试验和错误并在这里阅读文档: https : //docs.botframework.com/en-us/node/builder/chat/session/#sending-message ,我弄清楚了如何使这项工作有效。 本质上,要使用的类型是附件,正确发送富卡的方式如下:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM