I have a Bot Framework V3 SDK implementation (working fine) that can initiate a 1:1 private chat with any user in a specific Team using a Microsoft Teams Bot (installed on the same team). I am having problems trying to migrate it to the V4 SDK.
I read on various articles and other questions that it's not possible to do it unless the user contacts the the bot first (to avoid spamming to users), but this is not true with the V3 version and not an option for the functionality that I need.
The original solution uses the "CreateOrGetDirectConversation" extension method from the Microsoft.Bot.Connector.Teams assembly, but it's not available in the V4 version of the assembly.
I tried using the CreateDirectConversation/CreateDirectConversationAsync methods but with no success (they always result in "Bad Request" errors).
This is the code actually working using the V3 libraries:
// Create 1:1 conversation
var conversation = connectorClient.Conversations.CreateOrGetDirectConversation(botAccount, user, tenantId);
// Send message to the user
var message = Activity.CreateMessageActivity();
message.Type = ActivityTypes.Message;
message.Text = "My message";
connectorClient.Conversations.SendToConversation((Activity)message, conversation.Id);
And I am finding it very hard to migrate. Am I missing something?
Per the docs, Proactive messaging for bots:
Note: The Microsoft.Bot.Builder.Teams extension is still in Prerelease for V4, which is why samples and code are kind of hard to find for it.
Adding the Middleware
In
Startup.cs:Prepping Your Bot
In your main
<YourBot>.cs:Sending the Message
Note: If you need to get user ID's, you can use:
Also, I didn't need this in my testing, but if you get 401 errors, you may need to trust the Teams ServiceUrl:
Resources