M
M
magpro2019-04-21 16:56:15
Node.js
magpro, 2019-04-21 16:56:15

How to submit questions and use answers on node telegram Bot api?

Can't figure out how to receive and send serial messages in node telegram Bot api?
Example.
The bot asks a question: To whom do you want to write?
The user sends a response with the user ID: The
bot remembers the Id and says, such a user has been found, what should I write to him?
The user sends a message.
The bot receives a message and then sends the message to the recipient.
Actually here is the code:

if (result==='peredat'){
            bot.sendMessage(chatId,'Отправьте мне ID получателя:');
            bot.on('message', (msg) => {
            const chatId = msg.chat.id
            conn.query("SELECT * FROM `users` WHERE `id_tlg`="+msg.text, function(err, results){
              console.log(results[0])
              if (results[0].user_name != 'undefined'){
                var nick=results[0].user_name
              } else {
                var nick=results[0].first_name
              }
conn.query("SELECT * FROM `users` WHERE `id_tlg`="+chatId, function(err, results2){
              var option = {
       "parse_mode": "HTML",
   };
              bot.sendMessage(chatId,'Сколько баллов отправить пользователю с ID=<b>'+results[0].id+' ('+nick+')</b>?\nВы можете отправить не более <b>'+results2[0].ball+'</b> баллов',option);

              bot.on('message', (msg) => {
                const chatId = msg.chat.id
                console.log(msg.text)
              })

            })


            });

          })




     };
   })

The meaning of sending points

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gololobov, 2019-04-21
@dGololobov

Look towards telegraf.js . Personally, I liked this one better.
But not the point.
You need to correctly determine what you want to get. At what point will the bot ask the user "to whom to write?". etc. questions. What you want to do is very simple. You need to "listen" to the user's messages. Carry out some actions with them and send responses either to the user himself or to the addressee.

bot.onText(/^[0-9]$/, (msg, match) => {

  const resp = match[1];  // полученный от пользователя id
 /*
   Тут он ищет id по базе
*/
  // Отправляем ответ пользователю
  bot.sendMessage(msg.chat.id, resp); // Таким же образом можно пересылать текст в чат с другим пользователем
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question