refactor: cleanup

This commit is contained in:
2025-09-16 23:53:50 +02:00
parent 235c032062
commit c9af1429f9
13 changed files with 896 additions and 880 deletions

View File

@@ -1,56 +1,56 @@
#include <string.h>
#include "json.h"
json_object *tgbot_json_builder(tgbot_json_option *options, size_t optionslen) {
json_object *rjson = json_object_new_object();
for (size_t i = 0; i < optionslen; ++i) {
if (options[i].type == tgbot_opt_int) {
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
} else if (options[i].type == tgbot_opt_string) {
if (!options[i].value) {
continue;
}
json_object_object_add(rjson, options[i].key, json_object_new_string((char *)options[i].value));
} else if (options[i].type == tgbot_opt_int64) {
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
} else if (options[i].type == tgbot_opt_inlinekeyboard) {
if (options[i].value != NULL) {
json_object *reply_markup = tgbot_new_inlinekeyboardmarkup((tgbot_inlinekeyboard *)options[i].value);
json_object_object_add(rjson, "reply_markup", reply_markup);
}
} else if (options[i].type == tgbot_opt_null) {
continue;
}
}
return rjson;
}
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboard *keyboard) {
json_object *reply_markup = json_object_new_object();
json_object *inline_keyboard_array = json_object_new_array();
for (size_t i = 0; i < keyboard->rows; ++i) {
json_object *row = json_object_new_array();
for (size_t j = 0; j < keyboard->columns; ++j) {
tgbot_inlinekeyboardbutton *kbbutton = tgbot_inlinekeyboard_button_at(keyboard, i, j);
if (strcmp(kbbutton->text, "") == 0) {
continue;
}
json_object *button = json_object_new_object();
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));
json_object_array_add(row, button);
}
json_object_array_add(inline_keyboard_array, row);
}
json_object_object_add(reply_markup, "inline_keyboard", inline_keyboard_array);
return reply_markup;
}
#include <string.h>
#include "json.h"
json_object *json_builder(tgbot_json_option *options, size_t optionslen) {
json_object *rjson = json_object_new_object();
for (size_t i = 0; i < optionslen; ++i) {
if (options[i].type == tgbot_opt_int) {
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
} else if (options[i].type == tgbot_opt_string) {
if (!options[i].value) {
continue;
}
json_object_object_add(rjson, options[i].key, json_object_new_string((char *)options[i].value));
} else if (options[i].type == tgbot_opt_int64) {
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
} else if (options[i].type == tgbot_opt_inlinekeyboard) {
if (options[i].value != NULL) {
json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value);
json_object_object_add(rjson, "reply_markup", reply_markup);
}
} else if (options[i].type == tgbot_opt_null) {
continue;
}
}
return rjson;
}
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
json_object *reply_markup = json_object_new_object();
json_object *inline_keyboard_array = json_object_new_array();
for (size_t i = 0; i < keyboard->rows; ++i) {
json_object *row = json_object_new_array();
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) {
continue;
}
json_object *button = json_object_new_object();
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));
json_object_array_add(row, button);
}
json_object_array_add(inline_keyboard_array, row);
}
json_object_object_add(reply_markup, "inline_keyboard", inline_keyboard_array);
return reply_markup;
}

View File

