Giter VIP home page Giter VIP logo

telegrab's Introduction

Telegrab is easy-to-use C++11 library for Telegram Bot API.

The library itself consists of a single header file telegrab.hpp.

Dependencies

Telegrab requires the nlohmann::json and yhirose::cpp-httplib libraries.

Those are single-header libraries. Just drop them into the same folder as the telegrab.hpp file.

You also need to install openssl.

NOTE: cpp-httplib currently supports only version 1.1.1.

sudo apt install build-essential -y
wget -c https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar -xzvf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
./config
make
sudo make install

Compilation

Compile using g++ version 4.8 or higher:

g++ -std=c++11 main.cpp -lssl -lcrypto -pthread

Examples

First you need to include telegrab.hpp to your project.

Usually main.cpp file will look like this:

#include "telegrab.hpp"

void Telegrab::Instructions(incoming data)
{
  // Instructions for the bot
}

int main()
{
  Telegrab bot("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11");
  bot.start();

  return 0;
}

You can also use a json file as an argument

(if you use a token, the config file will be generated automatically).

Telegrab bot("config.json");

Usually config.json file will look like this:

{
  "polling":
  {
    "interval":0,
    "limit":100,
    "timeout":30,
    "retryTimeout":10
  },
  "token":"123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
}

In which token - Telegram Bot API token.

interval - How often check updates (seconds).

limit - Limits the number of updates to be retrieved (1-100).

timeout - Timeout in seconds for long polling (0 - short polling).

retryTimeout - Reconnecting timeout (seconds).

Simple echo bot

void Telegrab::Instructions(incoming data)
{
  if (!data.text.empty())
  {
    content message;
    message.text = data.text;
    // Send (message_id) to (chat_id)
    send(message, data.chat_id);
  }
  ...
}

Simple echo bot that forwards a message

void Telegrab::Instructions(incoming data)
{
  if (!data.text.empty())
  {
    int message_id = data.message_id;
    int chat_id_from = data.chat_id;
    int chat_id_to = data.chat_id;
    // Forward (message_id) from (chat_id_from) to (chat_id_to)
    forward(message_id, chat_id_from, chat_id_to);
  }
  ...
}

Simple echo bot with a file

How you do this depends on whether you want to download the file or not:

void Telegrab::Instructions(incoming data)
{
  if (!data.photo.empty())
  {
    content message;
    // Using file_id of the original image (without downloading)
    message.photo = data.photo;
    message.text = "This photo was sent by its file_id";
    send(message, data.chat_id);

    // Using the path to the downloaded image
    message.photo = download(data.photo);
    message.text = "This photo was downloaded";
    send(message, data.chat_id);
  }
  ...
}

You can also send your own file, using the full path or URL:

void Telegrab::Instructions(incoming data)
{
  content message;
  // Upload local file
  message.photo = "photos/image.jpg";
  send(message, data.chat_id);

  // Download and send image
  message.photo = download("https://something.com/image.jpg");
  send(message, data.chat_id);

  // Send image without downloading using Telegram server (5 MB max size for photos and 20 MB max for other types of content)
  message.photo = "https://something.com/image.jpg";
  send(message, data.chat_id);

  ...
}

Message reply

void Telegrab::Instructions(incoming data)
{
  // If the message is a reply, you can pass the ID of the original message as an argument
  if (data.text == "reply test")
  {
    content message;
    message.text = "This message is a reply";
    // Reply to (message_id) in (chat_id) with (message)
    send(message, data.chat_id, data.message_id);

    return;
  }
  ...
}

How to use special objects (commands, hashtags, etc.)

void Telegrab::Instructions(incoming data)
{
  for (const auto& entity:data.entities)
  {
    if (entity == "/start")
    {
      content message;
      message.text = "Hello world!";
      send(message, data.chat_id);

      return;
    }
    if (entity == "#example")
    {
      content message;
      message.text = "Example!";
      send(message, data.chat_id);

      return;
    }
  }
  ...
}

Creating a custom reply keyboard

void Telegrab::Instructions(incoming data)
{
  for (const auto& entity:data.entities)
  {
    if (entity == "/start")
    {
      // The Telegram keyboard consists of rows, which consist of buttons
      ReplyKeyboardRow row_1;

      // Creating button
      KeyboardButton btn;

      // Button text
      btn.text = "Click me";

      // Optional fields, 'false' by default, so you can delete these lines (for more info go to Telegram Bot API)
      btn.request_contact = false;
      btn.request_location = false;

      // Pushing button to the row
      row_1.push_back(btn);

      // Pushing row to the keyboard
      message.reply_keyboard.keyboard.push_back(row_1);

      // Optional fields, 'false' by default, so you can delete these lines (for more info go to Telegram Bot API)
      // resize_keyboard set to 'true' so the buttons look better
      message.reply_keyboard.resize_keyboard = true;
      message.reply_keyboard.one_time_keyboard = false;
      message.reply_keyboard.selective = false;

      send(message, data.chat_id);

      return;
    }
  }
  ...
}

How to hide a custom keyboard

void Telegrab::Instructions(incoming data)
{
  if (data.text.find("Click me") != std::string::npos)
  {
    ontent message;
    message.text = "The custom keyboard has been removed.";

    // Deleting custom keyboard
    ReplyKeyboardHide h;
    h.hide = true;
    // Optional field, 'false' by default
    h.selective = false;
    message.hide_reply_keyboard = h;

    send(message, data.chat_id);

    return;
  }
  ...
}

All instructions must be in the same method

void Telegrab::Instructions(incoming data)
{
  if (!data.sticker.empty())
  {
    content message;
    message.sticker = "CAADAgADSAoAAm4y2AABrGwuPYwIwBwWBA";
    send(message, data.chat_id);

    return;
  }
  if (!data.document.empty())
  {
    content message;
    message.document = download(data.document);
    send(message, data.chat_id);

    return;
  }
  ...
}

Basic methods and data

Data

Incoming message

Integer:

chat_id and message_id

String (text or filename or file_id):

photo

video

document

text

audio

sticker

voice

caption

Vector<string> (contains all entities from the message, i.e. commands, hashtags, etc.):

entities

Upcoming message

String (text or filename or file_id or URL):

photo

video

document

text

audio

sticker

reply_keyboard

hide_reply_keyboard

Methods

Send

Send a message (if the message is a reply, 3d parameter required)

void send(content message, int chat_id)

void send(content message, int chat_id, int message_id)

Forward

Forward a message

void forward(int message_id, int chat_id_from, int chat_id_to)

Download

Download a file (returns the path to the file with the name included)

string download(string given)

telegrab's People

Contributors

krupakov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

arrufat

telegrab's Issues

Curl dependency

Hi, your libs looks perfect for low resource machine, im working in a project (openZmeter) where a telegram lib is needed to work in a low end ARM SOC. We use the nlohmann lib yet. However we stop using curl some time and replace it with another "single file" HTTP client/server lib (cpp-httplib).

This HTTP lib is in C++. I think in fork your project and replace the curl calls with cpp-httplib, but i think is better to ask you before, maybe you are interested in this change and we can supply a pull request instead of create a new derived lib.

Update

Hi, i see your project is a bit outdated (cant compile using last httplib).

I think in fork it but I also see it not maintained recently, if you left it, im interested in continue working in it. if interested add me mantainer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.