自适应卡未发送给用户

时间:2020-11-06 16:05:31

标签: c# botframework microsoft-teams adaptive-cards

我目前正在尝试向Microsoft Teams中的最终用户输出自适应卡,但是每当我尝试发布此卡时,它都会返回错误。最重要的是,我所做的任何小调整都会返回一个错误,因此我发布了一系列错误,我相信我所做的肯定有一些根本性的错误,因此我正在对此领域进行澄清,因为我是新来的它。 所以这是场景 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的漂亮卡片,该json是使用提供的外部来源的信息创建的。如此说来,我要实现的目标有根本上的错误吗?我是否不允许使用context.PostAsync()将自适应卡发布给最终用户?

因此,需要在进行一些研究后收集的消息上设置ReplyToId(),但是一旦克服了这一障碍,我就会不断遇到以下错误,

"Microsoft.Bot.Connector.ErrorResponseException: 'Operation returned an invalid status code 'BadRequest''".

2 个答案:

答案 0 :(得分:0)

我可以看到您正在使用 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);
    }

答案 1 :(得分:0)

事实证明,“ 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数据对象中的空白条目解决了该问题!