Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed addHandler() and removeHandler() #267

Closed
wants to merge 3 commits into from

Conversation

lifestreamy
Copy link

Changed HandlersDsl.kt to account for changes in addHandler

You can now add handlers in dispatcher specifying their names, every handler is added in a mutableMap with its name as a key. If no name is specified then it is either defaulted to null or to main parameter of handler

@lifestreamy
Copy link
Author

This commit is made to address this issue that I have posted recently
#262

You can now add handlers in dispatcher specifying their names, every handler is added in a mutableMap with its name as a key. If no name is specified then it is either defaulted to null or to main parameter of handler
@vjgarciag96
Copy link
Member

Thanks for the contribution @lifestreamy! This will take me some time to review as I'll have to thoroughly go through your proposal and think about it because it's changing a very core part of the library and I'm not sure this is the behaviour we want, and in case we want it, I'll need to check whether this is the way we'd want to implement it. I'll add a suggestion on your issue on how you could solve the issue without changing the library in the meantime if that works for you.

@lifestreamy
Copy link
Author

Alright. Thank you for the answer.

@lifestreamy
Copy link
Author

lifestreamy commented Oct 19, 2022

And yes, it probably is not the best way to solve the problem and it could be improved. But it works for me for now, although I don't really like some pieces in my code.

So it is more of a suggestion to get attention rather than an actual pull request.

@lifestreamy
Copy link
Author

Some clarification: in addHandler() it is written so that it does not replace existing handler with the same one (same id and type). If types are different it replaces the handler previously added under the same id

@lifestreamy
Copy link
Author

Also it would be cool to be able to add
a nested handler like this

callbackQuery("cb1", chatId){}

This way it will be only added to the selected chat

And, to avoid cluttering, I think it is alright to restart the backend periodically,
but it is probably possible (and would be better) to improve the library so that you could just reset the bot every select interval.

Also, there is a problem with dispatcher that you can't just send message every time the bot starts or on some backend event (like when there is an update a message will be sent to every person using the bot with the news)

Currently I have to manually send such messages using telegram API directly.

Like when the bot is started, it sends only to my own chat "Backend started"

I can't just write bot.sendMessage in dispatcher, it is not allowed by the library.

Right now the bot can only send messages from inside a handler block.

@vjgarciag96
Copy link
Member

vjgarciag96 commented Oct 19, 2022

I take note of all the feedback. The suggestion around making it easier to add/remove handlers, and adding the same handler several times will be in the roadmap for new features of the library. The handler per chat id, we'll have to give that a thought and see whether we want that or not. And the ability to reset the bot sounds like a good idea too.

Also, there is a problem with dispatcher that you can't just send message every time the bot starts or on some backend event (like when there is an update a message will be sent to every person using the bot with the news)
Currently I have to manually send such messages using telegram API directly.
Like when the bot is started, it sends only to my own chat "Backend started"
I can't just write bot.sendMessage in dispatcher, it is not allowed by the library.
Right now the bot can only send messages from inside a handler block.

The dispatcher block is not the place where you want to use the bot as it should only be used to define handlers for updates or errors. If you want to send a message when you start a bot you can do something like the next:

fun main() {
    val bot = bot {
        dispatcher {
            // whatever goes here
        }    
    }
    bot.startPolling()
    bot.sendMessage(ANY_CHAT_ID, ANY_MESSAGE)
}

@lifestreamy
Copy link
Author

Thank you for your replies! It is great that you answer and care about your product, keep up the good work! Meanwhile I will use my fork. And yes, I haven't run any tests before making pull request. Whoops lol

@lifestreamy
Copy link
Author

https://github.com/lifestreamy/kotlin-telegram-bot/blob/main/README.md
Please, check this out.

Again, just to get some attention to the issue.

I can (and will) delete this pull request on your demand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants