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

时间:2016-12-18 18:58:30

标签: node.js botframework facebook-messenger botconnector

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

我尝试将类型更改为文本,卡片和附件,但这些都没有效果。什么是正确的类型以及如何声明它以便发送卡?

1 个答案:

答案 0 :(得分: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))