0
votes

I've got an issue with my proactive bot.

My problem is that when I add the bot to a team for the first time it fires and event which creates the team and General channel.

My issue is that it doesn't create a chat reference for other existing channels prior to adding the bot. If I add a new channel to the team it fires an channelCreated event and a new conversation reference is created. I have overridden OnConversationUpdateActivityAsync and as mentioned it works when first adding the bot and then if new channels are added.

Anyone know what is best practice when adding the bot for the first time? Should I just create conversations references for all existing channels when the bot is added the first time?

https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/send-proactive-messages?tabs=dotnet

https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/16.proactive-messages

1

1 Answers

0
votes

I'm assuming you just want to be able to have the bot message all channels. If that's not the case, let me know and I'll edit the answer.

I recommend taking a look at the Teams - Start New Thread in Channel Sample.

Basically, you need to have the bot get a list of Channels:

public class MyBot : TeamsActivityHandler
{
    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        IEnumerable<ChannelInfo> channels = await TeamsInfo.GetTeamChannelsAsync(turnContext, turnContext.Activity.TeamsGetTeamInfo().Id, cancellationToken);

        await turnContext.SendActivityAsync($"The channel count is: {channels.Count()}");
    }
}

And then you can send a Proactive Message to the channel by creating the conversation and then continuing it:

var conversationParameters = new ConversationParameters
{
    IsGroup = true,
    ChannelData = new { channel = new { id = teamsChannelId } },
    Activity = (Activity)message,
};

ConversationReference conversationReference = null;

await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
    teamsChannelId,
    serviceUrl,
    credentials,
    conversationParameters,
    (t, ct) =>
    {
        conversationReference = t.Activity.GetConversationReference();
        return Task.CompletedTask;
    },
    cancellationToken);


await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
    _appId,
    conversationReference,
    async (t, ct) =>
    {
        await t.SendActivityAsync(MessageFactory.Text("This will be the first response to the new thread"), ct);
    },
    cancellationToken);
}