-
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.
added sendVideo and receiveVideo examples
- Loading branch information
1 parent
1643d36
commit e4f55e8
Showing
10 changed files
with
205 additions
and
8 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
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,15 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(receive_video_bot) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare(tgbotxx | ||
GIT_REPOSITORY "https://github.com/baderouaich/tgbotxx" | ||
GIT_TAG main | ||
) | ||
FetchContent_MakeAvailable(tgbotxx) | ||
|
||
add_executable(${PROJECT_NAME} main.cpp) | ||
target_link_libraries(${PROJECT_NAME} PUBLIC 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,18 @@ | ||
## Receive video | ||
This example shows how to program a Telegram Bot that will receive videos from users | ||
using the `Api::getFile()` and `Api::downloadFile()` api methods. | ||
|
||
|
||
### Run | ||
```bash | ||
mkdir build && cd build | ||
cmake .. -DCMAKE_BUILD_TYPE=Release | ||
make -j8 | ||
./photo_bot YOUR_BOT_TOKEN | ||
``` | ||
|
||
### How to create a new Bot and obtain its private token ? | ||
1. Open the Telegram mobile app and search BotFather | ||
2. Send BotFather a command /newbot | ||
3. Follow instructions to create a new Bot | ||
4. After you finish the instructions, you will receive a Bot Token, make sure you keep it secured. |
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,58 @@ | ||
#include <csignal> | ||
#include <iostream> | ||
#include <tgbotxx/tgbotxx.hpp> | ||
using namespace tgbotxx; | ||
#include <filesystem> | ||
namespace fs = std::filesystem; | ||
|
||
class ReceiveVideoBot : public Bot { | ||
public: | ||
ReceiveVideoBot(const std::string& token) : Bot(token) {} | ||
|
||
private: | ||
void onStart() override { | ||
std::cout << "Bot " << api()->getMe()->username << " Started\n"; | ||
} | ||
|
||
void onAnyMessage(const Ptr<Message>& message) override { | ||
if (message->video) { | ||
// Download video bytes from Telegram | ||
Ptr<File> videoFile = api()->getFile(message->video->fileId); | ||
std::string bytes = api()->downloadFile(videoFile->filePath, [](auto downloadTotal, auto downloadNow) -> bool { | ||
std::cout << "Downloading video " << downloadNow << "/" <<downloadTotal << " bytes..." <<std::endl; | ||
return true; | ||
}); | ||
|
||
// Save video to a file | ||
fs::path filename = fs::path(videoFile->filePath).filename(); | ||
std::ofstream ofs{filename, std::ios::binary}; | ||
ofs << bytes; | ||
ofs.close(); | ||
|
||
std::cout << "Downloaded video to " << filename.string() << " (" << bytes.size() << " bytes)\n"; | ||
} | ||
} | ||
|
||
void onStop() override { | ||
std::cout << "Bot " << api()->getMe()->username << " Stopped\n"; | ||
} | ||
}; | ||
|
||
|
||
int main(int argc, const char *argv[]) { | ||
if (argc < 2) { | ||
std::cerr << "Usage:\nreceive_video_bot \"BOT_TOKEN\"\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
static std::unique_ptr<ReceiveVideoBot> BOT(new ReceiveVideoBot(argv[1])); | ||
std::signal(SIGINT, [](int) { // Graceful Bot exit on CTRL+C | ||
if (BOT) { | ||
std::cout << "Stopping Bot. Please wait...\n"; | ||
BOT->stop(); | ||
} | ||
std::exit(EXIT_SUCCESS); | ||
}); | ||
BOT->start(); | ||
return EXIT_SUCCESS; | ||
} |
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,17 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(send_video_bot) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare(tgbotxx | ||
GIT_REPOSITORY "https://github.com/baderouaich/tgbotxx" | ||
GIT_TAG main | ||
) | ||
FetchContent_MakeAvailable(tgbotxx) | ||
|
||
add_executable(${PROJECT_NAME} main.cpp) | ||
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx) | ||
|
||
add_compile_definitions(VIDEOS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/videos") |
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,16 @@ | ||
## Send photo | ||
This example shows how to program a Telegram Bot that will send users photos using the `Api::sendPhoto()` method. | ||
|
||
### Run | ||
```bash | ||
mkdir build && cd build | ||
cmake .. -DCMAKE_BUILD_TYPE=Release | ||
make -j8 | ||
./photo_bot YOUR_BOT_TOKEN | ||
``` | ||
|
||
### How to create a new Bot and obtain its private token ? | ||
1. Open the Telegram mobile app and search BotFather | ||
2. Send BotFather a command /newbot | ||
3. Follow instructions to create a new Bot | ||
4. After you finish the instructions, you will receive a Bot Token, make sure you keep it secured. |
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,79 @@ | ||
#include <tgbotxx/tgbotxx.hpp> | ||
#include <cpr/cpr.h> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <csignal> | ||
#include <array> | ||
using namespace tgbotxx; | ||
|
||
class VideoBot : public Bot { | ||
public: | ||
VideoBot(const std::string &token) : Bot(token) {} | ||
|
||
private: | ||
void onStart() override { | ||
// Drop pending updates | ||
api()->deleteWebhook(true); | ||
|
||
// Register my commands | ||
Ptr<BotCommand> localVideoCmd(new BotCommand()); | ||
localVideoCmd->command = "/local_video"; | ||
localVideoCmd->description = "Get video from bot videos/ directory"; | ||
Ptr<BotCommand> networkVideoCmd(new BotCommand()); | ||
networkVideoCmd->command = "/network_video"; | ||
networkVideoCmd->description = "Get video from the internet (URL)"; | ||
api()->setMyCommands({localVideoCmd, networkVideoCmd}); | ||
|
||
std::cout << "Bot " << api()->getMe()->username << " Started\n"; | ||
std::srand(std::time(nullptr)); | ||
} | ||
|
||
void onCommand(const Ptr<Message> &message) override | ||
{ | ||
if (message->text == "/local_video") | ||
{ | ||
cpr::File video(std::string(VIDEOS_DIR) + "/video.mp4"); | ||
api()->sendVideo(message->chat->id, video); | ||
} | ||
else if (message->text == "/network_video") | ||
{ | ||
static std::array<std::string, 6> imageUrls = { | ||
// Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future. | ||
"https://www.pexels.com/download/video/5752729/?fps=30.0&h=240&w=426", | ||
"https://www.pexels.com/download/video/3209828/?fps=25.0&h=240&w=426", | ||
"https://www.pexels.com/download/video/5532765/?fps=25.0&h=426&w=226", | ||
"https://www.pexels.com/download/video/5532771/?fps=25.0&h=426&w=226", | ||
"https://www.pexels.com/download/video/5722113/?fps=25.0&h=426&w=226", | ||
"https://www.pexels.com/download/video/7220850/?fps=25.0&h=426&w=226" | ||
}; | ||
std::string randomVideoUrl = imageUrls[std::rand() % imageUrls.size()]; | ||
std::cout << randomVideoUrl << std::endl; | ||
api()->sendVideo(message->chat->id, randomVideoUrl); | ||
} | ||
} | ||
|
||
void onStop() override | ||
{ | ||
std::cout << "Bot " << api()->getMe()->username << " Stopped\n"; | ||
} | ||
|
||
}; | ||
|
||
|
||
int main(int argc, const char *argv[]) { | ||
if (argc < 2) { | ||
std::cerr << "Usage:\nsend_video_bot \"BOT_TOKEN\"\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
static std::unique_ptr<VideoBot> BOT(new VideoBot(argv[1])); | ||
std::signal(SIGINT, [](int) { // Graceful Bot exit on CTRL+C | ||
if(BOT) { | ||
std::cout << "Stopping Bot. Please wait...\n"; | ||
BOT->stop(); | ||
} | ||
std::exit(EXIT_SUCCESS); | ||
}); | ||
BOT->start(); | ||
return EXIT_SUCCESS; | ||
} |
Binary file not shown.