Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...