botgram / shell-bot

:robot: Telegram bot that executes commands and sends the live output
GNU General Public License v3.0
797 stars 488 forks source link

Can I customize the command? #26

Closed vcfe closed 4 years ago

vcfe commented 4 years ago

I want to enter a script to /run or /start running

mildsunrise commented 4 years ago

you are supposed to enter the command just after /run, i.e. /run ./script.sh

there's currently no easy way to create custom shortcut commands, you'll have to edit the code for now...

vcfe commented 4 years ago

Thank you for your answer, Do you have any plans to add this feature

hrvstr commented 4 years ago

It's pretty easy to add your own commands. I replaced the /run command inside server.js with this code:

// Add name of the command you want to use with your bot (I use "process")
bot.command("process", function (msg, reply, next) {

  if (msg.context.command) {
    var command = msg.context.command;
    return reply.text("A command is already running.");
  }

  if (msg.editor) msg.editor.detach();
  msg.editor = null;

// Add the command that should be run in the shell here. My command is again called "process" which is an executable in /usr/local/bin/process
  var args = "process";
  msg.context.command = new Command(reply, msg.context, args);
  msg.context.command.on("exit", function() {
    msg.context.command = null;
  });
});
vcfe commented 4 years ago

Awesome, a good example, successfully helped me, thank you

cgkings commented 4 years ago

It's pretty easy to add your own commands. I replaced the /run command inside server.js with this code:

// Add name of the command you want to use with your bot (I use "process")
bot.command("process", function (msg, reply, next) {

  if (msg.context.command) {
    var command = msg.context.command;
    return reply.text("A command is already running.");
  }

  if (msg.editor) msg.editor.detach();
  msg.editor = null;

// Add the command that should be run in the shell here. My command is again called "process" which is an executable in /usr/local/bin/process
  var args = "process";
  msg.context.command = new Command(reply, msg.context, args);
  msg.context.command.on("exit", function() {
    msg.context.command = null;
  });
});

Hello, I want to add a shortcut button to the shell bot, click to run the following command: cd / root run ./gd.sh I don't know what kind of code should be added to the shell bot, can you help me?

hrvstr commented 4 years ago

put your command into a script or alias and call that.

cgkings commented 4 years ago

将您的命令放入脚本或别名中,然后调用它。

I don’t understand the code too much. Could you please be more specific?

hrvstr commented 4 years ago

Looking at your command, it's already a script. No need to cd into the directory first.

// bot command = "/gd"
bot.command("gd", function (msg, reply, next) {

  if (msg.context.command) {
    var command = msg.context.command;
    return reply.text("A command is already running.");
  }

  if (msg.editor) msg.editor.detach();
  msg.editor = null;

// command that that should be used
  var args = "./root/gd.sh";
  msg.context.command = new Command(reply, msg.context, args);
  msg.context.command.on("exit", function() {
    msg.context.command = null;
  });
});
cgkings commented 4 years ago

查看您的命令,它已经是一个脚本。无需先cd进入目录。

// bot命令=“ / gd” 
bot 。命令(“ gd” , 功能 (消息, 回复, 下一个) {

  如果 (MSG 。上下文。命令) { 
    VAR  命令 =  MSG 。情境。命令; 
    返回 回复。文本(“命令已在运行。” );
  }

  如果 (MSG 。编辑) 味精。编辑。分离(); 
  味精。编辑器 = null ;

//应该使用的命令
  var  args  =  “ ./root/gd.sh” ; 
  味精。情境。命令 =  新 命令(答复, 味精。上下文, ARGS ); 
  味精。情境。命令。上(“退出” , 函数() { 
    MSG 。上下文。命令 =空; 
  } ); 
} );

Thank you very much. I didn’t expect you to reply to me so quickly. Thank you for your code. I need to copy this code to which file in the shellbot folder? In addition, my script needs to be interactive and needs to be input after running In the next piece of information, you must reply with / r in the shellbot. How can I omit “/ r” and reply directly? In addition, is your TG number convenient to tell me, thank you very much, my tg number is https://t.me/cg_king b

hrvstr commented 4 years ago

The snippet should be place inside server.js right BEFORE the invalid command part.

bot.command(function (msg, reply, next) {
  reply.reply(msg).text("Invalid command.");
});

You can get an interactive shell with the command /setinteractive yes. To send input to a running command you don't need /r <command> you can just reply the the message.

Bildschirmfoto 2020-05-26 um 17 37 39

Let's keep the discussions on GitHub so others can benefit from them aswell.

mildsunrise commented 4 years ago

thank you :)

cgkings commented 4 years ago

thank you :)

How to add the new custom command to the start button?

cgkings commented 4 years ago

The snippet should be place inside server.js right BEFORE the invalid command part.

bot.command(function (msg, reply, next) {
  reply.reply(msg).text("Invalid command.");
});

You can get an interactive shell with the command /setinteractive yes. To send input to a running command you don't need /r <command> you can just reply the the message.

Bildschirmfoto 2020-05-26 um 17 37 39

Let's keep the discussions on GitHub so others can benefit from them aswell.

Sorry, but to trouble you again, my sh script is a one-click dump script that uses rclone's google driver. After execution, it returns a lot of information. How can I set it to return only the specified information?

hrvstr commented 4 years ago

How to add the new custom command to the start button?

From the blog post

... it’s a good idea to talk to the BotFather and say /setcommands to define a list of commands your bot accepts. You’ll find this list in commands.txt; just paste the contents when asked.


... my sh script is a one-click dump script that uses rclone's google driver. After execution, it returns a lot of information. How can I set it to return only the specified information?

Not related to shell-bot but you could use sed grep or awk. Google is your friend here with the keywords 'filter command output'.