Telebot C Library

repository·master·Indexed 41 days ago

https://github.com/smartnode/telebot

A C library for interfacing with the Telegram Bot API, utilizing libcurl for network communication and json-c for JSON parsing. It provides functions for bot initialization, polling updates, and sending messages or multimedia.

Tokens
1.8K
Snippets
4
Records
4
Agent score
2%

What's inside Telebot

  1. Run the Telebot test executable

    master

    The repository includes a comprehensive test bot executable named testbot. To run it, you must first place your Telegram Bot API Token in a file named .token in the root folder of the repository.

    Run the test bot using:

    ./build/test/testbot

    The testbot supports testing various APIs, polling, custom keyboards, and multimedia sending.

    ./build/test/testbot
  2. Build the Telebot library

    master

    To build the library from source, use CMake. You can perform a standard build or a debug build.

    Standard Build:

    cd [your repository]
    mkdir -p build && cd build
    cmake ..
    make

    Debug Build: To include debug symbols, pass the DEBUG=1 flag to CMake:

    mkdir -p build && cd build
    cmake .. -DDEBUG=1
    make
    mkdir -p build && cd build
    cmake ..
    make
  3. Install dependencies for Telebot

    master

    Telebot is a C library that requires libcurl and json-c. You also need build tools like cmake, make, and binutils.

    Debian-based Linux:

    sudo apt-get install libcurl4-openssl-dev libjson-c-dev cmake binutils make

    macOS (via MacPorts):

    sudo port install cmake json-c curl
    sudo apt-get install libcurl4-openssl-dev libjson-c-dev cmake binutils make
  4. Example: Create a simple Echo Bot in C

    master

    This example demonstrates how to initialize a Telebot instance, retrieve bot information, and poll for updates to create an 'Echobot' that responds to messages.

    Key steps in the example:

    1. Read the bot token from a .token file.
    2. Initialize the handler using telebot_create.
    3. Get bot identity using telebot_get_me.
    4. Enter a loop using telebot_get_updates to poll for new messages.
    5. Respond to messages using telebot_send_message or telebot_send_dice.
    6. Clean up resources with telebot_destroy.
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <string.h>
    #include <unistd.h>
    #include <telebot.h>
    
    #define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
    
    int main(int argc, char *argv[])
    {
        printf("Welcome to Echobot\n");
    
        FILE *fp = fopen(".token", "r");
        if (fp == NULL)
        {
            printf("Failed to open .token file\n");
            return -1;
        }
    
        char token[1024];
        if (fscanf(fp, "%s", token) == 0)
        {
            printf("Failed to read token\n");
            fclose(fp);
            return -1;
        }
        printf("Token: %s\n", token);
        fclose(fp);
    
        telebot_handler_t handle;
        if (telebot_create(&handle, token) != TELEBOT_ERROR_NONE)
        {
            printf("Telebot create failed\n");
            return -1;
        }
    
        telebot_user_t me;
        if (telebot_get_me(handle, &me) != TELEBOT_ERROR_NONE)
        {
            printf("Failed to get bot information\n");
            telebot_destroy(handle);
            return -1;
        }
    
        printf("ID: %d\n", me.id);
        printf("First Name: %s\n", me.first_name);
        printf("User Name: %s\n", me.username);
    
        telebot_put_me(&me);
    
        int index, count, offset = -1;
        telebot_error_e ret;
        telebot_message_t message;
        telebot_update_type_e update_types[] = {TELEBOT_UPDATE_TYPE_MESSAGE};
    
        while (1)
        {
            telebot_update_t *updates;
            ret = telebot_get_updates(handle, offset, 20, 0, update_types, 0, &updates, &count);
            if (ret != TELEBOT_ERROR_NONE)
                continue;
            printf("Number of updates: %d\n", count);
            for (index = 0; index < count; index++)
            {
                message = updates[index].message;
                if (message.text)
                {
                    printf("%s: %s \n", message.from->first_name, message.text);
                    if (strstr(message.text, "/dice"))
                    {
                        telebot_send_dice(handle, message.chat->id, false, 0, "");
                    }
                    else
                    {
                        char str[4096];
                        if (strstr(message.text, "/start"))
                        {
                            snprintf(str, SIZE_OF_ARRAY(str), "Hello %s", message.from->first_name);
                        }
                        else
                        {
                            snprintf(str, SIZE_OF_ARRAY(str), "<i>%s</i>", message.text);
                        }
                        ret = telebot_send_message(handle, message.chat->id, str, "HTML", false, false, updates[index].message.message_id, "");
                    }
                    if (ret != TELEBOT_ERROR_NONE)
                    {
                        printf("Failed to send message: %d \n", ret);
                    }
                }
                offset = updates[index].update_id + 1;
            }
            telebot_put_updates(updates, count);
    
            sleep(1);
        }
    
        telebot_destroy(handle);
    
        return 0;
    }