示例#1
0
        public async Task OnTurnAsync(ITurnContext turnContext,
                                      CancellationToken cancellationToken = default(CancellationToken))
        {
            var user =
                await _stateBotPropertyAccessors.UserAccessor.GetAsync(turnContext,
                                                                       () => new User { DidBotWelcomeUser = false },
                                                                       cancellationToken);

            if (turnContext.Activity.Conversation != null)
            {
                _logger.LogDebug(
                    $"From JobId: {turnContext.Activity.From.Id} - Activity JobId: {turnContext.Activity.Id} - Conversation JobId: {turnContext.Activity.Conversation.Id} - Activity Type: {turnContext.Activity.Type} - Message: {turnContext.Activity.Text}");
            }

            switch (turnContext.Activity.Type)
            {
            // Handle Message activity type, which is the main activity type for shown within a conversational interface
            // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
            // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
            case ActivityTypes.Message:
            {
                // Check LUIS model
                try
                {
                    var recognizerResult =
                        await _services.LuisServices[LuisKey].RecognizeAsync(turnContext, cancellationToken);
                    var topIntent = recognizerResult?.GetTopScoringIntent();
                    if (topIntent != null && topIntent.Value.score >= 0.5)
                    {
                        Job job;
                        switch (topIntent.Value.intent)
                        {
                        case StartRecording:
                            await turnContext.SendActivityAsync("Boss I'm going to start the recording.",
                                                                inputHint : InputHints.IgnoringInput,
                                                                cancellationToken : cancellationToken);

                            await turnContext.Adapter.ContinueConversationAsync(AppId,
                                                                                _stateBotPropertyAccessors.AudioRecorderConversationReference,
                                                                                async (context, token) =>
                                {
                                    await context.SendActivityAsync(StartRecording, cancellationToken: token);
                                },
                                                                                cancellationToken);

                            break;

                        case SalesForecast:
                            await turnContext.SendActivityAsync(
                                "Boss, I'm querying finance systems right now. I'll get back to you.",
                                cancellationToken : cancellationToken);

                            await Task.Delay(7000, cancellationToken);

                            await turnContext.SendActivityAsync(
                                "Our sales forecast for this year will be $100 million. So work hard play hard!",
                                cancellationToken : cancellationToken);

                            break;

                        case StopRecording:
                            var replies = new IActivity[2];
                            job = await CreateJob(turnContext, cancellationToken);

                            var stopRecordingReply = turnContext.Activity.CreateReply();
                            stopRecordingReply.Text =
                                "Boss I'm going to stop the recording and will start processing meeting minutes.";
                            stopRecordingReply.InputHint = InputHints.IgnoringInput;
                            stopRecordingReply.Speak     =
                                "Boss I'm going to stop the recording and will start processing meeting minutes.";
                            replies[0] = stopRecordingReply;
                            var messageReply = turnContext.Activity.CreateReply();
                            messageReply.Text =
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.";
                            messageReply.Speak =
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.";
                            messageReply.InputHint = InputHints.IgnoringInput;
                            replies[1]             = messageReply;
                            await turnContext.SendActivitiesAsync(replies, cancellationToken);

                            await turnContext.Adapter.ContinueConversationAsync(AppId,
                                                                                _stateBotPropertyAccessors.AudioRecorderConversationReference,
                                                                                async (context, token) =>
                                {
                                    await context.SendActivityAsync($"{StopRecording},{job.Id}",
                                                                    cancellationToken: token);
                                },
                                                                                cancellationToken);

                            break;

                        case CreateHelpDeskTicket:
                            job = await CreateJob(turnContext, cancellationToken);

                            await turnContext.SendActivityAsync(
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.",
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.",
                                inputHint : InputHints.IgnoringInput,
                                cancellationToken : cancellationToken);

                            var uiPathArguments = new UiPathArguments
                            {
                                BotAppId       = AppId,
                                BotAppPassword = AppPassword,
                                JobId          = job.Id.ToString(),
                                ServiceUrl     = ServiceEndpoint
                            };
                            await _uiPathHttpClient.SendUiPathJob(uiPathArguments,
                                                                  "963d294a-960b-4e16-b6af-3b5dd782f637",
                                                                  cancellationToken);

                            break;

                        case BuyAmazon:
                            job = await CreateJob(turnContext, cancellationToken);

                            await turnContext.SendActivityAsync(
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.",
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.",
                                inputHint : InputHints.IgnoringInput,
                                cancellationToken : cancellationToken);

                            var amazonJob = new AmazonUiPathArguments
                            {
                                BotAppId       = AppId,
                                BotAppPassword = AppPassword,
                                JobId          = job.Id.ToString(),
                                ServiceUrl     = ServiceEndpoint,
                                Products       = new List <string> {
                                    "2QW1646 Cisco RV320"
                                }
                            };
                            await _uiPathHttpClient.SendUiPathJob(amazonJob,
                                                                  "91497268-6c3a-4bef-8fdd-36a802386921",
                                                                  cancellationToken);

                            break;

                        case SendEmail:
                            job = await CreateJob(turnContext, cancellationToken);

                            await turnContext.SendActivityAsync(
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.",
                                $"Job {job.Id} is created. Delegating task to an RPA Robot. We'll notify you when it's complete.",
                                inputHint : InputHints.IgnoringInput,
                                cancellationToken : cancellationToken);

                            var emailJob = new UiPathEmailJob(job.Id.ToString(),
                                                              turnContext.Activity.ServiceUrl,
                                                              $"Meeting minutes for {DateTime.Today.ToShortDateString()}", "Hello World",
                                                              "*****@*****.**");

                            await _uiPathHttpClient.SendUiPathJob(emailJob,
                                                                  "91497268-6c3a-4bef-8fdd-36a802386921",
                                                                  cancellationToken);

                            break;

                        default:
                            // Help or no intent identified, either way, let's provide some help.
                            // to the user
                            await turnContext.SendActivityAsync(
                                "Boss I'm sorry but I didn't understand what you just said to me.",
                                inputHint : InputHints.IgnoringInput,
                                cancellationToken : cancellationToken);

                            break;
                        }
                    }
                    else
                    {
                        const string msg =
                            @"No LUIS intents were found. Try typing 'Start Meeting' or 'Stop Meeting'.";
                        await turnContext.SendActivityAsync(msg, msg, InputHints.IgnoringInput,
                                                            cancellationToken : cancellationToken);
                    }

                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

            // This will be true if it was an external send a type event and value is the job id.
            case ActivityTypes.Event:
            {
                _logger.LogDebug("Received an activity type event");
                var jobStorage =
                    await _jobStatePropertyAccessor.GetAsync(turnContext, () => new JobStorage(),
                                                             cancellationToken);

                var activity = turnContext.Activity.AsEventActivity();
                if (activity.Name == JobCompleteEventName &&
                    activity.Value is JObject value)
                {
                    var jobEvent = value.ToObject <UiPathJobResponse>();
                    if (jobEvent != null)
                    {
                        if (jobStorage.ContainsKey(jobEvent.JobId) &&
                            !jobStorage[jobEvent.JobId].Completed)
                        {
                            _logger.LogDebug("Completing Job...");
                            await CompleteJobAsync(turnContext.Adapter, AppId, jobStorage, jobEvent,
                                                   cancellationToken);
                        }
                    }
                }

                break;
            }

            case ActivityTypes.ConversationUpdate:
                if (turnContext.Activity.MembersAdded.Any())
                {
                    // Iterate over all new members added to the conversation
                    foreach (var member in turnContext.Activity.MembersAdded)
                    {
                        // Greet anyone that was not the target (recipient) of this message
                        // the 'bot' is the recipient for events from the channel,
                        // turnContext.Activity.MembersAdded == turnContext.Activity.Recipient.JobId indicates the
                        // bot was added to the conversation.
                        if (member.Id == turnContext.Activity.Recipient.Id)
                        {
                            continue;
                        }
                        _logger.LogTrace($"Member Name: {member.Name} & Member JobId: {member.Id}");
                        user.DidBotWelcomeUser = true;

                        if (member.Name == "AudioRecorder")
                        {
                            _stateBotPropertyAccessors.AudioRecorderConversationReference =
                                turnContext.Activity.GetConversationReference();
                        }

                        await _stateBotPropertyAccessors.UserAccessor.SetAsync(turnContext, user,
                                                                               cancellationToken);

                        await _stateBotPropertyAccessors.UserState.SaveChangesAsync(turnContext,
                                                                                    cancellationToken : cancellationToken);

                        // Send a welcome message to the user and tell them what actions they may perform to use this bot
                        await SendWelcomeMessageAsync(turnContext, cancellationToken);
                    }
                }

                break;

            default:
                await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected",
                                                    cancellationToken : cancellationToken);

                break;
            }
        }
        private static async Task WebSocketClientOnOnMessage(MessageEventArgs e)
        {
            try
            {
                if (e.Data == null)
                {
                    return;
                }
                var activitySet = JsonConvert.DeserializeObject <ActivitySet>(e.Data);

                if (activitySet == null)
                {
                    return;
                }
                foreach (var activity in activitySet.Activities)
                {
                    if (activity.Type != ActivityTypes.Message || string.IsNullOrEmpty(activity.Text))
                    {
                        continue;
                    }

                    if (activity.Text.Contains("Record_Start"))
                    {
                        if (_audioWriter != null)
                        {
                            WriteLine(
                                "Tried to record again but can only have one recording at a time. Stop the other recorder first.");
                            continue;
                        }

                        _audioWriter = new AudioWriter(OutputFolder);
                        _audioWriter.StartRecording();
                    }
                    else if (activity.Text.Contains("Record_Stop"))
                    {
                        var messages = activity.Text.Split(',');
                        var jobId    = messages[1];
                        _audioWriter?.StopRecording();
                        var uiPathDownloadArguments = new MeetingMinutesUiPathArguments
                        {
                            JobId          = jobId,
                            ServiceUrl     = Settings.BotServiceUrl,
                            BotAppId       = Settings.BotAppId,
                            BotAppPassword = Settings.BotAppPassword,
                            EmailToSend    = Settings.EmailToSend,
                            EmailBody      = Settings.EmailBody,
                            EmailSubject   = $"Meeting minutes for {DateTime.Today.ToShortDateString()}",
                        };
                        await CallCognitiveServices(uiPathDownloadArguments);
                        await UploadToAzureBlob(uiPathDownloadArguments);

                        await _uiPathHttpClient.SendUiPathJob(uiPathDownloadArguments,
                                                              Settings.UiPathMeetingMinutesJobKey);

                        _audioWriter = null;
                    }
                }
            }
            catch (Exception exception)
            {
                WriteLine(exception);
                throw;
            }
        }