コード例 #1
0
 /// <summary>
 /// Easy way to get cmd controller instance
 /// </summary>
 /// <returns></returns>
 private CmdController GetCmdControllerInstance()
 {
     return(CmdController.GetInstance());
 }
コード例 #2
0
        /// <summary>
        /// Evaluates the given command
        /// </summary>
        /// <param name="command">Command to be runed</param>
        /// <param name="args">Arguments to specify the command</param>
        public async void EvaluateCommand(string command, string[] args)
        {
            switch (command.Replace("/", string.Empty).ToLower())
            {
                #region screenshot
            case "screenshot":
                if (!Properties.Settings.Default.AllowScreenshotRequest)
                {
                    GetTelegramBotControllerInstance().SendMessage(MessageSettingDisabled);
                    return;
                }
                GetTelegramBotControllerInstance().SendMessage("Screenshot gets rendered...");
                string     screenshotPath = ScreenshotController.GetScreenshot();
                FileToSend fts            = new FileToSend();
                if (Properties.Settings.Default.SendScreenshotFile)
                {
                    using (var stream = System.IO.File.Open(screenshotPath, FileMode.Open))
                    {
                        fts.Content  = stream;
                        fts.Filename = screenshotPath.Split('\\').Last();
                        try
                        {
                            await GetTelegramBotControllerInstance().Bot.SendDocumentAsync(GetTelegramBotControllerInstance().LastReceivedChatId, fts, "Screenshot Document (HQ)");
                        }
                        catch (Exception e)
                        {
                            AppendAsyncInfoLine(e.Message);
                        }
                    }
                }

                if (Properties.Settings.Default.SendScreenshotImage)
                {
                    using (var stream = System.IO.File.Open(screenshotPath, FileMode.Open))
                    {
                        fts.Content  = stream;
                        fts.Filename = screenshotPath.Split('\\').Last();
                        try
                        {
                            await GetTelegramBotControllerInstance().Bot.SendPhotoAsync(GetTelegramBotControllerInstance().LastReceivedChatId, fts, "Screenshot Image (LQ)");
                        }
                        catch (Exception e)
                        {
                            AppendAsyncInfoLine(e.Message);
                        }
                    }
                }

                break;

                #endregion
                #region cmd
            case "cmd":
                if (!Properties.Settings.Default.AllowCmdRequest)
                {
                    GetTelegramBotControllerInstance().SendMessage(MessageSettingDisabled);
                    return;
                }
                if (args.Length < 1)
                {
                    GetTelegramBotControllerInstance().SendMessage("Command cmd needs at least one parameter");
                    return;
                }

                string[] forbiddenExpressions = Properties.Settings.Default.ForbiddenExpressions.Split(';');

                string expression = string.Join(" ", args);

                if (!(forbiddenExpressions.Length == 1 && forbiddenExpressions[0] == "") && forbiddenExpressions.Any(s => expression.Trim().Contains(s)))
                {
                    GetTelegramBotControllerInstance().SendMessage("Your expression contains forbidden characters");
                    return;
                }

                CmdController.GetInstance().RunCommand(expression);
                break;

                #endregion
                #region file
            case "file":
                if (!Properties.Settings.Default.AllowSendDocuments)
                {
                    GetTelegramBotControllerInstance().SendMessage(MessageSettingDisabled);
                    return;
                }
                string   path           = string.Join(" ", args);
                string[] forbiddenPaths = Properties.Settings.Default.ForbiddenPaths.Split(';');
                if (!(forbiddenPaths.Length == 1 && forbiddenPaths[0] == "") && forbiddenPaths.Any(s => path.Trim().Contains(s)))
                {
                    GetTelegramBotControllerInstance().SendMessage("Your parameter contains forbidden characters");
                    return;
                }
                if (!System.IO.File.Exists(path))
                {
                    GetTelegramBotControllerInstance().SendMessage($"No document found at '{path}'");
                }
                else
                {
                    using (var stream = System.IO.File.Open(path, FileMode.Open))
                    {
                        fts.Content  = stream;
                        fts.Filename = path.Split('\\').Last();
                        try
                        {
                            await GetTelegramBotControllerInstance().Bot.SendDocumentAsync(GetTelegramBotControllerInstance().LastReceivedChatId, fts);
                        }
                        catch (Exception e)
                        {
                            AppendAsyncInfoLine(e.Message);
                        }
                    }
                }

                break;
                #endregion

            default:
                GetTelegramBotControllerInstance().SendMessage($"Command {command} cound not be interpreted.");
                break;
            }
        }