Skip to content

Commit

Permalink
updated Bot long polling loop
Browse files Browse the repository at this point in the history
- there is no need to check twice for m_running
- Improve the way we check if message is Command
  • Loading branch information
baderouaich committed Oct 10, 2023
1 parent f7665da commit 9c30eed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
43 changes: 13 additions & 30 deletions src/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ void Bot::start() {
/// Callback -> onStart
this->onStart();

/// Long Poll
while (m_running) {
// Dispatch updates
// Get updates from Telegram (any new events such as messages, commands, files, ...)
m_updates = m_api->getUpdates(/*offset=*/m_lastUpdateId);
// Dispatch updates to callbacks (onCommand, onAnyMessage, onPoll, ...)
for (const Ptr<Update>& update: m_updates) {
if (update->updateId >= m_lastUpdateId)
if (update->updateId >= m_lastUpdateId) {
m_lastUpdateId = update->updateId + 1;
this->dispatch(update);
}

if (m_running) {
// Confirm dispatched updates
m_updates = m_api->getUpdates(/*offset=*/m_lastUpdateId);
} else {
// Confirm last updates before stopping, timeout = 0
// Requesting new updates from Telegram server lets the server know that you handled the previous updates,
// so it will not send us duplicate updates.
m_updates = m_api->getUpdates(/*offset=*/m_lastUpdateId, /*limit=*/100, /*timeout=*/0);
this->dispatch(update);
}
}
}
}
Expand Down Expand Up @@ -120,23 +114,12 @@ void Bot::dispatchMessage(const Ptr<Message>& message) {
/// Callback -> onAnyMessage
this->onAnyMessage(message);

/// Command ?
if (StringUtils::startsWith(message->text, "/")) {
std::size_t splitPosition;
std::size_t spacePosition = message->text.find(' ');
std::size_t atSymbolPosition = message->text.find('@');
if (spacePosition == std::string::npos) {
if (atSymbolPosition == std::string::npos) {
splitPosition = message->text.size();
} else {
splitPosition = atSymbolPosition;
}
} else if (atSymbolPosition == std::string::npos) {
splitPosition = spacePosition;
} else {
splitPosition = (std::min)(spacePosition, atSymbolPosition);
}
std::string command = message->text.substr(1, splitPosition - 1);
/// Is this message a Command ? (starts with '/' character)
if (not message->text.empty() and message->text[0] == '/') {
std::size_t firstSpacePos = message->text.find_first_of(" \t\n\r");
if (firstSpacePos == std::string::npos)
firstSpacePos = message->text.size();
std::string command = message->text.substr(1, firstSpacePos - 1);
std::vector<Ptr<BotCommand>> myCommands = m_api->getMyCommands();
bool isKnownCommand = std::any_of(myCommands.begin(), myCommands.end(), [&command](const Ptr<BotCommand>& cmd) noexcept {
return cmd->command == command;
Expand Down
2 changes: 1 addition & 1 deletion tests/manual_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MyBot : public Bot {
/// Called when Bot is about to be stopped (triggered by Bot::stop())
/// Cleanup your code in this callback (close handles, backup data...)
void onStop() override {
std::cout << __func__ << ": " << getApi()->getMe()->firstName << " bot stopped." << std::endl;
std::cout << __func__ << ": " << getApi()->getMe()->firstName << " bot stopping..." << std::endl;
}

/// Called when a new message is received of any kind - text, photo, sticker, etc.
Expand Down

0 comments on commit 9c30eed

Please sign in to comment.