Skip to content

Commit

Permalink
added echo_bot_submodule example
Browse files Browse the repository at this point in the history
+You can also use this library as a submodule in your bot project without the need of installing it in your system.
  • Loading branch information
baderouaich committed Sep 30, 2023
1 parent 8beb70e commit b077e63
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,36 @@ cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
sudo make install
```

#### Windows
TODO
#### macOS
TODO


## Use tgbotxx as a project submodule (without installation)
You can also use this library as a submodule in your bot project without the need of installing it in your system.
Use git clone or git submodule add the library:
```shell
git submodule add https://github.com/baderouaich/tgbotxx ./lib/tgbotxx
```
or
```shell
git clone https://github.com/baderouaich/tgbotxx ./lib/tgbotxx
```

Then add `add_subdirectory(lib/tgbotxx)` in your `CMakeLists.txt`.
```cmake
cmake_minimum_required(VERSION 3.10)
project(my_bot_project)
add_subdirectory(lib/tgbotxx) # <-- clone tgbotxx in your lib/ directory
include_directories(lib/tgbotxx/include) # <-- include tgbotxx/ headers
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx) # <-- link to tgbotxx
```

### Ref:
[Telegram Api Documentation](https://core.telegram.org/bots/api)

Expand Down
8 changes: 8 additions & 0 deletions examples/echo_bot_submodule/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.10)
project(echo_bot_submodule)

add_subdirectory(lib/tgbotxx) # <-- clone tgbotxx in your lib/ directory
include_directories(lib/tgbotxx/include) # <-- include tgbotxx/ headers

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx) # <-- link to tgbotxx
31 changes: 31 additions & 0 deletions examples/echo_bot_submodule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## echo_bot_submodule
This example shows how to use tgbotxx as a submodule to program a Telegram Bot that echoes your messages back.

## Steps
1. git clone or git submodule add the library in your project libraries directory:
```shell
git submodule add https://github.com/baderouaich/tgbotxx ./lib/tgbotxx
```
or
```shell
git clone https://github.com/baderouaich/tgbotxx ./lib/tgbotxx
```

2. Then add `add_subdirectory(lib/tgbotxx)` in your `CMakeLists.txt`.
```cmake
cmake_minimum_required(VERSION 3.10)
project(my_bot_project)
add_subdirectory(lib/tgbotxx) # <-- clone tgbotxx in your lib/ directory
include_directories(lib/tgbotxx/include) # <-- include tgbotxx/ headers
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx) # <-- link to tgbotxx
```

3. Build your Bot:
```shell
mkdir build && cd build
cmake ..
make
```
67 changes: 67 additions & 0 deletions examples/echo_bot_submodule/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <tgbotxx/tgbotxx.hpp>
#include <iostream>
using namespace tgbotxx;

class EchoBot : public Bot {
public:
EchoBot() : Bot("BOT_TOKEN_FROM_BOTH_FATHER") { }

private:
/// Called before Bot starts receiving updates (triggered by Bot::start())
/// Use this callback to initialize your code, set commands..
void onStart() override {
// Drop awaiting updates (when Bot is not running, updates will remain 24 hours
// in Telegram server before they get deleted or retrieved by BOT)
getApi()->deleteWebhook(true);
std::cout << "Bot " << getApi()->getMe()->firstName << " started\n";
}

/// Called when Bot is about to be stopped (triggered by Bot::stop())
void onStop() override {
/// Cleanup your code in this callback (close handles, backup data...)
std::cout << "Bot " << getApi()->getMe()->firstName << " stopped\n";
}

/// Called when a new message is received of any kind - text, photo, sticker, etc.
void onAnyMessage(const Ptr<Message>& message) override {
std::cout << "Received " << message->text << " from " << message->from->username << std::endl;
getApi()->sendMessage(message->chat->id, message->text); // Echo back message
}

/// ============ [OPTIONAL OVERLOAD] =============
/// Called when a new command is received (messages with leading '/' char).
void onCommand(const Ptr<Message>& message) override {}
/// Called when a non-command message is received of any kind - text, photo, sticker, etc.
void onNonCommandMessage(const Ptr<Message> &message) override {}
/// Called when an unknown command is received (messages with leading '/' char).
/// Known commands are set with Bot::setCommands()
void onUnknownCommand(const Ptr<Message> &message) override {}
/// Called when a new version of a message that is known to the bot and was edited
void onEditedMessage(const Ptr<Message>& editedMessage) override {}
/// Called when a new incoming inline query is received
void onInlineQuery(const Ptr<InlineQuery>& inlineQuery) override {}
/// Called when the result of an inline query that was chosen by a user and sent to their chat partner.
void onChosenInlineResult(const Ptr<ChosenInlineResult>& chosenInlineResult) override {}
/// Called when a new incoming callback query is received
void onCallbackQuery(const Ptr<CallbackQuery>& callbackQuery) override {}
/// Called when a new incoming shipping query is received.
void onShippingQuery(const Ptr<ShippingQuery>& shippingQuery) override {}
/// Called when a new incoming pre-checkout query is received. Contains full information about checkout
void onPreCheckoutQuery(const Ptr<PreCheckoutQuery>& preCheckoutQuery) override {}
/// Called when a new poll state is received.
void onPoll(const Ptr<Poll>& poll) override {}
/// Called when a user changed their answer in a non-anonymous poll.
void onPollAnswer(const Ptr<PollAnswer>& pollAnswer) override {}
/// Called when the bot's chat member status was updated in a chat.
void onMyChatMember(const Ptr<ChatMemberUpdated>& myChatMemberUpdated) override {}
/// Called when a chat member's status was updated in a chat.
void onChatMember(const Ptr<ChatMemberUpdated>& chatMemberUpdated) override {}
/// Called when a A request to join the chat has been sent.
void onChatJoinRequest(const Ptr<ChatJoinRequest>& chatJoinRequest) override {}
};

int main() {
EchoBot bot;
bot.start();
return EXIT_SUCCESS;
}

0 comments on commit b077e63

Please sign in to comment.