コード例 #1
0
    async public Task OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken))
    {
        Console.WriteLine(context.Activity.Type);

        switch (context.Activity.Type)
        {
        case ActivityTypes.ConversationUpdate:

            if (context.Activity.MembersAdded != null)
            {
                var msg = "Hi! I'm Gartner Bot. You can ask me questions about vendor briefings or the magic quadrant process. I can also find reports for you if you ask me 'get me the top 5 reports about mobile development based on IDE'.";
                foreach (var newMember in context.Activity.MembersAdded)
                {
                    if (newMember.Id != context.Activity.Recipient.Id)
                    {
                        await context.SendActivityAsync(msg);
                    }
                }
            }

            break;

        case ActivityTypes.Message:

            if (_didHandoff == context.Activity.From.Id || _isWatching == context.Activity.From.Id)
            {
                await next.Invoke(cancellationToken);

                return;
            }

            if (context.Activity.Text == "agent-sign-in")
            {
                _isWatching                = context.Activity.From.Id;
                context.Activity.Text      = "command watch";
                context.Activity.From.Role = "agent";
                await next.Invoke(cancellationToken);

                return;
            }

            if (_didAskForHandoff == context.Activity.From.Id && context.Activity.Text.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
            {
                _didHandoff = context.Activity.From.Id;
                //User indicated they want to be connected to live agent

                context.Activity.Text      = "human";
                context.Activity.From.Role = "user";
                await next.Invoke(cancellationToken);

                return;
            }

            //Get the intent recognition result
            var recognizerResult = await DispatchService.RecognizeAsync(context, cancellationToken);

            var topIntent = recognizerResult?.GetTopScoringIntent();

            if (topIntent == null)
            {
                await AskForHandoff(context, "I'm not quite sure what you mean.");
            }
            else
            {
                await DispatchToTopIntentAsync(context, topIntent, cancellationToken);
            }
            break;
        }

        //await next(cancellationToken).ConfigureAwait(false);
    }