Bot Composer 和 BotBuilder FacebookAdapter 导致 "Object reference not set to an instance of an object"
Bot Composer and BotBuilder FacebookAdapter resulting in "Object reference not set to an instance of an object"
我将 MS Bot Composer 与 BotBuilder Facebook Adapter 结合使用,以便 post 我的机器人遵循 Facebook Workplace 的“新”规则 - 重要的是要记住,我已经尝试直接在Facebook Messenge,同样的情况。
集成和连接工作正常,现在的问题是了解如何 post 除了文本之外的任何内容。
下面是尝试获取正确模板的示例
# channelData
-```{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"What do you want to do next?",
"buttons":[
{
"type":"web_url",
"url":"https://www.messenger.com",
"title":"Visit Messenger"
}
]
}
}
}```
然后,机器人的回答:
如果您使用频道数据路由,Facebook 适配器希望您提供完整的 FacebookMessage
对象作为频道数据。这比您提供的 JSON 高一级,因此它看起来像这样:
{
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"What do you want to do next?",
"buttons":[
{
"type":"web_url",
"url":"https://www.messenger.com",
"title":"Visit Messenger"
}
]
}
}
}
}
但是,这行不通,因为它缺少 FacebookMessage
需要的其他属性,例如 sender
。与其尝试填充完整的 Facebook 消息,不如使用附件路径而不是频道数据路径可能更容易。您可以在 Facebook adapter sample:
中查看如何执行此操作
private static Attachment CreateTemplateAttachment(string filePath)
{
var templateAttachmentJson = File.ReadAllText(filePath);
var templateAttachment = new Attachment()
{
ContentType = "template",
Content = JsonConvert.DeserializeObject(templateAttachmentJson),
};
return templateAttachment;
}
在 Composer 中,您可以设置 activity 的附件而不是其通道数据。只需将内容类型设置为“模板”,将内容设置为您现在拥有的附件的有效负载,使用 Facebook adapter sample's resources 作为指南:
# attachment
- ```
{
"contentType": "template",
"content": {
"template_type": "button",
"text": "What do you want to do next?",
"buttons": [
{
"type": "web_url",
"url": "https://www.messenger.com",
"title": "Visit Messenger"
}
]
}
}
```
您可以通过查看 FacebookHelper
class.
了解 Facebook 适配器如何处理频道数据和附件。
我将 MS Bot Composer 与 BotBuilder Facebook Adapter 结合使用,以便 post 我的机器人遵循 Facebook Workplace 的“新”规则 - 重要的是要记住,我已经尝试直接在Facebook Messenge,同样的情况。
集成和连接工作正常,现在的问题是了解如何 post 除了文本之外的任何内容。
下面是尝试获取正确模板的示例
# channelData
-```{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"What do you want to do next?",
"buttons":[
{
"type":"web_url",
"url":"https://www.messenger.com",
"title":"Visit Messenger"
}
]
}
}
}```
然后,机器人的回答:
如果您使用频道数据路由,Facebook 适配器希望您提供完整的 FacebookMessage
对象作为频道数据。这比您提供的 JSON 高一级,因此它看起来像这样:
{
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"What do you want to do next?",
"buttons":[
{
"type":"web_url",
"url":"https://www.messenger.com",
"title":"Visit Messenger"
}
]
}
}
}
}
但是,这行不通,因为它缺少 FacebookMessage
需要的其他属性,例如 sender
。与其尝试填充完整的 Facebook 消息,不如使用附件路径而不是频道数据路径可能更容易。您可以在 Facebook adapter sample:
private static Attachment CreateTemplateAttachment(string filePath) { var templateAttachmentJson = File.ReadAllText(filePath); var templateAttachment = new Attachment() { ContentType = "template", Content = JsonConvert.DeserializeObject(templateAttachmentJson), }; return templateAttachment; }
在 Composer 中,您可以设置 activity 的附件而不是其通道数据。只需将内容类型设置为“模板”,将内容设置为您现在拥有的附件的有效负载,使用 Facebook adapter sample's resources 作为指南:
# attachment
- ```
{
"contentType": "template",
"content": {
"template_type": "button",
"text": "What do you want to do next?",
"buttons": [
{
"type": "web_url",
"url": "https://www.messenger.com",
"title": "Visit Messenger"
}
]
}
}
```
您可以通过查看 FacebookHelper
class.