自适应卡未发送给用户
Adaptive card not sending to user
我目前正在尝试向 Microsoft Teams 中的最终用户输出自适应卡片,但每当我尝试 post 这张卡片时,它 returns 就会出错。最重要的是,我所做的任何小调整,它也是 returns 一个错误所以而不是 post 一系列错误我相信我在做的事情一定是根本错误的,所以我正在寻求澄清在这个领域,因为我是新手。
所以这是场景,
i) 最终用户输入回复并点击发送,将他们的消息发送到下面的函数,
private async Task CallCreatePart2(IDialogContext context, IAwaitable<object> email)
{
var callemail = await email as string;
...
var cardJson = GetCardJson();
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
AdaptiveCard card = result.Card;
IMessageActivity message = Activity.CreateMessageActivity();
message.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
Microsoft.Bot.Connector.Attachment plAttachment = new Microsoft.Bot.Connector.Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
message.Attachments.Add(plAttachment);
message.ReplyToId = context.Activity.Id;
await context.PostAsync(message);
}
所以这将抛出以下内容,
System.NullReferenceException: 'Object reference not set to an instance of an object.'
我的适配卡如下,
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "My Notification"
},
{
"type": "TextBlock",
"weight": "Bolder",
"text": "New ticket",
"wrap": true
},
{
"type": "ActionCard",
"name": "Visit",
"actions": [
{
"type": "OpenUri",
"name": "Visit",
"targets": [
{
"os": "default",
"uri": "https://www.microsoft.com"
}
]
}
]
},
{
"type": "FactSet",
"facts": [
{
"title": "Test1",
"value": "hello"
},
{
"title": "Test2:",
"value": "test"
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
这里设置了ReplyToId值,因为它之前抱怨它是null。事情是在这之后我想输出一个 PromptDialog.Confirm 对象,所以用户会回复这个。自适应卡片纯粹是为了输出一张漂亮的卡片,其中 json 是使用所提供的外部来源的信息创建的。因此,话虽这么说,我想要实现的目标有什么根本性的错误吗?我是否不允许使用 context.PostAsync() 向最终用户 post 自适应卡?
因此需要在消息上设置 ReplyToId(),这是我在做一些研究后收集的,但是一旦通过这个障碍,我就会不断收到以下错误,
"Microsoft.Bot.Connector.ErrorResponseException: 'Operation returned an invalid status code 'BadRequest''".
我看到您正在使用 Activity.CreateMessageActivity(),当您使用此方法时,它只会创建 新消息而不会收到消息语境。因此,对话详细信息为空。因此您收到异常。您需要使用 activity.CreateReply() 来获取正确的数据以及发送消息所需的对话详细信息。能否请您检查以下代码:
private async Task CallCreatePart2(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var activity = await argument as Activity;
var cardJson = GetCardJson();
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
AdaptiveCard card = result.Card;
IMessageActivity message = activity.CreateReply();
message.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
Microsoft.Bot.Connector.Attachment plAttachment = new Microsoft.Bot.Connector.Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
message.Attachments.Add(plAttachment);
message.ReplyToId = context.Activity.Id;
await context.PostAsync(message);
}
事实证明,“BadRequest”错误实际上完全归因于传递到 AdaptiveCard 中的数据。
string callCardJson = CreateJsonPayloadAsDataForAdapativeCard();
var myInfo = JsonConvert.DeserializeObject<JsonPayLoad>(callCardJson);
var templateJson = await WebhookController.GetCardText("my_template");
#if DEBUG
templateJson = await WebhookController.GetCardText("my_template_test");
#endif
AdaptiveCardTemplate template = new AdaptiveCardTemplate(templateJson);
var myData = new
{
title = myInfo.title,
name = myInfo.name,
token = myInfo.token,
reference = myInfo.reference,
info = my.info
};
string cardJson = template.Expand(myData);
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
card = result.Card;
所以 AdaptiveCard 在多种情况下被用作通用卡片,但并非所有情况下都需要所有数据字段,所以最初我将空白值传递到 json 数据对象中,例如作为上面的标记和参考。当它在上面被反序列化然后传递到 AdapativeCard 时,它导致了“BadRequest”错误。我通过删除 json 数据对象中的空白条目解决了这个问题!
我目前正在尝试向 Microsoft Teams 中的最终用户输出自适应卡片,但每当我尝试 post 这张卡片时,它 returns 就会出错。最重要的是,我所做的任何小调整,它也是 returns 一个错误所以而不是 post 一系列错误我相信我在做的事情一定是根本错误的,所以我正在寻求澄清在这个领域,因为我是新手。 所以这是场景, i) 最终用户输入回复并点击发送,将他们的消息发送到下面的函数,
private async Task CallCreatePart2(IDialogContext context, IAwaitable<object> email)
{
var callemail = await email as string;
...
var cardJson = GetCardJson();
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
AdaptiveCard card = result.Card;
IMessageActivity message = Activity.CreateMessageActivity();
message.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
Microsoft.Bot.Connector.Attachment plAttachment = new Microsoft.Bot.Connector.Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
message.Attachments.Add(plAttachment);
message.ReplyToId = context.Activity.Id;
await context.PostAsync(message);
}
所以这将抛出以下内容,
System.NullReferenceException: 'Object reference not set to an instance of an object.'
我的适配卡如下,
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "My Notification"
},
{
"type": "TextBlock",
"weight": "Bolder",
"text": "New ticket",
"wrap": true
},
{
"type": "ActionCard",
"name": "Visit",
"actions": [
{
"type": "OpenUri",
"name": "Visit",
"targets": [
{
"os": "default",
"uri": "https://www.microsoft.com"
}
]
}
]
},
{
"type": "FactSet",
"facts": [
{
"title": "Test1",
"value": "hello"
},
{
"title": "Test2:",
"value": "test"
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
这里设置了ReplyToId值,因为它之前抱怨它是null。事情是在这之后我想输出一个 PromptDialog.Confirm 对象,所以用户会回复这个。自适应卡片纯粹是为了输出一张漂亮的卡片,其中 json 是使用所提供的外部来源的信息创建的。因此,话虽这么说,我想要实现的目标有什么根本性的错误吗?我是否不允许使用 context.PostAsync() 向最终用户 post 自适应卡?
因此需要在消息上设置 ReplyToId(),这是我在做一些研究后收集的,但是一旦通过这个障碍,我就会不断收到以下错误,
"Microsoft.Bot.Connector.ErrorResponseException: 'Operation returned an invalid status code 'BadRequest''".
我看到您正在使用 Activity.CreateMessageActivity(),当您使用此方法时,它只会创建 新消息而不会收到消息语境。因此,对话详细信息为空。因此您收到异常。您需要使用 activity.CreateReply() 来获取正确的数据以及发送消息所需的对话详细信息。能否请您检查以下代码:
private async Task CallCreatePart2(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var activity = await argument as Activity;
var cardJson = GetCardJson();
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
AdaptiveCard card = result.Card;
IMessageActivity message = activity.CreateReply();
message.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
Microsoft.Bot.Connector.Attachment plAttachment = new Microsoft.Bot.Connector.Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
message.Attachments.Add(plAttachment);
message.ReplyToId = context.Activity.Id;
await context.PostAsync(message);
}
事实证明,“BadRequest”错误实际上完全归因于传递到 AdaptiveCard 中的数据。
string callCardJson = CreateJsonPayloadAsDataForAdapativeCard();
var myInfo = JsonConvert.DeserializeObject<JsonPayLoad>(callCardJson);
var templateJson = await WebhookController.GetCardText("my_template");
#if DEBUG
templateJson = await WebhookController.GetCardText("my_template_test");
#endif
AdaptiveCardTemplate template = new AdaptiveCardTemplate(templateJson);
var myData = new
{
title = myInfo.title,
name = myInfo.name,
token = myInfo.token,
reference = myInfo.reference,
info = my.info
};
string cardJson = template.Expand(myData);
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
card = result.Card;
所以 AdaptiveCard 在多种情况下被用作通用卡片,但并非所有情况下都需要所有数据字段,所以最初我将空白值传递到 json 数据对象中,例如作为上面的标记和参考。当它在上面被反序列化然后传递到 AdapativeCard 时,它导致了“BadRequest”错误。我通过删除 json 数据对象中的空白条目解决了这个问题!