机器人对话参考问题

时间:2020-09-18 12:27:42

标签: botframework microsoft-teams

我的主动型机器人遇到了问题。

我的问题是,当我第一次将漫游器添加到团队中时,它会触发并触发创建团队和常规频道的事件。

我的问题是,在添加漫游器之前,它没有为其他现有渠道创建聊天参考。 如果我向团队添加新频道,则会触发channelCreated事件并创建新的会话引用。 我已经覆盖了OnConversationUpdateActivityAsync,并且如上所述,它在第一次添加漫游器然后添加新通道时有效。

任何人都知道首次添加该机器人的最佳做法是什么?第一次添加漫游器时,是否应该为所有现有渠道创建对话参考?

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 个答案:

答案 0 :(得分:0)

我假设您只是希望能够在所有频道上收到漫游器消息。如果不是这种情况,请告诉我,我将编辑答案。

我建议您看看Teams - Start New Thread in Channel Sample

基本上,您需要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()}");
    }
}

然后您可以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);
}