Cppgram  1.0.0
Easy and modern C++14 Telegram Bot API wrapper
Reference

What is Cppgram

Cppgram is a lighweight framework for Telegram Bot API that provides support for the most important api methods and basic features a user need, ensuring speed and stability.

Usage

// Include the framework
#include "cppgram/cppgram.hpp"

// Create your custom Bot class
class MyBot : public cppgram::BasicBot<MyBot> {
    public:
    MyBot(string &token) : BasicBot(token, "MyBotName", this) {}

    MyBot(const MyBot &b) : BasicBot(b, this) {}
};

// Answer all messages received
void helloWorld(MyBot &bot,
            const cppgram::types::Message &message) {

    // sending a "Hello World" message
    bot.sendMessage("Hello World");
}

int main() {
    std::string token = "token";
    auto bot = MyBot(token);
    // Say the bot to answer all messages using our Hello World function
    bot.processMessage = &helloWorld;
    // Create a poll with 8 thread running
    auto poll = cppgram::Polling<MyBot>(bot, 8);
    poll.run();
}

Features

Install

To start using this library you can fork this repository. It is an example project configured with the minimal boilerplate you need to make this library works.

If you already have a project you can include this library using git submodules:

git submodule add git://github.com/DanySpin97/cppgram.git
git submodule update --init --recursive

Then add the directory in your cmake configuration:

add_subdirectory(cppgram)

Cppgram will be compiled, headers and libraries needs to be included by the project:

include_directories(${CPPGRAM_INCLUDE_DIRS})
target_link_libraries( your_target_name ${CPPGRAM_LIBRARIES})

How to use it

all messages

Create a method that take a class derived from cppgram::BasicBot and a cppgram::types::Message as argument:

void processMessage(MyBot& bot, cppgram::types::Message);

Then say the bot to use this funciton to process all messages received:

bot.processMessage = &processMessage;

All the message received will be forwarded to this function. To each update type a different function pointer is called:

Check the bot API reference for more information.

Commands

Cppgram support commands, as of version 1.0.

Commands are entities that have special conditions and they're called only if an update meet them.

The validation check is done before the pointers listed above. If no command has been triggered, the bot will use the processUpdates pointers.

Message Commands

cppgram::commands::MessageCommand are commands triggered when a message contains a MessageEntity of type bot_command as first part of the text.

If you want to put a greeting for user that click /start on the bot using MessageCommand:

// Define the function that will be called on each "/start" message received
void startCommand(MyBot &bot,
               const cppgram::types::Message &message) {
    bot.sendMessage("This is a start message");
}

int main() {
    // Create the bot
    ...

    // The string that has to appear in the bot_command
    // Do not include the "/"
    std::string command_name = "start";

    // Create the command
    cppgram::commands::MessageCommand *command =
            // passing the command name and the function pointer
            new cppgram::commands::MessageCommand<MyBot>(command_name, &startCommand);

    // Add it to the command handler
    bot.commands().addCommand(command);

    // Run the bot
    ...
}

Inline Keyboards

Inline keyboard represent a button below a message. To send a message with a button use this sintax:

// Create a button
bot.keyboard().addButton("Cppgram Documentation",
        "http://DanySpin97.github.io/cppgram/", InlineKeyboardButtonType::Url);

// Get the keyboard JSON decoded to send it to telegram
string button_string;
keyboard.getKeyboard(button_string);

// Call the api to send a message
sendMessage(chat_id, "Test bot for Cppgram wrapper", button_string);

For a complete list of methods check the documentation about cppgram::Keyboard.

Compile Options

About

Authors

This wrapper has been developed by WiseDragonStd (Danilo Spinella, Stefano Belli).

License

This software has been released under the GNU LGLPv3.