tompaana / intermediator-bot-sample

A sample bot, built with the Microsoft Bot Framework (v4), that routes messages between two users on different channels.
https://tompaana.github.io/content/chatbots_as_middlemen.html
MIT License
125 stars 65 forks source link

Bot Framework V4 Human Handoff Middlewareset Issue #42

Open chinnivyshnavi opened 5 years ago

chinnivyshnavi commented 5 years ago

Hello,

Please help me I am a beginner for the Human Handover in the V4 bot framework. I am following the approach of the middleware. Also, I have the LUIS as well added as a service to my bot. I added the middleware in my Startup.cs file as follows.

services.AddBot(options => { options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

            // Catches any errors that occur during a conversation turn and logs them to currently
            // configured ILogger.
            ILogger logger = _loggerFactory.CreateLogger<BasicLUISBot>();
            options.OnTurnError = async (context, exception) =>
            {
                logger.LogError($"Exception caught : {exception}");
                await context.SendActivityAsync($"Sorry, it looks like something went wrong. {exception}");
            };
            options.Middleware.Add(new HandoffMiddleware(Configuration));
        });

I am using the Microsoft.Bot.Builder version 4.1.5

My Middleware code is public async Task OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken) { Activity activity = context.Activity;

        if (activity.Type is ActivityTypes.Message)
        {               

            MessageRouter.StoreConversationReferences(activity);

            AbstractMessageRouterResult messageRouterResult = null;

            // Check the activity for commands
            if (await CommandHandler.HandleCommandAsync(context) == false)
            {                   
                messageRouterResult = await MessageRouter.RouteMessageIfSenderIsConnectedAsync(activity);

                if (messageRouterResult is MessageRoutingResult
                    && (messageRouterResult as MessageRoutingResult).Type == MessageRoutingResultType.NoActionTaken)
                {
                    if (!string.IsNullOrWhiteSpace(activity.Text)
                        && (activity.Text.ToLower() == "human"))
                    {
                        messageRouterResult = MessageRouter.CreateConnectionRequest(
                            MessageRouter.CreateSenderConversationReference(activity),
                            true);
                    }
                    else
                    {
                        // No action taken - this middleware did not consume the activity so let it propagate
                        await next(cancellationToken);
                    }
                }
            }               

When I run the solution in the emulator, the create connection is assigned to "NotSetup" even i connected in two seperate instances in the emlator and during debug I get the following error image

Please help me here

chinnivyshnavi commented 5 years ago

@tompaana can you please help me

hshahbaz commented 5 years ago

@chinnivyshnavi did you find a solution for this? I have a bot on .NET core that is using LUIS and QnA Maker. The last thing I want to add is to implement bot to human handoff by opening a channel in MSTeams.

@tompaana implementation works great with the hand off I want to do but I want to integrate this in my code. I wonder if you have used his classes and could point me in the right direction.

gauravanandwhiz commented 3 years ago

@chinnivyshnavi, @hshahbaz did you manage to integrate the Agent HandOff with your code? The IntermediatorBot code works fine in isolation but, when I integrated my code(which latest version packages), the HandoffMiddleware.OnTurnAsync(...) doesn't get triggered as my code has a Controller / ApiController class as follows.

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;

namespace Yoda.Controllers
{
    // This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
    // implementation at runtime. Multiple different IBot implementations running at different endpoints can be
    // achieved by specifying a more specific type for the bot constructor argument.
    [Route("api/messages")]
    [ApiController]
    public class BotController : ControllerBase
    {
        private readonly IBotFrameworkHttpAdapter Adapter;
        private readonly IBot Bot;

        public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
        {
            Adapter = adapter;
            Bot = bot;
        }

        [HttpPost, HttpGet]
        public async Task PostAsync()
        {
            // Delegate the processing of the HTTP POST to the adapter.
            // The adapter will invoke the bot.
            await Adapter.ProcessAsync(Request, Response, Bot);
        }
    }
}

Would appreciate if you could share whether, you could manage to integrate your code with the IntermediatorBot?