node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

best practice for manage user concurrency

Open pani94 opened this issue 6 years ago • 3 comments

Hello everybody, I'm new to telegram and to node.js. I'm doing a bot in which a user can notify and event that will be store in a mongo database.Every user talk individually with the bot. I use bot.ontext and bot.on('message') for get user message but if more than what user is using the bot will response to any of them. For example here after I get a location I want to add to the alert(a notify of some event) the event.But when the bot get the location, the bot.once('message') will listen any message sent from any user.

bot.on("location", function (msg) {
      
 bot.sendMessage(msg.chat.id,"Location get.Insert the event that has occured", alert2_kb).then(() =>
 {

              var longitude = msg.location.longitude;
              var latitude = msg.location.latitude; 
              var Alert = require("./models/alert");
              alert = new Alert({date:new Date(),timestamp: Date.now(), chatId: msg.chat.id, latitude: latitude, longitude: longitude});
              bot.once("message", function(answer) {
                  switch(answer.text){
                    case "Problem 1":{
                      alert.type = 1;
                      break;
                    }
                    case "Problem 2":{
                      alert.type = 2;
                      break;
                    }case "Problem 3":{
                      alert.type = 3;
                      break;
                    }
                    default:break;
                  }

                  if(alert.type){
                    alert.save(function (err, alert) {
                      if (err) throw err
                      console.log("Saved to DB");
                      });
                      bot.sendMessage(receiver, "Alert gets successfully.", start_keyboard);
                  }
                  
                  
              })
          })
        }
    
  })

pani94 avatar Jul 21 '18 08:07 pani94

I have the same problem! How can we listen to a specific user just like we send a message to a specific one referring to their message.chat.id?

aner87 avatar Jul 23 '18 16:07 aner87

@pani94 Your answer is not clear for me, you should send lots of messages to a list of users that have subscribed to your bot?

sidelux avatar Jul 31 '18 18:07 sidelux

@pani94 Could you fix the broken markdown (code) in your issue/question?

kamikazechaser avatar Aug 01 '18 16:08 kamikazechaser