Skip to content

Commit

Permalink
added sendVideo and receiveVideo examples
Browse files Browse the repository at this point in the history
  • Loading branch information
baderouaich committed Oct 7, 2023
1 parent 1643d36 commit e4f55e8
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 8 deletions.
4 changes: 1 addition & 3 deletions examples/receiveAudio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ FetchContent_Declare(tgbotxx
FetchContent_MakeAvailable(tgbotxx)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)

add_compile_definitions(PHOTOS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/photos")
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)
4 changes: 1 addition & 3 deletions examples/receiveDocument/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ FetchContent_Declare(tgbotxx
FetchContent_MakeAvailable(tgbotxx)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)

add_compile_definitions(PHOTOS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/photos")
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)
2 changes: 0 additions & 2 deletions examples/receivePhoto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ FetchContent_MakeAvailable(tgbotxx)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)

add_compile_definitions(PHOTOS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/photos")
15 changes: 15 additions & 0 deletions examples/receiveVideo/CMakeLists.txt
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)
18 changes: 18 additions & 0 deletions examples/receiveVideo/README.md
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.
58 changes: 58 additions & 0 deletions examples/receiveVideo/main.cpp
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;
}
17 changes: 17 additions & 0 deletions examples/sendVideo/CMakeLists.txt
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")
16 changes: 16 additions & 0 deletions examples/sendVideo/README.md
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.
79 changes: 79 additions & 0 deletions examples/sendVideo/main.cpp
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 added examples/sendVideo/videos/video.mp4
Binary file not shown.

0 comments on commit e4f55e8

Please sign in to comment.