Search code examples
c#botframeworkazure-language-understanding

How to run CMD and plug in a command from a bot azure program if intent is recognized?


Below is a sample intent from the Azure SDK bot that runs in the bot framework emulator. The bot recognizes my intent by returning a string type response. This was just a preparation for the bot, when it recognizes my intention, it is supposed to run the CMD program and execute the command in the system, and after executing the command in the CMD and completing this, it will return a response that the command was executed.... However, as you can see below, unfortunately this does not work. Instead, the bot immediately returns all responses without waiting and running the command in CMD.

case WebAppBotTester.Intent.TestPageOne:
   var getSearchActionText = "Redirecting to the Action and run CMD, wait...";
   var getSearchActionMessage = MessageFactory.Text(getSearchActionText, getSearchActionText, InputHints.IgnoringInput);
   await stepContext.Context.SendActivityAsync(getSearchActionMessage, cancellationToken);
   string command = @"cd ..\\..& cd tests & npx [MAKE ACTION..]";
   ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
   cmdsi.Arguments = command;
   Process cmd = Process.Start(cmdsi);
   cmd.WaitForExit();
   var getresultActionText = "The result is ready!";
   var getresultActionMessage = MessageFactory.Text(getresultActionText , getresultActionText, InputHints.IgnoringInput);
   await stepContext.Context.SendActivityAsync(getresultActionMessage, cancellationToken);
break;

What am I doing wrong ?


Solution

  • This solved my problem:

    I have written a simple NodeJsServer class in C# that can help you with these things. It is available on GitHub here. It has a lot of options, you can execute 'npm install' commands in specific directories, or start NodeJs, check the current status (is it running, is it compiling, is it starting, is it installing) and finally stop the NodeJs. Check the quick example usage.

    This is the raw code (copied mostly from the NodeJsServer class) of what you are trying to do:

    // create the command-line process
    var cmdProcess = new Process
    {
        StartInfo =
        {
            FileName = "cmd.exe",
            UseShellExecute = false,
            CreateNoWindow = true, // this is probably optional
            ErrorDialog = false, // this is probably optional
            RedirectStandardOutput = true,
            RedirectStandardInput = true
        }
    };
    
    // register for the output (for reading the output)
    cmdProcess.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
    {
        string output = e.Data;
        // inspect the output text here ...
    };
    
    // start the cmd process
    cmdProcess.Start();
    cmdProcess.BeginOutputReadLine();
    
    // execute your command
    cmdProcess.StandardInput.WriteLine("quicktype --version");