Bot sending duplicates
See original GitHub issueHello everyone! I’m a newcomer for Telegram bot programming and I’m not really sure if this is a bug or it’s me doing something wrong. If that’s the case forgive me!
I’m trying to create a bot which simply returns data from a json file, filtering and manipulating it. Each json has a tag called “level”, so I wrote a “search by level” callback query. Once is fired, this callback sends a message to ask the user to input the level (from 0 to 9). Then the bot simply reads the json and filters the output to send as a message.
The problem is that it sends duplicates while the callback is fired more than once. If you fire it 2 times, it will send 2 messages and the third time it will send 3 messages, and so on.
Here’s the code:
val holder = deserializeSpells("/home/nicola/Desktop/formattedFile.json")
val bot = bot {
dispatch {
command("start") {
bot.sendMessage(ChatId.fromId(update.message!!.chat.id),"<b><i>Benvenuto in WizardBook!</i></b>", parseMode = ParseMode.HTML)
bot.sendMessage(ChatId.fromId(update.message!!.chat.id),"Ti aiuterò a trovare gli incantesimi!")
val replyMarkup = InlineKeyboardMarkup.create(
listOf(
InlineKeyboardButton.CallbackData("Cerca per livello","bylevel"),
InlineKeyboardButton.CallbackData("Cerca per classe","byclass")),
listOf(
InlineKeyboardButton.CallbackData("Cerca per scuola","byschool"),
InlineKeyboardButton.CallbackData("Cerca per nome (o un pezzo)","byname")),
listOf(InlineKeyboardButton.CallbackData("Cerca per nome (preciso)","byreference")),
listOf(InlineKeyboardButton.CallbackData("La magia del giorno!","byday")),
listOf(InlineKeyboardButton.CallbackData("Contatti","bycontacts"))
)
bot.sendMessage(ChatId.fromId(message.chat.id),"Scegli il metodo di ricerca", replyMarkup = replyMarkup)
}
callbackQuery ("bylevel") {
val chatId = update.callbackQuery?.message!!.chat.id
bot.sendMessage(ChatId.fromId(chatId),"Che livello stai cercando? <u>(Da 0 a 9)</u>", parseMode = ParseMode.HTML)
text {
var out :String = ""
holder.forEach {
if (it.level == text) out+= "\n${it.name}\n<i><u>Lvl. ${it.level} Classi ${it.combat}</u></i>"
}
bot.sendMessage(ChatId.fromId(chatId),out, parseMode = ParseMode.HTML)
}
}
}
}
bot.startPolling()
Maybe it’s me not understanding how to properly handle updates? Thanks a lot!
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
Having a library like that would be awesome, one could build much more complex commands and queries. Up until that Christmas present though, I’ll try to work something around implementing one of the solutions you guys suggested and poke it around to see the behavior, maybe manually closing the text handler each time. Thanks a lot!
I think I’ve mistakenly mentioned the wrong user. Here we go @seik @vjgarciag96