Пример #1
0
 public override void OnStart(Message msg)
 {
     try
     {
         var sb = new StringBuilder();
         sb.AppendLine("Processes:");
         var needClean = false;
         lock (Shell.ProcessTable.LockObject)
         {
             foreach (var proc in Shell.ProcessTable.Table)
             {
                 var workingStr = proc.Value.Working ? "working" : "not working";
                 sb.AppendLine($"{proc.Key} - {proc.Value.Path} - {workingStr}");
                 if (!proc.Value.Working)
                 {
                     needClean = true;
                 }
             }
         }
         var listTask = TelegramApi.SendTextMessageAsync(msg.Chat.Id, sb.ToString());
         if (needClean)
         {
             listTask.ContinueWith(x => TelegramApi.SendTextMessageAsync(msg.Chat.Id, "/pclean - to clean not working processes"));
         }
     }
     finally
     {
         Session.Exit(this);
     }
 }
Пример #2
0
        public virtual void InitProcess(long chatId)
        {
            if (ExecProcess != null)
            {
                return;
            }
            try
            {
                ExecProcess           = new Process();
                ExecProcess.StartInfo = new ProcessStartInfo();
                ExecProcess.StartInfo.WorkingDirectory       = Shell.CurrentDirectory;
                ExecProcess.StartInfo.UseShellExecute        = false;
                ExecProcess.StartInfo.RedirectStandardInput  = true;
                ExecProcess.StartInfo.RedirectStandardOutput = true;
                ExecProcess.StartInfo.RedirectStandardError  = true;
                ExecProcess.StartInfo.FileName  = Path;
                ExecProcess.StartInfo.Arguments = Arguments;

                ExecProcess.Start();

                ExecProcess.StandardInput.AutoFlush = true;
                Task.Run(() => BeginProxy(chatId, ExecProcess.StandardOutput, ExecProcess.StandardError));
            }
            catch (Exception ex)
            {
                Session.Exit(this);
                TelegramApi.SendTextMessageAsync(chatId, "Exec error: " + ex.Message);
            }
        }
Пример #3
0
 public override void Process(Message msg)
 {
     if (WaitingSaveCheck)
     {
         if (msg.Type == Telegram.Bot.Types.Enums.MessageType.TextMessage)
         {
             var text = msg.Text.ToLower();
             if (text == "cancel")
             {
                 TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Canceled");
                 Session.Exit(this);
             }
             else if (text == "ok")
             {
                 Task.Run(() => StreamUploader(msg.Chat.Id, FileId, Path));
                 TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Uploading file...");
                 WaitingSaveCheck = false;
             }
             else
             {
                 SendSaveQuestion(msg.Chat.Id);
             }
             return;
         }
         else
         {
         }
     }
     else
     {
         TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Uploading file...");
     }
 }
Пример #4
0
 public override void Process(Message msg)
 {
     if (string.IsNullOrWhiteSpace(msg.Text))
     {
         if (TempKey == null)
         {
             TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Waiting..");
         }
         else
         {
             SendQuestion(msg.Chat.Id, TempKey.Value);
         }
     }
     else
     {
         if (TempKey == null)
         {
             if (msg.Text == "/cancel")
             {
                 Session.Exit(this);
             }
             else
             {
                 TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Waiting..");
             }
         }
         else
         {
             ProcessAnswer(TempKey.Value, msg.Text);
             TempKey = null;
             ProcessNext(msg.Chat.Id);
         }
     }
 }
Пример #5
0
        protected override void ProcessComplete(long chatId)
        {
            var path = Shell.GetPath(Path);

            if (File.Exists(path))
            {
                Task.Run(() =>
                {
                    try
                    {
                        using (var stream = File.OpenRead(path))
                        {
                            var filename   = System.IO.Path.GetFileName(path);
                            var fileToSend = new FileToSend(filename, stream);
                            TelegramApi.SendDocumentAsync(chatId, fileToSend).Wait();
                        }
                    }
                    catch (Exception ex)
                    {
                        TelegramApi.SendTextMessageAsync(chatId, "File sending error: " + ex.Message);
                    }
                    finally
                    {
                        Session.Exit(this);
                    }
                });
            }
            else
            {
                TelegramApi.SendTextMessageAsync(chatId, "File " + path + " not exists");
                Session.Exit(this);
            }
        }
Пример #6
0
        private void SendSaveQuestion(long chatId)
        {
            WaitingSaveCheck = true;
            var buttons = new string[] { "OK", "Cancel" }.Select(x => new KeyboardButton(x)).Select(x => new KeyboardButton[] { x }).ToArray();
            var keyboard = new ReplyKeyboardMarkup(buttons, oneTimeKeyboard: true);

            TelegramApi.SendTextMessageAsync(chatId, "File " + Path + " already exist", replyMarkup: keyboard);
        }
Пример #7
0
        protected virtual void SendQuestion(long chatId, KeyValuePair <string, string> key)
        {
            var variants = ListVariants(key);
            var buttons  = variants.Select(x => new KeyboardButton(x)).Select(x => new KeyboardButton[] { x }).ToArray();
            var keyboard = new ReplyKeyboardMarkup(buttons, oneTimeKeyboard: true);

            TelegramApi.SendTextMessageAsync(chatId, key.Value, replyMarkup: keyboard);
        }
Пример #8
0
 public override void Process(Message msg)
 {
     lock (AuthSet)
     {
         AuthSet.Add(msg.Chat.Id);
         TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Auth success").Wait();
     }
 }
