Search code examples
microsoft-teamsmicrosoft-teams-js

How Do You Complete Teams Notification Following OAuth Login


I have a Teams app that was working for quite some time. It was on the shelf for a while and I'm in the process of bringing it back now. However, I now get an error after redirecting to do an OAuth login. In my page that is invoked following authentication, I have had this simple javascript that was working previously:

        var userId = document.getElementById("UserId").value;
        var tenantId = document.getElementById("TenantId").value;
        var isSuccess = document.getElementById("IsSuccessful").value;

        microsoftTeams.initialize(() => {

            if (isSuccess == "True")
                microsoftTeams.authentication.notifySuccess({ "UserID": userId }); 
            else
                microsoftTeams.authentication.notifyFailure();
        });

This used to return processing to the activity that called out to it - OnTeamsMessagingExtensionSubmitActionAsync. Now, I see the OnInvokeActivityAsync fire, and then the global error handler is invoked:

public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger)
        : base(configuration, logger)
    {
        OnTurnError = async (turnContext, exception) =>
        {
            // Log any leaked exception from the application.
            logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
            
            // Send a message to the user
            await turnContext.SendActivityAsync("The bot encountered an error or bug.");
            //await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");

            // Send a trace activity, which will be displayed in the Bot Framework Emulator
            await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
        };
    }

The exception.Message is "Error reading string. Unexpected token: StartObject. Path 'state'." When I look at the Value of the turnContext.Activity, it is this chunk of json, obviously starting with a "state" element:

{{
  "state": {
    "UserID": "12345678-362b-4942-a5b0-cda1198c8223"
  },
  "commandId": "exportContent",
  "commandContext": "compose",
  "requestId": "db113a08fde34abbb1d4b5da8e5f385c",
  "context": {
    "theme": "default"
  },
  "data": {
    "TenantId": "12345678-92aa-4476-9dcf-3953b4efcec2",
    "TeamId": "19:[email protected]",
    "ChannelId": "19:[email protected]",
    "MessageId": "1234567866366",
    "Op": "channel"
  }
}}

Obviously the json is being generated by the Microsoft Teams javascript SDK so I'm not sure how to resolve this problem. The javascript SDK version I'm using is 1.5.2. In my C# project I'm using the Nuget package for Microsoft.Bot.Builder.Integration.AspNet.Core, version 4.19.3.

Any suggestions on how to unwind this? It feels kind of like being stuck since I'm relying upon the Teams JS SDK to do the work here.


Solution

  • I was able to fix this by downgrading the version of Microsoft.Bot.Builder.Integration.AspNet.Core. After trying multiple versions, when I tried 4.10.0 everything started working again.