2

I am building a bot using the Microsoft C# bot framework v4. When the bot is first added to a team, I want it to proactively 1:1 message each member of the team, introducing itself. I know it should be possible to start a chat with a user, even if the user has not previously interacted with the bot. This process is described in the documentation:

When using proactive messaging to send a welcome message to a user you must keep in mind that for most people receiving the message they will have no context for why they are receiving it. This is also the first time they will have interacted with your app; it is your opportunity to create a good first impression. The best welcome messages will include:

  • Why are they receiving this message. It should be very clear to the user why they are receiving the message. If your bot was installed in a channel and you sent a welcome message to all users, let them know what channel it was installed in and potentially who installed it.

To me this indicates that I should be able to initiate a chat message with each member of the channel, but I cannot get the bot to message anyone in the channel other than me. In the TeamsConversationBot sample provided by Microsoft, there is a MessageAllMembers() method that seems to be what I am looking for, however when I call it in my code, and add the bot to a team in Teams, the bot only messages me.

Here is the MessageAllMembers() code I am using:

private async Task MessageAllMembersAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
    {
        var Id = turnContext.Activity.TeamsGetChannelId();
        var serviceUrl = turnContext.Activity.ServiceUrl;
        var credentials = new MicrosoftAppCredentials(_configuration["MicrosoftAppId"], _configuration["MicrosoftAppPassword"]);
        ConversationReference conversationReference = null;

        var members = await TeamsInfo.GetMembersAsync(turnContext, cancellationToken);

        foreach (var teamMember in members)
        {
            if (teamMember.Id != turnContext.Activity.Recipient.Id) {
                var proactiveMessage = MessageFactory.Text($"Hello {teamMember.GivenName} {teamMember.Surname}. I'm a Teams conversation bot.");

                var conversationParameters = new ConversationParameters
                {
                    IsGroup = false,
                    Bot = turnContext.Activity.Recipient,
                    Members = new ChannelAccount[] { teamMember },
                    TenantId = turnContext.Activity.Conversation.TenantId,
                };

                await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
                    Id,
                    serviceUrl,
                    credentials,
                    conversationParameters,
                    async (t1, c1) =>
                    {
                        conversationReference = t1.Activity.GetConversationReference();
                        await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
                            _configuration["MicrosoftAppId"],
                            conversationReference,
                            async (t2, c2) =>
                            {
                                await t2.SendActivityAsync(proactiveMessage, c2);
                            },
                            cancellationToken);
                    },
                    cancellationToken);
            }
        }

        await turnContext.SendActivityAsync(MessageFactory.Text("All messages have been sent."), cancellationToken);
    }

It is not throwing any exceptions, it's just not doing what I expect.

Apex
  • 21
  • 2
  • Welcome to stack overflow! Sorry for asking the obvious, but I'm assuming you've installed this bot into a Team, and it's not just a 1-1 chat directly with the Bot? Aside from that, have you tried debugging and seeing how many items come back in the call to GetMembersAsync? – Hilton Giesenow Jan 09 '20 at 17:06
  • I am installing it in the team, and not 1-1 chat. When I debug it looks like it is only picking up my name in the channel, even though there are other members. I will try to get that fixed first and see if it solves the whole problem. Thank you for the tip! – Apex Jan 09 '20 at 18:05
  • If it helps, you can also try another approach to getting the conversation members. I posted about it here: https://stackoverflow.com/questions/59598946/find-out-both-chat-participants-in-ms-teams-tab-javascript/59599581#59599581 . In that example it was in the OnMembersAddedAsync, but you can put it in OnTurnAsync as well – Hilton Giesenow Jan 09 '20 at 20:55
  • Please refer to the Send Proactive Messages [documentation](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/get-teams-context?tabs=dotnet) and https://stackoverflow.com/questions/55517906/how-do-i-send-a-11-welcome-message/55525014#55525014 to get started with. – ranusharao Jan 15 '20 at 23:33

0 Answers0