improve keyboard api
This commit is contained in:
17
src/json.c
17
src/json.c
@@ -1,20 +1,23 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "json.h"
|
||||
|
||||
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboardmarkup **keyboard, size_t rows, size_t columns) {
|
||||
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 < rows; ++i) {
|
||||
for (size_t i = 0; i < keyboard->rows; ++i) {
|
||||
json_object *row = json_object_new_array();
|
||||
for (size_t j = 0; j < columns; ++j) {
|
||||
if (strcmp(keyboard[i][j].text, "") == 0) {
|
||||
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(keyboard[i][j].text));
|
||||
json_object_object_add(button, "url", json_object_new_string(keyboard[i][j].url));
|
||||
json_object_object_add(button, "callback_data", json_object_new_string(keyboard[i][j].callback_data));
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#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];
|
||||
|
||||
@@ -52,7 +52,7 @@ tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler
|
||||
json_object *message = json_object_object_get(result, "message");
|
||||
if (message) {
|
||||
tgbot_parse_message(bot, update, result);
|
||||
} else {
|
||||
} else if (cbq_handler != NULL) {
|
||||
tgbot_cbquery query;
|
||||
tgbot_parse_cbquery(bot, &query, result, cbq_handler);
|
||||
}
|
||||
@@ -212,7 +212,7 @@ tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me) {
|
||||
return TGBOT_OK;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse_mode, tgbot_inlinekeyboardmarkup **keyboard, size_t rows, size_t columns) {
|
||||
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse_mode, tgbot_inlinekeyboard *keyboard) {
|
||||
char url[1024];
|
||||
|
||||
json_object *rjson = json_object_new_object();
|
||||
@@ -221,7 +221,7 @@ tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse
|
||||
json_object_object_add(rjson, "parse_mode", json_object_new_string(parse_mode));
|
||||
|
||||
if (keyboard != NULL) {
|
||||
json_object *reply_markup = tgbot_new_inlinekeyboardmarkup(keyboard, rows, columns);
|
||||
json_object *reply_markup = tgbot_new_inlinekeyboardmarkup(keyboard);
|
||||
json_object_object_add(rjson, "reply_markup", reply_markup);
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse
|
||||
return TGBOT_OK;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, char *text, tgbot_inlinekeyboardmarkup **keyboard, size_t rows, size_t columns) {
|
||||
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, char *text, tgbot_inlinekeyboard *keyboard) {
|
||||
char url[1024];
|
||||
|
||||
json_object *rjson = json_object_new_object();
|
||||
@@ -254,7 +254,7 @@ tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, c
|
||||
snprintf(url, sizeof(url), "%seditMessageText", bot->api);
|
||||
|
||||
if (keyboard != NULL) {
|
||||
json_object *reply_markup = tgbot_new_inlinekeyboardmarkup(keyboard, rows, columns);
|
||||
json_object *reply_markup = tgbot_new_inlinekeyboardmarkup(keyboard);
|
||||
json_object_object_add(rjson, "reply_markup", reply_markup);
|
||||
}
|
||||
|
||||
|
||||
41
src/types.c
41
src/types.c
@@ -1,20 +1,41 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
tgbot_rc tgbot_allocate_inlinekeyboardmarkup(tgbot_inlinekeyboardmarkup ***keyboard, size_t rows, size_t columns) {
|
||||
*keyboard = (tgbot_inlinekeyboardmarkup **)malloc(rows * sizeof(tgbot_inlinekeyboardmarkup *));
|
||||
for (size_t i = 0; i < rows; ++i) {
|
||||
(*keyboard)[i] = (tgbot_inlinekeyboardmarkup *)malloc(columns * sizeof(tgbot_inlinekeyboardmarkup));
|
||||
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;
|
||||
}
|
||||
|
||||
return TGBOT_OK;
|
||||
memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton));
|
||||
|
||||
return keyboard;
|
||||
}
|
||||
|
||||
tgbot_rc tgbot_deallocate_inlinekeyboardmarkup(tgbot_inlinekeyboardmarkup **keyboard, size_t rows) {
|
||||
for (size_t i = 0; i < rows; ++i) {
|
||||
free(keyboard[i]);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user