Skip to content

crastinus/tgbot-cpp

 
 

Repository files navigation

tgbot-cpp

Build Status Docker Hub pulls GitHub contributors

Contact reo7sp Join the chat at https://gitter.im/reo7sp/tgbot-cpp

Donate using PayPal Donate using Yandex.Money

C++ library for Telegram bot API.

Documentation is located here.

Forks

The largest fork of this library is https://github.com/foxcpp/tgbot-cpp. There is a development of payment and video notes APIs and more.

Library compilation

Firstly you need to install some dependencies such as Boost and build tools such as CMake. On Debian-based distibutives you can do it with these commands:

sudo apt-get install g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev

To compile the library execute this commands:

cd /path/where/you/have/cloned/the/library/repository
cmake .
make -j4
sudo make install

Bot compilation

With CMake

Example CMakeLists.txt

Without CMake

g++ telegram_bot.cpp -o telegram_bot --std=c++11 -I/usr/local/include -lTgBot -lboost_system -lboost_iostreams -lssl -lcrypto -lpthread

You can use Docker to build and run your bot. Set the base image of your's Dockerfile to reo7sp/tgbot-cpp.

Samples

Simple echo bot which sends everything it receives:

#include <stdio.h>
#include <tgbot/tgbot.h>

int main() {
    TgBot::Bot bot("PLACE YOUR TOKEN HERE");
    bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
        bot.getApi().sendMessage(message->chat->id, "Hi!");
    });
    bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
        printf("User wrote %s\n", message->text.c_str());
        if (StringTools::startsWith(message->text, "/start")) {
            return;
        }
        bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
    });
    try {
        printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
        TgBot::TgLongPoll longPoll(bot);
        while (true) {
            printf("Long poll started\n");
            longPoll.start();
        }
    } catch (TgBot::TgException& e) {
        printf("error: %s\n", e.what());
    }
    return 0;
}

All samples are located here.

Feedback

Feel free to create new issues on GitHub or contact me on Telegram.

Licence

The MIT License.

Fork tips

Created for compability with visual studio 2015-2017.

Configure project like (change path to your boost root/libs directory)

cmake . -DBOOST_ROOT="D:\boost\boost_1_59_0" -DBoost_LIBRARY_DIRS="D:\boost\boost_1_59_0\x86_64\lib"

About

C++ library for Telegram bot API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 99.3%
  • Other 0.7%