-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+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
1 parent
8beb70e
commit b077e63
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |