microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.45k stars 2.44k forks source link

SendAdaptiveCards does not work with generic objects #6629

Closed MichaMican closed 6 months ago

MichaMican commented 6 months ago

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();
    }
  3. Send a valid adaptive card to that endpoint (e.g. via Postman)
  4. 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]()

Javascript SDK [TODO]()

Python SDK [TODO]()

Java SDK [TODO]()

Samples [TODO]()

Docs [TODO]()

Tools [TODO]()

InfinytRam commented 6 months ago

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.

MichaMican commented 6 months ago

Thanks for the clarification!

MichaMican commented 6 months ago

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