0

I have a question regarding the development of a Telegram bot that involves sending the nearest location to a user. I'm facing an issue with handling user locations.

Here's the scenario: When users press the "nearest loc" button, they are prompted to share their location. The problem arises when two users are in this step simultaneously. If the first user shares their location, the bot receives the coordinates and processes the function location.getNearestLocation(bot, latitude, longitude, chatId) for both users. As a result, the bot mistakenly assumes that the received location is from both users.

To provide some context, below is the code snippet that shows the specific input handling:

I would greatly appreciate any insights or suggestions on how to approach this issue correctly.

// Handle /start command
bot.onText(/\/start/, (msg) => {
  const chatId = msg.chat.id;
  const firstName = msg.from.first_name;
  const lastName = msg.from.last_name;
  const username = msg.from.username;
  const phoneNumber = msg.contact ? msg.contact.phone_number : '';
  const userType = 'user'; // Set the default user type
  
  // Register user in the database
  db.registerUser(chatId, firstName, lastName, username, phoneNumber, userType)
    .then(() => {

      // Create and send the custom keyboard
      const keyboard = {
        keyboard: [
          ['Nearest loc'],
          ['Comment' , 'Disclaimer'],
          [ 'Register loc']
        ],
        resize_keyboard: true
      };
  
      bot.sendMessage(chatId, `Welcome ${msg.from.first_name}. Please select an option:`, {
        reply_markup: keyboard
      });
    })
    .catch((error) => {
      console.error('An error occurred while registering the user:', error);
      bot.sendMessage(chatId, 'Sorry, there was an error during registration. Please try again later.');
    });
});


// Handle user input
bot.onText(/(.+)/, (msg, match) => {
  const chatId = msg.chat.id;
  const userInput = match[1];
  const sendForceReply = (chatId, message) => {
    bot.sendMessage(chatId, message, {
      reply_markup: {
        force_reply: true
      }
    });
  };

  // Process the user input based on the selected option
  switch (userInput) {
    case 'Nearest loc':
      const option = {
        parse_mode: 'Markdown',
        reply_markup: {
          keyboard: [
            [
              {
                text: 'Share Location',
                request_location: true
              }
            ],
            ['No']
          ],
          resize_keyboard: true,
          one_time_keyboard: true
        }
      };

      bot.sendMessage(chatId, 'Please share your location:', option)
        .then(() => {
          bot.once('location', (msg) => {
            const latitude = msg.location.latitude;
            const longitude = msg.location.longitude;
            location.getNearestLocation(bot, latitude, longitude, chatId);
          });
        })
        .catch((error) => {
          console.error('An error occurred while requesting the location:', error);
          bot.sendMessage(chatId, 'Sorry, there was an error. Please try again later.');
        });
      break;

I need a perfect multiple user input handling without conflict

Anwar
  • 1
  • 1

0 Answers0