microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SendAdaptiveCards does not work with generic objects

MichaMican opened this issue · comments

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Version

4.18.1

Describe the bug

When calling the SendAdaptiveCard() function of a TeamsBotInstallation with a generic object of type object the internal serialization does not wokr properly and a empty adaptive card is send (see Screenshots)

To Reproduce

Steps to reproduce the behavior:

  1. Create a Notification bot (with toolkit).
  2. Replace the notification PostAsync controller code with the following:
[HttpPost]
public async Task<ActionResult> PostAsync([FromBody] object body, CancellationToken cancellationToken = default)
{
    var pageSize = 100;
    string continuationToken = null;
    do
    {
        var pagedInstallations = await _conversation.Notification.GetPagedInstallationsAsync(pageSize, continuationToken, cancellationToken);
        continuationToken = pagedInstallations.ContinuationToken;
        var installations = pagedInstallations.Data;
        var usersInstallation = pagedInstallations.Data.Where((i) => (i.ConversationReference.Conversation.TenantId == incidentHandlerRequest.TenantId && i.ConversationReference.User.AadObjectId == incidentHandlerRequest.AadUserId));

        foreach (var installation in usersInstallation)
        {
            var response = await installation.SendAdaptiveCard(body, cancellationToken);
        }
    } while (!string.IsNullOrEmpty(continuationToken));

    return Ok();
}
  1. Send a valid adaptive card to that endpoint (e.g. via Postman)
  2. See empty adaptive card in the chat

Expected behavior

The adaptivecard that was send to the endpoint should be send properly

Screenshots

image

Additional context

I worked arround the issue in a very weird way.
I basically converted the object to something that Newtonsoft.Json can deserialize properly

var jsonSerializerOptions = new System.Text.Json.JsonSerializerOptions(System.Text.Json.JsonSerializerDefaults.Web);
serializedAdaptiveCard = System.Text.Json.JsonSerializer.Serialize(body, jsonSerializerOptions); // System text json can serialize generic objects properly

then i Deserialize the serializedAdaptiveCard with Newtonsoft JSON

var response = await installation.SendAdaptiveCard(JsonConvert.DeserializeObject(serializedAdaptiveCard), cancellationToken);

This is an unbelievably dirty workarround. I am not sure if Newtonsoft.Json can be configured to serialize object properly but swapping to System.Text.Json should fix te issue

Tracking Status

Dotnet SDK TODO

  • PR
  • Merged

Javascript SDK TODO

  • PR
  • Merged

Python SDK TODO

  • PR
  • Merged

Java SDK TODO

  • PR
  • Merged

Samples TODO

  • PR
  • Merged

Docs TODO

  • PR
  • Merged

Tools TODO

  • PR
  • Merged

SendAdaptiveCard() isn't part of the Bot Framework SDK. It's tied to the Teams Toolkit and TeamsFx repo.

You might want to drop an issue over there. The TeamsFx folks should sort you out.

Let me know if you have any question.
Thanks.

Thanks for the clarification!

I have created a issue on TeamsFx side - Therefore i close this for now :)