This is a lightweight C library to interact with the Telegram Bot API. It supports basic messaging features like reading updates, sending messages/documents, deleting, and editing messages. It's perfect for minimal bots written in C.
Before you begin, make sure you have the following:
You will also need to compile the library as a static .a file (optional for reusability).
| File | Description |
|---|---|
tgbot.c |
Source file with all bot logic |
tgbot.h |
Header file for declarations |
README.md |
This file |
last_update.txt |
Used for tracking offset |
In tgbot.c, replace the placeholder with your botβs API token.
char* BOT_API = "YOUR_TELEGRAM_BOT_TOKEN";These are internal utilities to support the main bot functions.
size_t callback(char *ptr, size_t size, size_t nmemb, void *userdata);
void last_up_add();
void last_update_read();
char* latest_file(const char* file_path);callback()β Used internally for handling libcurl responses.last_up_add()/last_update_read()β Save and load last update offset fromlast_update.txt.latest_file(path)β Returns the latest file from a directory (useful for sending recent downloads).
updateData *get_updates();
void get_document(char *file_id);
int send_message(long long *CID, const char *message, const int reply, long long reply_id);
void send_document(long long *CID, const char *file_name, const int reply, const int reply_id);
void delete_message(long long *CID, const int bot_msgid, long long *group_chat_id);
void edit_message(long long *CID, const int edit_msg_id, const char *message);The Update data is actually struct that contain
typedef struct updateData {
char *chat_Result; // chat message initially "chat"
long long chat_id; // chat id initially -1
long long group_chat_id; // group chat id(if it exist) initially -1
int reply_id; // reply message id, initially -1
char *file_id; // file id(if file was sent) initially "file_id=NULL"
} updateData;You can receiving updates by adding this line at the top of your code
updateData *updateData = get_updates();and access them as struct
updateData->chat_Result // or this updateData.chat_Result if u prefer "." (dot)All of the data are
chat_Resultβ The user's messagechat_idβ The chat idgroup_chat_idβ The group chat id (if any)reply_idβ The message id to reply tofile_idβ To download a file by using the get_document() function
When someone sends a file get_updates() function will capture a unique file id with that you can download any files the function get_document() need a file id inorder to download file
void get_document(char *file_id);start by checking if file id exist and then fetch it-
if(userdata->file_id)
get_document(userdata->file_id);Keep in mind that when a file is fetched, it will be saved in its respective folder based on its type. For example, photos will be saved in the Photos folder
To send a message, use:
int send_message(long long *CID, const char *message, const int reply, long long reply_id);Parameters:
CIDβ Pointer to chat idmessageβ Message text to sendreplyβ 1 to reply to a message, 0 otherwisereply_idβ Message id to reply to (from get_updates)
The function returns the bot's sent message id, which can be used to edit or delete the message later.
To send files under 50 MB(Telegram Bot limitation):
void send_document(long long *CID, const char *file_name, const int reply, const int reply_id);Parameters:
CIDβ Pointer to chat idfile_nameβ Path to file (can include directories)replyβ 1 to reply, 0 otherwisereply_idβ Message id to reply to
void delete_message(long long *CID, const int bot_msgid, long long *group_chat_id);Parameters:
CIDβ Pointer to chat idbot_msgidβ Bot message id (from send_message)group_chat_idβ Pointer to group chat id (if applicable)
void edit_message(long long *CID, const int edit_msg_id, const char *message);Parameters:
CIDβ Pointer to chat idedit_msg_idβ Bot message id to edit (from send_message)messageβ New message text
If you'd like to contribute, feel free to fork the repo and submit a pull request.
- Ensure your code follows a clean and readable C coding style.
- Keep functions modular and well-documented using comments.
- Do not include your own bot token or any sensitive data in commits.
- Test your code thoroughly before submitting a pull request.
- Use descriptive function/variable names and meaningful commit messages.
We appreciate your contributions to improve this project!
Pull requests will be closed without merging if they:
- Add unnecessary or unrelated features
- Contain broken, untested, or buggy code
- Include hardcoded sensitive data (e.g., bot tokens)
- Do not follow basic C coding style or formatting guidelines
Please make sure your contributions adhere to the project standards to avoid delays or closures.
If you encounter bugs, have feature requests, or want to suggest improvements, please open an issue on the repository.
- A clear and descriptive title.
- A detailed description of the problem or suggestion.
- Steps to reproduce the bug (if applicable).
- Your environment details (OS, compiler version, etc.).
- Any relevant code snippets or error messages.
This helps us understand and address your issue more efficiently. Thank you for helping improve the project!
This project is licensed under the MIT License β see the LICENSE file for details.