Search code examples
c#azurebotframeworkbotsmicrosoft-teams

Communication between Teams and Web Chat via Bot


With https://portal.azure.com I managed to create a QnA Bot.

I also managed to send a message from the Bot Framework Emulator to MS Teams and the Test Web Chat of the bot. (Though it is very static and manually done as of now)

I get my Token from https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token. With that Token I can send a message to https://smba.trafficmanager.net/emea/v3/conversations/ConversationID/activities (which is Teams) and I can send a message to https://webchat.botframework.com/v3/conversations/ConversationID/activities (which is the Test Web Chat).

Little bit offtopic: Since I'm new to Azure I wonder if everything done so far is fine or completly false?

Anyway...

My goal so far is that when writing a message in the Test Web Chat the message should also end up in MS Teams. Then from Teams I want to answer to the message like @Bot This is my Answer. This Answer in Teams should then also end up in the Test Web Chat. (So basically communication between Teams and Web Chat)

What would be the best approach to do this?

I was thinking of a backend in Azure but I don't know what would be possible to use and if it's even possible.

My idea was:

A message is sent Web Chat. In Code I would upload the Web Chat Conversation Information to Azure. Also in Teams there is now the same message that was written in Web Chat. The Conversation ID in Teams also needs to be uploaded now and the Conversation ID the Web Chat is sending to needs to be updated (this would be needed so there is no spamming of new threads/messages in Teams from one Conversation and all the messages from this Web Chat Conversation are now in one thread.) Now to answer in the Conversation of the Web Chat via Teams I would write and send the answer. Before the sending of the answer happens I would need the Conversation ID from the Web Chat. So the Bot would download the information from Azure so it knows where it has to sent the answer to.

Is this a good apporach or do you know better ones? Also what ressources in Azure could I use to achieve this?


Solution

  • It sounds like you are thinking about this the right way. Basically you want to pair two conversation references - one in Teams and the other in Web Chat - together and then forward the messages between the two.

    BotFramework SDK v4 (C#)

    I would recommend creating two functions - AddConversationReferenceAsync and GetConversationReferenceAsync. In those functions, you should manage how you store, connect, and retrieve conversation references. You can check the channel Id - activity.ChannelId - to determine how you want to handle the reference. Then in OnMessageActivityAsync you can add and retrieve the corresponding conversation references to send a proactive message to the other channel.

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        AddConversationReferenceAsync(turnContext.Activity as Activity);
        var conversationReference = GetConversationReferenceAsync(turnContext.Activity as Activity);
    
        if (conversationReference != null) {
          await turnContext.Adapter.ContinueConversationAsync(_appId, conversationReference, (ITurnContext context, CancellationToken cancellationToken) => {
            await context.SendActivityAsync(turnContext.Activity);
          }, cancellationToken);
        } else {
          await turnContext.SendActivityAsync("You are not connected to anyone at the moment");
        }
    }
    

    Screen Capture

    enter image description here

    I would recommend looking at the BotBuilder Proactive Messaging sample, and @tompaana built a Hunan Handoff Bot sample that connects conversations in Teams and Slack that might be helpful.

    Hope this helps!