yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.43k stars 1.53k forks source link

How to get text after command is sent to the bot #954

Closed realrecordzLab closed 2 years ago

realrecordzLab commented 2 years ago

I have created a simple bot that is able to send and receive files. I need a way to get a password for the files that are sent to the bot from the users but at the moment I don't know how to proceed, I'm only able to get the commands but how I will get only the text that is following the command?

At the monent I have this three commands in my code. It will be more easy to get the password for the files before thy are sent.

bot.on('message', async (msg) => {

  console.log(msg);
  if( msg.entities && msg.entities[0].type === 'bot_command' && msg.text === '/encrypt'){
    bot.sendMessage(msg.chat.id, 'Please send me the password you want to use and then the file to encrypt');
  }
  if( msg.entities && msg.entities[0].type === 'bot_command' && msg.text === '/decrypt'){
    bot.sendMessage(msg.chat.id, 'Please send me the file you want to decrypt and in a separate message the password you have used to encrypt the file.');
  }
  if( msg.entities && msg.entities[0].type === 'bot_command' && msg.text === '/password'){
    password = msg.text;
  }

});
danielperez9430 commented 2 years ago

You can use better that bot.on('message') that is a global event "onText". Example for "/encrypt" command:

// Matches "/encrypt [your text]"
bot.onText(/\/encrypt (.+)/, (msg, match) => {
  // 'match' is the result of executing the regexp above on the text content of the message

  const chatId = msg.chat.id;
  const resp = match[1]; // the captured "your text"

  bot.sendMessage(chatId, resp);
});