@@ -1,334 +1,333 @@
#include <curl/curl.h>
#include <string.h>
#include "json.h"
#include "methods.h"
tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler) {
char url[1024];
/* Clear Update */
memset(update, 0, sizeof(tgbot_update));
int limit = 1;
int timeout = 30;
tgbot_json_option options[3] = {
{"offset", &bot->offset, tgbot_opt_int64},
{"limit", &limit, tgbot_opt_int},
{"timeout", &timeout, tgbot_opt_int},
};
json_object *rjson = tgbot_json_builder(options, 3);
snprintf(url, sizeof(url), "%sgetUpdates", bot->api);
struct memory_buffer *mb;
tgbot_rc ret = tgbot_request(url, &mb, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
free(mb->data);
free(mb);
return TGBOT_GETUPDATES_ERROR;
}
json_object *json = json_tokener_parse(mb->data);
free(mb->data);
free(mb);
json_object *ok = json_object_object_get(json, "ok");
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
json_object_put(json);
return TGBOT_TELEGRAM_OK_ERROR;
}
json_object *results = json_object_object_get(json, "result");
size_t results_len = json_object_array_length(results);
if (results_len == 0) {
json_object_put(json);
return TGBOT_OK;
}
/* Check if it is a Message or a CallbackQuery*/
json_object *result = json_object_array_get_idx(results, 0);
json_object *message = json_object_object_get(result, "message");
if (message) {
tgbot_parse_message(bot, update, result);
} else if (cbq_handler != NULL) {
tgbot_cbquery query;
tgbot_parse_cbquery(bot, &query, result, cbq_handler);
}
json_object_put(json);
return TGBOT_OK;
}
tgbot_rc tgbot_parse_message(tgbot *bot, tgbot_update *update, json_object *result) {
json_object *update_id = json_object_object_get(result, "update_id");
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");
json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) {
update->message_id = json_object_get_int(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);
}
json_object *chat_first_name = json_object_object_get(chat, "first_name");
if (chat_first_name) {
strncpy(update->chat_first_name, json_object_get_string(chat_first_name), sizeof(update->chat_first_name) - 1);
}
json_object *chat_last_name = json_object_object_get(chat, "last_name");
if (chat_last_name) {
strncpy(update->chat_last_name, json_object_get_string(chat_last_name), sizeof(update->chat_last_name) - 1);
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username != NULL) {
strncpy(update->chat_username, json_object_get_string(chat_username), sizeof(update->chat_username) - 1);
}
json_object *chat_type = json_object_object_get(chat, "type");
if (chat_type) {
strncpy(update->chat_type, json_object_get_string(chat_type), sizeof(update->chat_type) - 1);
}
json_object *date = json_object_object_get(message, "date");
if (date) {
update->date = json_object_get_int(date);
}
json_object *text = json_object_object_get(message, "text");
if (text) {
strncpy(update->text, json_object_get_string(text), sizeof(update->text) - 1);
}
return TGBOT_OK;
}
tgbot_rc tgbot_parse_cbquery(tgbot *bot, tgbot_cbquery *query, json_object *result, Callback cbq_handler) {
json_object *update_id = json_object_object_get(result, "update_id");
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");
json_object *message = json_object_object_get(callback_query, "message");
json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) {
query->message_id = json_object_get_int(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);
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username) {
strncpy(query->chat_username, json_object_get_string(chat_username), sizeof(query->chat_username) - 1);
}
json_object *date = json_object_object_get(message, "date");
if (date) {
query->date = json_object_get_int(date);
}
json_object *text = json_object_object_get(message, "text");
if (text) {
strncpy(query->text, json_object_get_string(text), sizeof(query->text) - 1);
}
json_object *chat_instance = json_object_object_get(callback_query, "chat_instance");
if (chat_instance) {
strncpy(query->chat_instance, json_object_get_string(chat_instance), sizeof(query->chat_instance) - 1);
}
json_object *data = json_object_object_get(callback_query, "data");
if (data) {
strncpy(query->data, json_object_get_string(data), sizeof(query->data) - 1);
}
cbq_handler(bot, query);
return TGBOT_OK;
}
size_t write_callback(void *ptr, size_t size, size_t nmemb, char **userdata) {
size_t real_size = size * nmemb;
struct memory_buffer *mem = (struct memory_buffer *)userdata;
mem->data = realloc(mem->data, mem->size + real_size + 1);
memcpy(&(mem->data[mem->size]), ptr, real_size);
mem->size += real_size;
mem->data[mem->size] = '\0';
return real_size;
}
size_t discard_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
return size * nmemb;
}
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) {
CURL *curl = curl_easy_init();
if (!curl) {
return TGBOT_REQUEST_ERROR;
}
const char *json_string = NULL;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (mb != NULL) {
*mb = calloc(1, sizeof(struct memory_buffer));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, *mb);
} else {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, discard_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
}
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 30L);
if (json != NULL) {
json_string = json_object_to_json_string_ext(json, JSON_C_TO_STRING_PLAIN);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_string);
}
CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (res != CURLE_OK) {
if (mb != NULL && *mb) {
free((*mb)->data);
free(*mb);
*mb = NULL;
}
return TGBOT_REQUEST_ERROR;
}
curl_easy_cleanup(curl);
return TGBOT_OK;
}
tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me) {
char url[1024];
snprintf(url, sizeof(url), "%sgetMe", bot->api);
struct memory_buffer *mb;
tgbot_rc ret = tgbot_request(url, &mb, NULL);
if (ret != TGBOT_OK) {
free(mb->data);
free(mb);
return TGBOT_GETME_ERROR;
}
json_object *json = json_tokener_parse(mb->data);
free(mb->data);
free(mb);
json_object *ok = json_object_object_get(json, "ok");
if (!json_object_get_boolean(ok)) {
json_object_put(json);
return TGBOT_GETME_ERROR;
}
json_object *result = json_object_object_get(json, "result");
json_object *first_name = json_object_object_get(result, "first_name");
strncpy(me->first_name, json_object_get_string(first_name), sizeof(me->first_name) - 1);
json_object *username = json_object_object_get(result, "username");
strncpy(me->username, json_object_get_string(username), sizeof(me->username) - 1);
json_object_put(json);
return TGBOT_OK;
}
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const char *parse_mode, tgbot_inlinekeyboard *keyboard) {
char url[1024];
tgbot_json_option 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},
};
json_object *rjson = tgbot_json_builder(options, 4);
snprintf(url, sizeof(url), "%ssendMessage", bot->api);
tgbot_rc ret = tgbot_request(url, NULL, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
return TGBOT_SENDMESSAGE_ERROR;
}
return TGBOT_OK;
}
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard *keyboard) {
char url[1024];
tgbot_json_option options[4] = {
{"chat_id", &chat_id, tgbot_opt_int64},
{"message_id", &message_id, tgbot_opt_int},
{"text", (void *)text, tgbot_opt_string},
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
};
json_object *rjson = tgbot_json_builder(options, 4);
snprintf(url, sizeof(url), "%seditMessageText", bot->api);
tgbot_rc ret = tgbot_request(url, NULL, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
return TGBOT_EDITMESSAGETEXT_ERROR;
}
return TGBOT_OK;
}
tgbot_rc tgbot_send_dice(tgbot *bot, int64_t chat_id, const char *emoji) {
char url[1024];
tgbot_json_option options[2] = {
{"chat_id", &chat_id, tgbot_opt_int64},
{"emoji", (void *)emoji, tgbot_opt_string},
};
json_object *rjson = tgbot_json_builder(options, 2);
snprintf(url, sizeof(url), "%ssendDice", bot->api);
tgbot_rc ret = tgbot_request(url, NULL, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
return TGBOT_SENDDICE_ERROR;
}
return TGBOT_OK;
}
#include <curl/curl.h>
#include <string.h>
#include "json.h"
#include "methods.h"
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler) {
char url[1024];
memset(update, 0, sizeof(tgbot_update_s));
int limit = 1;
int timeout = 30;
tgbot_json_option options[3] = {
{"offset", &bot->offset, tgbot_opt_int64},
{"limit", &limit, tgbot_opt_int},
{"timeout", &timeout, tgbot_opt_int},
};
json_object *rjson = json_builder(options, 3);
snprintf(url, sizeof(url), "%sgetUpdates", bot->api);
struct memory_buffer *mb;
tgbot_rc ret = tgbot_request(url, &mb, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
free(mb->data);
free(mb);
return TGBOT_GETUPDATES_ERROR;
}
json_object *json = json_tokener_parse(mb->data);
free(mb->data);
free(mb);
json_object *ok = json_object_object_get(json, "ok");
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
json_object_put(json);
return TGBOT_TELEGRAM_OK_ERROR;
}
json_object *results = json_object_object_get(json, "result");
size_t results_len = json_object_array_length(results);
if (results_len == 0) {
json_object_put(json);
return TGBOT_OK;
}
/* Check if it is a Message or a CallbackQuery*/
json_object *result = json_object_array_get_idx(results, 0);
json_object *message = json_object_object_get(result, "message");
if (message) {
tgbot_parse_message(bot, update, result);
} else if (cbq_handler != NULL) {
tgbot_cbquery_s query;
tgbot_parse_cbquery(bot, &query, result, cbq_handler);
}
json_object_put(json);
return TGBOT_OK;
}
tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) {
json_object *update_id = json_object_object_get(result, "update_id");
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");
json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) {
update->message_id = json_object_get_int(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);
}
json_object *chat_first_name = json_object_object_get(chat, "first_name");
if (chat_first_name) {
strncpy(update->chat_first_name, json_object_get_string(chat_first_name), sizeof(update->chat_first_name) - 1);
}
json_object *chat_last_name = json_object_object_get(chat, "last_name");
if (chat_last_name) {
strncpy(update->chat_last_name, json_object_get_string(chat_last_name), sizeof(update->chat_last_name) - 1);
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username != NULL) {
strncpy(update->chat_username, json_object_get_string(chat_username), sizeof(update->chat_username) - 1);
}
json_object *chat_type = json_object_object_get(chat, "type");
if (chat_type) {
strncpy(update->chat_type, json_object_get_string(chat_type), sizeof(update->chat_type) - 1);
}
json_object *date = json_object_object_get(message, "date");
if (date) {
update->date = json_object_get_int(date);
}
json_object *text = json_object_object_get(message, "text");
if (text) {
strncpy(update->text, json_object_get_string(text), sizeof(update->text) - 1);
}
return TGBOT_OK;
}
tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) {
json_object *update_id = json_object_object_get(result, "update_id");
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");
json_object *message = json_object_object_get(callback_query, "message");
json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) {
query->message_id = json_object_get_int(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);
}
json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username) {
strncpy(query->chat_username, json_object_get_string(chat_username), sizeof(query->chat_username) - 1);
}
json_object *date = json_object_object_get(message, "date");
if (date) {
query->date = json_object_get_int(date);
}
json_object *text = json_object_object_get(message, "text");
if (text) {
strncpy(query->text, json_object_get_string(text), sizeof(query->text) - 1);
}
json_object *chat_instance = json_object_object_get(callback_query, "chat_instance");
if (chat_instance) {
strncpy(query->chat_instance, json_object_get_string(chat_instance), sizeof(query->chat_instance) - 1);
}
json_object *data = json_object_object_get(callback_query, "data");
if (data) {
strncpy(query->data, json_object_get_string(data), sizeof(query->data) - 1);
}
cbq_handler(bot, query);
return TGBOT_OK;
}
size_t write_callback(void *ptr, size_t size, size_t nmemb, char **userdata) {
size_t real_size = size * nmemb;
struct memory_buffer *mem = (struct memory_buffer *)userdata;
mem->data = realloc(mem->data, mem->size + real_size + 1);
memcpy(&(mem->data[mem->size]), ptr, real_size);
mem->size += real_size;
mem->data[mem->size] = '\0';
return real_size;
}
size_t discard_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
return size * nmemb;
}
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) {
CURL *curl = curl_easy_init();
if (!curl) {
return TGBOT_REQUEST_ERROR;
}
const char *json_string = NULL;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (mb != NULL) {
*mb = calloc(1, sizeof(struct memory_buffer));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, *mb);
} else {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, discard_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
}
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 30L);
if (json != NULL) {
json_string = json_object_to_json_string_ext(json, JSON_C_TO_STRING_PLAIN);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_string);
}
CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (res != CURLE_OK) {
if (mb != NULL && *mb) {
free((*mb)->data);
free(*mb);
*mb = NULL;
}
return TGBOT_REQUEST_ERROR;
}
curl_easy_cleanup(curl);
return TGBOT_OK;
}
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
char url[1024];
snprintf(url, sizeof(url), "%sgetMe", bot->api);
struct memory_buffer *mb;
tgbot_rc ret = tgbot_request(url, &mb, NULL);
if (ret != TGBOT_OK) {
free(mb->data);
free(mb);
return TGBOT_GETME_ERROR;
}
json_object *json = json_tokener_parse(mb->data);
free(mb->data);
free(mb);
json_object *ok = json_object_object_get(json, "ok");
if (!json_object_get_boolean(ok)) {
json_object_put(json);
return TGBOT_GETME_ERROR;
}
json_object *result = json_object_object_get(json, "result");
json_object *first_name = json_object_object_get(result, "first_name");
strncpy(me->first_name, json_object_get_string(first_name), sizeof(me->first_name) - 1);
json_object *username = json_object_object_get(result, "username");
strncpy(me->username, json_object_get_string(username), sizeof(me->username) - 1);
json_object_put(json);
return TGBOT_OK;
}
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode, tgbot_inlinekeyboard_s *keyboard) {
char url[1024];
tgbot_json_option 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},
};
json_object *rjson = json_builder(options, 4);
snprintf(url, sizeof(url), "%ssendMessage", bot->api);
tgbot_rc ret = tgbot_request(url, NULL, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
return TGBOT_SENDMESSAGE_ERROR;
}
return TGBOT_OK;
}
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard_s *keyboard) {
char url[1024];
tgbot_json_option options[4] = {
{"chat_id", &chat_id, tgbot_opt_int64},
{"message_id", &message_id, tgbot_opt_int},
{"text", (void *)text, tgbot_opt_string},
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
};
json_object *rjson = json_builder(options, 4);
snprintf(url, sizeof(url), "%seditMessageText", bot->api);
tgbot_rc ret = tgbot_request(url, NULL, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
return TGBOT_EDITMESSAGETEXT_ERROR;
}
return TGBOT_OK;
}
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji) {
char url[1024];
tgbot_json_option options[2] = {
{"chat_id", &chat_id, tgbot_opt_int64},
{"emoji", (void *)emoji, tgbot_opt_string},
};
json_object *rjson = json_builder(options, 2);
snprintf(url, sizeof(url), "%ssendDice", bot->api);
tgbot_rc ret = tgbot_request(url, NULL, rjson);
json_object_put(rjson);
if (ret != TGBOT_OK) {
return TGBOT_SENDDICE_ERROR;
}
return TGBOT_OK;
}

