Example #1
0
 public void ProcessMessage(IChatMessage message, IChatResponse response)
 {
     var expression = new Expression(message.CommandText);
     response.Write(expression.Evaluate().ToString());
 }
Example #2
0
 public void ProcessMessage(IChatMessage message, IChatResponse response)
 {
     // parse the command so we know what to run
     var command = this.ParseCommand(message.CommandText);
     if (command == null)
     {
         response.Write("Erk! Not sure what to say to that.");
         return;
     }
     // check the script module exists
     var modulePath = this.GetFullModulePath(command.Command);
     if (!File.Exists(modulePath))
     {
         response.Write("Unknown command! Try \"@autobot Get-Help\" instead.");
         return;
     }
     // initialise the host
     var host = new Host.Host(this.Logger);
     // add a handler for OnWrite events so we can bubble them up to the chat session
     var hostUi = (host.UI as UserInterface);
     if (hostUi != null)
     {
         hostUi.OnWrite += (sender, value) => response.Write(value);
     }
     // create a new initial state with the script module loaded
     var state = InitialSessionState.CreateDefault();
     state.ImportPSModule(new string[] { modulePath });
     // run the script inside the host
     using (var runspace = RunspaceFactory.CreateRunspace(host, state))
     {
         runspace.Open();
         using (var invoker = new RunspaceInvoke(runspace))
         {
             try
             {
                 // execute the PowerShell function with the same name as the module
                 IList errors;
                 var psObjects = invoker.Invoke(string.Format("{0} {1}", command.Command, command.Parameters), null, out errors);
                 // handle any errors
                 if ((errors != null) && (errors.Count > 0))
                 {
                     var errorString = new System.Text.StringBuilder();
                     foreach (var error in errors)
                     {
                         errorString.AppendLine(error.ToString());
                     }
                     this.Logger.Error(string.Format("ERROR!: {0}", errorString.ToString()));
                     response.Write(string.Format("OOohhh, I got an error running {0}. It looks like this:", command));
                     response.Write(errorString.ToString());
                     return;
                 }
                 // write the result
                 foreach (var psObject in psObjects)
                 {
                     response.Write(this.SerializePSObject(psObject));
                 }
             }
             catch (Exception ex)
             {
                 this.Logger.Error("ERROR!: ", ex);
                 response.Write(string.Format("Urghhh!, that didn't taste nice!  There's a problem with me running the {0} script.", command));
                 response.Write(string.Format("Check you are calling the script correctly by using \"@autobot get-help {0}\"", command));
                 response.Write("If all else fails ask your administrator for the event/error log entry.");
             }
         }
     }
 }