Helixform / TeleGPT

An out-of-box ChatGPT bot for Telegram.
https://icystudio.github.io/TeleGPT/telegpt_core
MIT License
236 stars 15 forks source link

[EPIC] Dispatcher refactor #8

Closed unixzii closed 1 year ago

unixzii commented 1 year ago

A problem raised when we are working on Dall-E feature: We need the bot to enter a dialogue mode where the bot ask questions, and users can reply back to continue the painting. The current dispatcher will literally not support this operation, because the handlers are performed one-by-one in registration order. Registering DallE module earlier than Chat module seems to solve our problem, but that would just be a workaround.

The common usage of the bot is chatting, it's the default mode. However, sometimes, some other modules may want to respond to a message with higher priority temporarily. This is where Conversation comes in. It's similar to Dialogue in teloxide, but instead of single-typed state, we support multiple-typed state, and each type of state can provide different handlers. Back to the problem we issued before. When user want to use Dall-E, it can type let's say /paint, then the bot will enter a Dall-E painting conversation. User can answer the questions asked by bot in the conversation without being intercepted by Chat module. This is implemented by a handler ahead of regular module handlers.

As we are doing the refactor, we also refined how commands are handled. In the current implementation, command providing and handling are separated, which is not that scalable and maintainable. In the next version, it will also be unified.

Here is a overview of how updates are processed in the new dispatching pipeline:

[Updates] --> (Preflight Filter) --> (Conversation Handler)
                                 |-> (Command Handler)
                                 |-> (Module Filter Handler)
                                 |-> (Default Handler)

Legend: [Input]
        (Handler)
ktiays commented 1 year ago

LGTM