How to send a 'conversationUpdate' to Microsoft Teams from bot manually?

核能气质少年 提交于 2019-12-02 11:41:48

问题


I have a bot written with the help of bot framework v4. The bot is integrated with Microsoft Teams. I want to send a welcome message to the user when the user installed the bot and joins the 1:1 conversation. In Teams the conversationUpdate is fired exactly once (this is when the suer joins the 1:1 conversation) and then never again for that user. My idea was to write a function that is triggered by a chat message to send the updateConversation activity manually to debug the welcome message.

I failed so far and got a

BadArgument: Unknown activity type exception.

I have tried using the Microsoft.Bot.Builder.Teams nuget using the ConnectorClient to send the conversationUpdate activity to the conversation.

Also I set up a console application and tried using the v3/directline/conversations/{conversationId}/activities and got a Forbidden error.

private async Task SendConversationUpdateToTeamsAsync(ITurnContext turnContext, CancellationToken cToken = default)
{
    var connectorClient = turnContext.TurnState.Get<IConnectorClient>();

    var conversationUpdateMessage = new Activity
    {
        Type = ActivityTypes.ConversationUpdate,
        Id = turnContext.Activity.Id,
        ServiceUrl = turnContext.Activity.ServiceUrl,
        From = turnContext.Activity.From,
        Recipient = turnContext.Activity.Recipient,
        Conversation = turnContext.Activity.Conversation,
        ChannelData = turnContext.Activity.ChannelData,
        ChannelId = turnContext.Activity.ChannelId,
        Timestamp = turnContext.Activity.Timestamp,
        MembersAdded = new List<ChannelAccount>
        {
            turnContext.Activity.From,
            turnContext.Activity.Recipient
        },
    };

    var result = await connectorClient.Conversations.SendToConversationAsync(conversationUpdateMessage, cToken);
}

I expect that sending a conversationUpdate manually to debug the behavior in Teams works. Creating new users in the office portal and installing the bot for them to debug the conversationUpdate behavior is no option for me, because it is to time consuming. If there is another workaround to trigger the conversationUpdate in Teams please let me know.


回答1:


I'm not sure of a way to force a ConversationUpdate to be sent in the way you're attempting to. Instead, I'd just throw something like this in OnMessageAsync():

if (turnContext.Activity.Text == "fakeConversationUpdate")
{
    var fakeTurnContext = new TurnContext(turnContext.Adapter, MessageFactory.Text(string.Empty));
    fakeTurnContext.Activity.AsConversationUpdateActivity();
    fakeTurnContext.Activity.Type = ActivityTypes.ConversationUpdate;
    fakeTurnContext.Activity.MembersAdded = new List<ChannelAccount>()
    {
        new ChannelAccount()
        {
            Id = "fakeUserId",
            Name = "fakeUserName"
        }
    };
    await OnConversationUpdateActivityAsync(new DelegatingTurnContext<IConversationUpdateActivity>(fakeTurnContext), cancellationToken);

}

Then to debug, you just write "fakeConversationUpdate" (which you can change/customize) to the bot in chat and it will send your fakeTurnContext (which you can change/customize) through OnConversationUpdateActivityAsync()



来源:https://stackoverflow.com/questions/57304988/how-to-send-a-conversationupdate-to-microsoft-teams-from-bot-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!