View File

@@ -1,20 +1,26 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "tgbot.h"
tgbot_rc tgbot_init(tgbot *bot, char *token) {
curl_global_init(CURL_GLOBAL_DEFAULT);
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 TGBOT_OK;
}
void tgbot_destroy(tgbot *bot) {
curl_global_cleanup();
}
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "tgbot.h"
tgbot_s *tgbot_new(char *token) {
tgbot_s *bot = malloc(sizeof(tgbot_s));
if (!bot) {
return NULL;
}
curl_global_init(CURL_GLOBAL_DEFAULT);
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) {
curl_global_cleanup();
free(bot);
}

View File

@@ -1,41 +1,41 @@
#include <stdlib.h>
#include <string.h>
#include "types.h"
tgbot_inlinekeyboard *tgbot_new_inlinekeyboard(size_t rows, size_t columns) {
tgbot_inlinekeyboard *keyboard = (tgbot_inlinekeyboard *)malloc(sizeof(tgbot_inlinekeyboard));
if (!keyboard)
return NULL;
keyboard->rows = rows;
keyboard->columns = columns;
keyboard->buttons = (tgbot_inlinekeyboardbutton *)malloc(rows * columns * sizeof(tgbot_inlinekeyboardbutton));
if (!keyboard->buttons) {
free(keyboard);
return NULL;
}
memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton));
return keyboard;
}
void tgbot_destroy_inlinekeyboard(tgbot_inlinekeyboard *keyboard) {
free(keyboard->buttons);
free(keyboard);
}
tgbot_rc tgbot_inlinekeyboard_button(tgbot_inlinekeyboard *keyboard, size_t row, size_t column, const char *text, const char *url, const char *callback_data) {
tgbot_inlinekeyboardbutton *button = tgbot_inlinekeyboard_button_at(keyboard, row, column);
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);
return TGBOT_OK;
}
tgbot_inlinekeyboardbutton *tgbot_inlinekeyboard_button_at(tgbot_inlinekeyboard *keyboard, size_t row, size_t column) {
return &keyboard->buttons[row * keyboard->columns + column];
}
#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 (!keyboard)
return NULL;
keyboard->rows = rows;
keyboard->columns = columns;
keyboard->buttons = (tgbot_inlinekeyboardbutton_s *)malloc(rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
if (!keyboard->buttons) {
free(keyboard);
return NULL;
}
memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
return keyboard;
}
void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard) {
free(keyboard->buttons);
free(keyboard);
}
tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column) {
return &keyboard->buttons[row * keyboard->columns + column];
}
tgbot_rc 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);
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);
return TGBOT_OK;
}