Search code examples
c#.netbotframeworkmicrosoft-teams

Microsoft teams bot - could not parse tenant id


I'm working on a bot for MS Teams, and running into an issue. When trying to initiate a conversation from the bot, I get this error:

Microsoft.Rest.HttpOperationException: Could not parse tenant id

I haven't been able to find anywhere in the docs that mentions a required Tenant ID, and I never set one up in the application. How can I specify this, or is the root cause something else?

Below is the code I am using that returns the error (strings obfuscated).

private ConversationResourceResponse GetConversation(IActivity activity)
    {
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        var userAccount = new ChannelAccount("[email protected]");
        var botAccount = new ChannelAccount("@botHandle", "botName");

        var conversationId = connector.Conversations.CreateDirectConversation(botAccount, userAccount);
        return conversationId;
    }

Thank you!


Solution

  • There is a special behaviour in MS Teams when you want to create a conversation, so you have to use a specific method provided by MS Teams NuGet package:

    // Create or get existing chat conversation with user
    var response = client.Conversations.CreateOrGetDirectConversation(activity.Recipient, activity.From, activity.GetTenantId());
    

    You can see that the method has the tenantId in parameter.

    The NuGet package is called Microsoft.Bot.Connector.Teams and is available here.

    More details on the MS Teams documentation (it's not detailed on Bot framework side): https://learn.microsoft.com/en-us/microsoftteams/platform/scenarios/bots-personal-conversations#starting-a-11-conversation