Compare commits

...

10 Commits

13 changed files with 348 additions and 147 deletions
+2
View File
@@ -6,3 +6,5 @@ IndentCaseLabels: true
ColumnLimit: 120
PointerAlignment: Right
AllowShortFunctionsOnASingleLine: None
InsertBraces: true
AllowShortBlocksOnASingleLine: Empty
+63 -63
View File
@@ -1,63 +1,63 @@
# tgbot
A minimal C Telegram API Framework.
## Requirements
- libcurl
- json-c
> Note: This project is purely educational. It does not aim to cover the entire Telegram Bot API, but only a selected subset of methods.
## How to build
<details>
<summary>Linux</summary>
```bash
$ meson setup build
$ cd build
$ meson compile
$ meson install
```
</details>
<details>
<summary>Windows</summary>
Install all the required library with `vcpkg` and then copy the DLL file.
```powershell
$ meson setup build --native-file meson-vcpkg.txt
$ cd build
$ meson compile
$ meson install
```
</details>
## Examples
You can find some examples [here](./examples/).
### Supported Types
- **InlineKeyboardMarkup**
- Note: Standard `KeyboardMarkup` is intentionally not supported.
#### Supported Methods
- `getMe`
- `sendMessage`
- `editMessageText`
- `sendDice`
## Roadmap
- `sendPhoto`
- `sendAudio`
- `sendDocument`
- `sendVideo`
# tgbot
A minimal C Telegram API library.
## Requirements
- libcurl
- json-c
> Note: This project is purely educational. It does not aim to cover the entire Telegram Bot API, but only a selected subset of methods.
## How to build
<details>
<summary>Linux</summary>
```bash
$ meson setup build
$ cd build
$ meson compile
$ meson install
```
</details>
<details>
<summary>Windows</summary>
Install all the required library with `vcpkg` and then copy the DLL file.
```powershell
$ meson setup build --native-file meson-vcpkg.txt
$ cd build
$ meson compile
$ meson install
```
</details>
## Examples
You can find some examples [here](./examples/).
### Supported Types
- **InlineKeyboardMarkup**
- Note: Standard `KeyboardMarkup` is intentionally not supported.
#### Supported Methods
- `getMe`
- `sendMessage`
- `editMessageText`
- `sendDice`
- `sendPhoto`
## Roadmap
- `sendAudio`
- `sendDocument`
- `sendVideo`
+39
View File
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tgbot/methods.h>
#include <tgbot/tgbot.h>
#include <tgbot/types.h>
#define START_MESSAGE "Send /photo to receive a nice landscape!"
#define PHOTO_PATH "my_photo.jpg"
int main(void) {
FILE *tok = fopen(".token", "r");
if (!tok) {
exit(EXIT_FAILURE);
}
char token[512];
fscanf(tok, "%s", token);
fprintf(stdout, "Token: %s\n", token);
tgbot_s *bot = tgbot_new(token);
if (!bot) {
return -1;
}
tgbot_update_s update;
while (1) {
tgbot_get_update(bot, &update, NULL);
if (!strcmp(update.text, "/start")) {
tgbot_send_message(bot, update.chat_id, START_MESSAGE, "MARKDOWN", NULL);
} else if (!strcmp(update.text, "/photo")) {
tgbot_send_photo(bot, update.chat_id, PHOTO_PATH, "Mountains!");
}
}
return 0;
}
+1 -1
View File
@@ -2,7 +2,6 @@
#define TGBOT_METHODS_H
#include "types.h"
#include <json-c/json.h>
/* Retrieve update */
int tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
@@ -12,6 +11,7 @@ int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me);
int tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
tgbot_inlinekeyboard_s *reply_markup);
int tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji);
int tgbot_send_photo(const tgbot_s *bot, int64_t chat_id, const char *path, const char *caption);
/* Updating Methods */
int tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
+2
View File
@@ -15,6 +15,8 @@ tgbot_s *tgbot_new(const char *token);
/**
* @brief Cleans the memory.
* Note: If you're using curl it will call curl_global_cleanup().
* Maybe I'll change the API.
*
* @param[out] bot The Bot object.
*/
-28
View File
@@ -1,28 +0,0 @@
[constants]
vcpkg_base_path = 'C:/vcpkg/'
vcpkg_base_install_dir = 'C:/vcpkg/installed/'
vcpkg_target_triplet = 'x64-windows'
vcpkg_host_triplet = 'x64-windows'
vcpkg_installed_dir = vcpkg_base_install_dir + vcpkg_target_triplet + '/'
vcpkg_host_installed_dir = vcpkg_base_install_dir + vcpkg_host_triplet + '/'
vcpkg_toolchain_file = vcpkg_base_path + 'scripts/toolchains/windows.cmake'
[properties]
cmake_toolchain_file = vcpkg_base_path + 'scripts/buildsystems/vcpkg.cmake'
[binaries]
vcpkg = [ vcpkg_base_path + 'vcpkg.exe']
pkgconfig = [ vcpkg_installed_dir + 'tools/pkgconf/pkgconf.exe']
[cmake]
VCPKG_TARGET_TRIPLET = vcpkg_target_triplet
VCPKG_HOST_TRIPLET = vcpkg_host_triplet
VCPKG_CHAINLOAD_TOOLCHAIN_FILE = vcpkg_base_path + 'scripts/toolchains/windows.cmake'
_VCPKG_INSTALLED_DIR = vcpkg_installed_dir
VCPKG_CRT_LINKAGE = 'dynamic'
[built-in options]
pkg_config_path = [ vcpkg_installed_dir + 'lib/pkgconfig;' + vcpkg_installed_dir + 'share/pkgconfig']
cmake_prefix_path = [ vcpkg_installed_dir ]
+24 -4
View File
@@ -39,12 +39,32 @@ tgbot_dep = declare_dependency(
dependencies: deps,
)
cppcheck = find_program('cppcheck', required: false)
if cppcheck.found()
run_target(
'cppcheck',
command: [
cppcheck,
'--enable=all',
'--inconclusive',
'--std=c18',
'--check-level=exhaustive',
'--suppress=missingIncludeSystem',
'--project='
+ join_paths(meson.current_build_dir(), 'compile_commands.json'),
],
)
endif
clangformat = find_program('clang-format', required: false)
if clangformat.found()
run_target('format', command: [clangformat, '-i', sources])
endif
# Example
example = executable(
executable(
'example',
'examples/inlinekeyboard/inlinekeyboard.c',
'examples/sender/sendpic.c',
dependencies: tgbot_dep,
build_by_default: false,
)
test('example_inlinekeyboard', example)
+1 -1
View File
@@ -23,7 +23,7 @@ struct memory_buffer {
typedef struct tgbot {
char token[TOKEN_SIZE]; /**< Bot token. */
char api[API_SIZE]; /**< Bot API url. */
int32_t offset; /**< Bot offset. */
int64_t offset; /**< Bot offset. */
} tgbot_s;
enum tgbot_opt_type {
+31 -3
View File
@@ -6,6 +6,10 @@
#include "tg_types.h"
json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
if (!options && optionslen > 0) {
return NULL;
}
json_object *rjson = json_object_new_object();
if (!rjson) {
return NULL;
@@ -14,6 +18,9 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
for (size_t i = 0; i < optionslen; ++i) {
switch (options[i].type) {
case tgbot_opt_int: {
if (!options[i].value) {
break;
}
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
break;
}
@@ -25,12 +32,19 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
break;
}
case tgbot_opt_int64: {
if (!options[i].value) {
break;
}
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
break;
}
case tgbot_opt_inlinekeyboard: {
if (options[i].value != NULL) {
json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value);
if (!reply_markup) {
json_object_put(rjson);
return NULL;
}
json_object_object_add(rjson, options[i].key, reply_markup);
}
break;
@@ -45,6 +59,10 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
}
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
if (!keyboard) {
return NULL;
}
json_object *reply_markup = json_object_new_object();
if (!reply_markup) {
return NULL;
@@ -58,9 +76,15 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
for (size_t i = 0; i < keyboard->rows; ++i) {
json_object *row = json_object_new_array();
if (!row) {
json_object_put(inline_keyboard_array);
json_object_put(reply_markup);
return NULL;
}
for (size_t j = 0; j < keyboard->columns; ++j) {
tgbot_inlinekeyboardbutton_s *kbbutton = tgbot_inlinekb_button_at(keyboard, i, j);
if (strcmp(kbbutton->text, "") == 0) {
if (!kbbutton || kbbutton->text[0] == '\0') {
continue;
}
@@ -70,8 +94,12 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
}
json_object_object_add(button, "text", json_object_new_string(kbbutton->text));
json_object_object_add(button, "url", json_object_new_string(kbbutton->url));
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));
if (kbbutton->url[0] != '\0') {
json_object_object_add(button, "url", json_object_new_string(kbbutton->url));
}
if (kbbutton->callback_data[0] != '\0') {
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));
}
json_object_array_add(row, button);
}
+72 -8
View File
@@ -58,7 +58,7 @@ static int tgbot_request(const char *url, struct memory_buffer **mb, json_object
}
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 30L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
if (json != NULL) {
json_string = json_object_to_json_string_ext(json, JSON_C_TO_STRING_PLAIN);
@@ -96,6 +96,60 @@ static int tgbot_execute_method(const tgbot_s *bot, const char *method, tgbot_op
return ret;
}
static int tgbot_execute_method_multipart(const tgbot_s *bot, const char *method, int64_t chat_id, const char *path,
const char *caption) {
CURL *curl = curl_easy_init();
if (!curl) {
return -1;
}
char url[URL_LEN] = {0};
int chars = snprintf(url, sizeof(url), "%s%s", bot->api, method);
if (chars < 0 || (size_t)chars >= sizeof(url)) {
curl_easy_cleanup(curl);
return -1;
}
curl_mime *mime = curl_mime_init(curl);
curl_mimepart *part;
char chat_id_str[512];
snprintf(chat_id_str, sizeof chat_id_str, "%ld", chat_id);
part = curl_mime_addpart(mime);
curl_mime_data(part, chat_id_str, CURL_ZERO_TERMINATED);
curl_mime_name(part, "chat_id");
part = curl_mime_addpart(mime);
curl_mime_filedata(part, path);
curl_mime_name(part, "photo");
part = curl_mime_addpart(mime);
curl_mime_data(part, caption, CURL_ZERO_TERMINATED);
curl_mime_name(part, "caption");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
/* Do not print response to output */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, discard_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
curl_mime_free(mime);
return -1;
}
curl_easy_cleanup(curl);
curl_mime_free(mime);
return 0;
}
int tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler) {
char url[URL_LEN];
@@ -163,11 +217,13 @@ int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) {
char url[URL_LEN];
snprintf(url, sizeof(url), "%sgetMe", bot->api);
struct memory_buffer *mb;
struct memory_buffer *mb = {0};
int ret = tgbot_request(url, &mb, NULL);
if (ret != 0) {
free(mb->data);
free(mb);
if (mb) {
free(mb->data);
free(mb);
}
return -1;
}
@@ -185,10 +241,14 @@ int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) {
const json_object *result = json_object_object_get(json, "result");
json_object *first_name = json_object_object_get(result, "first_name");
snprintf(me->first_name, sizeof(me->first_name), "%s", json_object_get_string(first_name));
if (first_name) {
snprintf(me->first_name, sizeof(me->first_name), "%s", json_object_get_string(first_name));
}
json_object *username = json_object_object_get(result, "username");
snprintf(me->username, sizeof(me->username), "%s", json_object_get_string(username));
if (username) {
snprintf(me->username, sizeof(me->username), "%s", json_object_get_string(username));
}
json_object_put(json);
@@ -196,12 +256,12 @@ int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) {
}
int tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
tgbot_inlinekeyboard_s *keyboard) {
tgbot_inlinekeyboard_s *reply_markup) {
tgbot_option_s options[4] = {
{"chat_id", &chat_id, tgbot_opt_int64},
{"text", (void *)text, tgbot_opt_string},
{"parse_mode", (void *)parse_mode, tgbot_opt_string},
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
{"reply_markup", reply_markup, tgbot_opt_inlinekeyboard},
};
return tgbot_execute_method(bot, "sendMessage", options, opt_size(options));
@@ -227,3 +287,7 @@ int tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji) {
return tgbot_execute_method(bot, "sendDice", options, opt_size(options));
}
int tgbot_send_photo(const tgbot_s *bot, int64_t chat_id, const char *path, const char *caption) {
return tgbot_execute_method_multipart(bot, "sendPhoto", chat_id, path, caption);
}
+62 -29
View File
@@ -2,41 +2,56 @@
#include "internal/common.h"
int tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) {
if (!bot || !update || !result) {
return -1;
}
json_object *update_id = json_object_object_get(result, "update_id");
if (!update_id) {
return -1;
}
bot->offset = json_object_get_int(update_id) + 1;
update->update_id = json_object_get_int(update_id);
json_object *message = json_object_object_get(result, "message");
if (!message) {
return -1;
}
json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) {
update->message_id = json_object_get_int(message_id);
update->message_id = json_object_get_int64(message_id);
}
json_object *chat = json_object_object_get(message, "chat");
json_object *chat_id = json_object_object_get(chat, "id");
if (chat_id) {
update->chat_id = json_object_get_int64(chat_id);
}
if (chat) {
json_object *chat_id = json_object_object_get(chat, "id");
if (chat_id) {
update->chat_id = json_object_get_int64(chat_id);
}
json_object *chat_first_name = json_object_object_get(chat, "first_name");
if (chat_first_name) {
snprintf(update->chat_first_name, sizeof(update->chat_first_name), "%s",
json_object_get_string(chat_first_name));
}
json_object *chat_first_name = json_object_object_get(chat, "first_name");
if (chat_first_name) {
snprintf(update->chat_first_name, sizeof(update->chat_first_name), "%s",
json_object_get_string(chat_first_name));
}
json_object *chat_last_name = json_object_object_get(chat, "last_name");
if (chat_last_name) {
snprintf(update->chat_last_name, sizeof(update->chat_last_name), "%s", json_object_get_string(chat_last_name));
}
json_object *chat_last_name = json_object_object_get(chat, "last_name");
if (chat_last_name) {
snprintf(update->chat_last_name, sizeof(update->chat_last_name), "%s",
json_object_get_string(chat_last_name));
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username != NULL) {
snprintf(update->chat_username, sizeof(update->chat_username), "%s", json_object_get_string(chat_username));
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username) {
snprintf(update->chat_username, sizeof(update->chat_username), "%s", json_object_get_string(chat_username));
}
json_object *chat_type = json_object_object_get(chat, "type");
if (chat_type) {
snprintf(update->chat_type, sizeof(update->chat_type), "%s", json_object_get_string(chat_type));
json_object *chat_type = json_object_object_get(chat, "type");
if (chat_type) {
snprintf(update->chat_type, sizeof(update->chat_type), "%s", json_object_get_string(chat_type));
}
}
json_object *date = json_object_object_get(message, "date");
@@ -53,26 +68,44 @@ int tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *resul
}
int tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) {
if (!bot || !query || !result || !cbq_handler) {
return -1;
}
json_object *update_id = json_object_object_get(result, "update_id");
if (!update_id) {
return -1;
}
bot->offset = json_object_get_int(update_id) + 1;
query->update_id = json_object_get_int(update_id);
json_object *callback_query = json_object_object_get(result, "callback_query");
if (!callback_query) {
return -1;
}
json_object *message = json_object_object_get(callback_query, "message");
if (!message) {
return -1;
}
json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) {
query->message_id = json_object_get_int(message_id);
query->message_id = json_object_get_int64(message_id);
}
json_object *chat = json_object_object_get(message, "chat");
json_object *chat_id = json_object_object_get(chat, "id");
if (chat_id) {
query->chat_id = json_object_get_int64(chat_id);
}
if (chat) {
json_object *chat_id = json_object_object_get(chat, "id");
if (chat_id) {
query->chat_id = json_object_get_int64(chat_id);
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username) {
snprintf(query->chat_username, sizeof(query->chat_username), "%s", json_object_get_string(chat_username));
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username) {
snprintf(query->chat_username, sizeof(query->chat_username), "%s", json_object_get_string(chat_username));
}
}
json_object *date = json_object_object_get(message, "date");
+26 -3
View File
@@ -8,21 +8,44 @@
#include "tgbot.h"
tgbot_s *tgbot_new(const char *token) {
if (!token || token[0] == '\0') {
return NULL;
}
tgbot_s *bot = malloc(sizeof(tgbot_s));
if (!bot) {
return NULL;
}
curl_global_init(CURL_GLOBAL_DEFAULT);
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
free(bot);
return NULL;
}
int chars = snprintf(bot->api, sizeof(bot->api), "https://api.telegram.org/bot%s/", token);
if (chars < 0 || (size_t)chars >= sizeof(bot->api)) {
curl_global_cleanup();
free(bot);
return NULL;
}
chars = snprintf(bot->token, sizeof(bot->token), "%s", token);
if (chars < 0 || (size_t)chars >= sizeof(bot->token)) {
curl_global_cleanup();
free(bot);
return NULL;
}
snprintf(bot->api, sizeof(bot->api), "https://api.telegram.org/bot%s/", token);
snprintf(bot->token, sizeof(bot->token), "%s", token);
bot->offset = 0;
return bot;
}
void tgbot_free(tgbot_s *bot) {
if (!bot) {
return;
}
curl_global_cleanup();
free(bot);
}
+25 -7
View File
@@ -1,44 +1,62 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns) {
tgbot_inlinekeyboard_s *keyboard = (tgbot_inlinekeyboard_s *)malloc(sizeof(tgbot_inlinekeyboard_s));
if (rows == 0 || columns == 0 || rows > SIZE_MAX / columns) {
return NULL;
}
tgbot_inlinekeyboard_s *keyboard = malloc(sizeof(tgbot_inlinekeyboard_s));
if (!keyboard) {
return NULL;
}
keyboard->rows = rows;
keyboard->columns = columns;
keyboard->buttons = (tgbot_inlinekeyboardbutton_s *)malloc(rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
size_t count = rows * columns;
keyboard->buttons = malloc(count * sizeof(tgbot_inlinekeyboardbutton_s));
if (!keyboard->buttons) {
free(keyboard);
return NULL;
}
memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
memset(keyboard->buttons, 0, count * sizeof(tgbot_inlinekeyboardbutton_s));
return keyboard;
}
void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard) {
if (!keyboard) {
return;
}
free(keyboard->buttons);
free(keyboard);
}
tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column) {
if (!keyboard || row >= keyboard->rows || column >= keyboard->columns) {
return NULL;
}
return &keyboard->buttons[row * keyboard->columns + column];
}
int tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text,
const char *url, const char *callback_data) {
tgbot_inlinekeyboardbutton_s *button = tgbot_inlinekb_button_at(keyboard, row, column);
if (!button) {
return -1;
}
strncpy(button->text, text, sizeof(button->text) - 1);
strncpy(button->url, url, sizeof(button->url) - 1);
strncpy(button->callback_data, callback_data, sizeof(button->callback_data) - 1);
snprintf(button->text, sizeof(button->text), "%s", text ? text : "");
snprintf(button->url, sizeof(button->url), "%s", url ? url : "");
snprintf(button->callback_data, sizeof(button->callback_data), "%s", callback_data ? callback_data : "");
return 0;
}