Пример #9
0
 public override void OnStart(Message msg)
 {
     try
     {
         Shell.ProcessTable.CleanUp(false);
         TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Clean up complete");
     }
     finally
     {
         Session.Exit(this);
     }
 }
Пример #10
0
 protected override void ProcessComplete(long chatId)
 {
     try
     {
         var path = Shell.GetPath(Path);
         Shell.CurrentDirectory = path;
         TelegramApi.SendTextMessageAsync(chatId, Shell.CurrentDirectory);
     }
     finally
     {
         Session.Exit(this);
     }
 }
Пример #11
0
 public override void OnStart(Message msg)
 {
     try
     {
         var args = Helpers.Ext.ParseArgs(msg.Text);
         ProcessStartArgs(msg, args);
         ProcessNext(msg.Chat.Id);
     }
     catch (Exception ex)
     {
         Session.Exit(this);
         TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Error: " + ex.Message);
     }
     finally
     {
     }
 }
Пример #12
0
        protected override void OnHelp(Message msg)
        {
            var sb = new StringBuilder();

            sb.AppendLine($"TgShell help:");
            sb.AppendLine($"    /cd - change dir");
            sb.AppendLine($"    /ls - list dir");
            sb.AppendLine($"    /cat - cat file");
            sb.AppendLine($"    /rm - remove file");
            sb.AppendLine($"    /download - download file from dir");
            sb.AppendLine($"    /exec - start a process");
            sb.AppendLine($"    /pls - list session's processes");
            sb.AppendLine($"    /whoami - info about this chat");
            sb.AppendLine($"Current directory:");
            sb.AppendLine($"    {CurrentDirectory}");

            TelegramApi.SendTextMessageAsync(msg.Chat.Id, sb.ToString()).Wait();
        }
Пример #13
0
        public override void OnStart(Message msg)
        {
            try
            {
                var sb = new StringBuilder();
                sb.AppendLine($"Chat id: {msg.Chat.Id}");
                sb.AppendLine($"Chat first name: {msg.Chat.FirstName}");
                sb.AppendLine($"Chat last name: {msg.Chat.LastName}");
                sb.AppendLine($"Chat user name: {msg.Chat.Username}");
                sb.AppendLine($"Chat title: {msg.Chat.Title}");

                TelegramApi.SendTextMessageAsync(msg.Chat.Id, sb.ToString());
            }
            finally
            {
                Session.Exit(this);
            }
        }
Пример #14
0
 protected override void ProcessComplete(long chatId)
 {
     try
     {
         var path = Shell.GetPath(Path);
         if (File.Exists(path))
         {
             File.Delete(path);
             TelegramApi.SendTextMessageAsync(chatId, "File " + path + " removed");
         }
         else
         {
             TelegramApi.SendTextMessageAsync(chatId, "File " + path + " not exists");
         }
     }
     finally
     {
         Session.Exit(this);
     }
 }
Пример #15
0
 public void Flush(long chatId)
 {
     lock (Output)
     {
         if (Output.Length == 0)
         {
             return;
         }
         try
         {
             foreach (var msg in Ext.SplitBigMessage(Output.ToString()))
             {
                 TelegramApi.SendTextMessageAsync(chatId, msg).Wait();
             }
         }
         catch
         {
         }
         Output.Clear();
     }
 }
Пример #16
0
        private void StreamUploader(long chatId, string fileId, string path)
        {
            try
            {
                Shell.GetPath(path);

                using (var outputStream = System.IO.File.Create(path))
                {
                    TelegramApi.GetFileAsync(fileId, destination: outputStream).Wait();
                }
                TelegramApi.SendTextMessageAsync(chatId, "File uploaded to: " + path).Wait();
            }
            catch (Exception ex)
            {
                TelegramApi.SendTextMessageAsync(chatId, "File uploading error: " + ex.Message).Wait();
            }
            finally
            {
                Session.Exit(this);
            }
        }
Пример #17
0
        public override void OnStart(Message msg)
        {
            try
            {
                Path   = Shell.GetPath(msg.Document.FileName);
                FileId = msg.Document.FileId;

                if (System.IO.File.Exists(Shell.GetPath(Path)))
                {
                    SendSaveQuestion(msg.Chat.Id);
                }
                else
                {
                    Task.Run(() => StreamUploader(msg.Chat.Id, FileId, Path));
                    TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Uploading file...");
                }
            }
            catch (Exception ex)
            {
                Session.Exit(this);
                TelegramApi.SendTextMessageAsync(msg.Chat.Id, ex.Message);
            }
        }
Пример #18
0
        public override void OnStart(Message msg)
        {
            try
            {
                var args = Helpers.Ext.ParseArgs(msg.Text);

                var path = "./";
                if (args.Length > 1)
                {
                    path = args[1];
                }
                path = Shell.GetPath(path);

                var sb = new StringBuilder();
                sb.AppendLine("Directories:");
                foreach (var dir in Directory.GetDirectories(path))
                {
                    sb.AppendLine("    " + Path.GetFileName(dir));
                }
                sb.AppendLine("Files:");
                foreach (var file in Directory.GetFiles(path))
                {
                    sb.AppendLine("    " + Path.GetFileName(file));
                }

                TelegramApi.SendTextMessageAsync(msg.Chat.Id, sb.ToString());
            }
            catch (Exception ex)
            {
                TelegramApi.SendTextMessageAsync(msg.Chat.Id, "Error: " + ex.Message);
            }
            finally
            {
                Session.Exit(this);
            }
        }
Пример #19
0
 public override void Process(Message msg)
 {
     TelegramApi.SendTextMessageAsync(msg.Text, "Auth failed");
 }