Closed m0x61h0x64i closed 1 year ago
@yagop please help
Hello, Can you give an example of what you want to do?
@VDS13 i want to check if the input is coming from a user that wanted to fill out phone number input then do the next step, but now when i console log msg.text under the bot.on('message'... it will receive any user input but how can i detect if the user that sent a number is the one that Im waiting for his/her number ?
I would suggest to use some king of "user states". I.e. when you prompt a user to enter phone number, you can set state that user with certain chatId (you can get chatId from .on('message')) is in state "entering_phone". So when next time the you get the message, you can check whether user with this chatId is sending you a phone number.
Here is basic example:
const userStates = {}
bot.on('message', (msg) => {
const chatId = msg.chat.id
if ( !userStates[chatId] ) {
// let's assume empty state means you need to prompt user for phone number
// send user prompt message and set user state
bot.sendMessage( chatId, "Please enter your phone number")
userStates[chatId] = "entering_phone"
} else if ( userStates[chatId] == "entering_phone") {
// based on "entering_phone" state, you know that user with this id is sending you phone number
// do what you need with the number
const phoneNumber = msg.text
// also don't forget to reset user state, so the same user can ask you for something else
userStates[chatId] = null
}
})
@derzhavets thanks a lot, was helpful
Hi @derzhavets Are you also using new_chat_members? Is it working for you? Cheers
i want to listen for a specific user messages, bot.on('message', ...) this will listen for all users messages!
i only want to get user input, is there a better way to do this ?