FSM #306
FSM
#306
|
Hello everyone. |
Answered by
reo7sp
Sep 14, 2026
Replies: 1 comment
|
tgbot-cpp does not include an FSM like aiogram. Keep the state in the application, keyed by chat or user ID: enum class State { Idle, WaitingForName };
std::unordered_map<std::int64_t, State> states;
bot.getEvents().onCommand("signup", [&](TgBot::Message::Ptr message) {
states[message->chat->id] = State::WaitingForName;
bot.getApi().sendMessage(message->chat->id, "Enter your name");
});
bot.getEvents().onNonCommandMessage([&](TgBot::Message::Ptr message) {
if (states[message->chat->id] != State::WaitingForName) {
return;
}
const auto name = message->text.value_or("");
states.erase(message->chat->id);
bot.getApi().sendMessage(message->chat->id, "Registered: " + name);
});Use a database if the state must survive restarts. Protect the map with a mutex when updates can be handled concurrently, for example with a webhook server. |
0 replies
Answer selected by
reo7sp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tgbot-cpp does not include an FSM like aiogram. Keep the state in the application, keyed by chat or user ID: