improve keyboard api
This commit is contained in:
@@ -9,12 +9,8 @@ void sighandler(int signum) {
|
||||
run = false;
|
||||
}
|
||||
|
||||
void callback(tgbot *bot, tgbot_cbquery *query) {
|
||||
fprintf(stdout, "Callback called!");
|
||||
}
|
||||
|
||||
void echo_message(tgbot *bot, tgbot_update *update) {
|
||||
tgbot_send_message(bot, update->chat_id, update->text, "MARKDOWN", NULL, 0, 0);
|
||||
tgbot_send_message(bot, update->chat_id, update->text, "MARKDOWN", NULL);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
@@ -38,7 +34,7 @@ int main(void) {
|
||||
tgbot_update update;
|
||||
|
||||
while (run) {
|
||||
tgbot_get_update(&bot, &update, callback);
|
||||
tgbot_get_update(&bot, &update, NULL);
|
||||
echo_message(&bot, &update);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tgbot.h>
|
||||
#include "tgbot.h"
|
||||
#include "types.h"
|
||||
|
||||
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
||||
|
||||
@@ -14,26 +15,21 @@ void callback_parser(tgbot *bot, tgbot_cbquery *query);
|
||||
|
||||
bool run = true;
|
||||
tgbot bot;
|
||||
size_t rows = 1, columns = 2;
|
||||
tgbot_inlinekeyboardmarkup **keyboard;
|
||||
tgbot_inlinekeyboard *keyboard;
|
||||
|
||||
/* Callback handler function */
|
||||
void callback_handler(tgbot *bot, tgbot_cbquery *query) {
|
||||
if (strcmp("test-callback", query->data) == 0) {
|
||||
/* Handle `test-callback` */
|
||||
|
||||
tgbot_inlinekeyboardmarkup **home_keyboard;
|
||||
tgbot_allocate_inlinekeyboardmarkup(&home_keyboard, 1, 1);
|
||||
strncpy(home_keyboard[0][0].text, "Home", sizeof(home_keyboard[0][0].text));
|
||||
strncpy(home_keyboard[0][0].url, "", sizeof(home_keyboard[0][0].url));
|
||||
strncpy(home_keyboard[0][0].callback_data, "home", sizeof(home_keyboard[0][0].callback_data));
|
||||
|
||||
tgbot_edit_message_text(bot, query->chat_id, query->message_id, "Callback called!", home_keyboard, 1, 1);
|
||||
tgbot_deallocate_inlinekeyboardmarkup(home_keyboard, 1);
|
||||
tgbot_inlinekeyboard *home_keyboard = tgbot_new_inlinekeyboard(1, 1);
|
||||
/* Add buttons */
|
||||
tgbot_inlinekeyboard_button(home_keyboard, 0, 0, "Home", "", "home");
|
||||
tgbot_edit_message_text(bot, query->chat_id, query->message_id, "Callback called!", home_keyboard);
|
||||
tgbot_destroy_inlinekeyboard(home_keyboard);
|
||||
|
||||
return;
|
||||
} else if (strcmp("home", query->data) == 0) {
|
||||
tgbot_edit_message_text(bot, query->chat_id, query->message_id, WELCOME_MSG, keyboard, rows, columns);
|
||||
tgbot_edit_message_text(bot, query->chat_id, query->message_id, WELCOME_MSG, keyboard);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -48,26 +44,7 @@ void parse_command(tgbot *bot, tgbot_update *update) {
|
||||
tgbot_rc ret;
|
||||
|
||||
if (strcmp("/start", update->text) == 0) {
|
||||
/* Example of an InlineKeyboardMarkup */
|
||||
/* Check global variables */
|
||||
tgbot_allocate_inlinekeyboardmarkup(&keyboard, rows, columns);
|
||||
|
||||
/* Populate the InlineKeyboardMarkup */
|
||||
strncpy(keyboard[0][0].text, "Google", sizeof(keyboard[1][0].text));
|
||||
strncpy(keyboard[0][0].url, "https://google.com", sizeof(keyboard[1][0].url));
|
||||
strncpy(keyboard[0][0].callback_data, "", sizeof(keyboard[1][0].callback_data));
|
||||
|
||||
strncpy(keyboard[0][1].text, "Callback", sizeof(keyboard[0][1].text));
|
||||
strncpy(keyboard[0][1].url, "", sizeof(keyboard[0][1].url));
|
||||
strncpy(keyboard[0][1].callback_data, "test-callback", sizeof(keyboard[0][1].callback_data));
|
||||
|
||||
/* If you want 3 buttons on 2 rows, for example on the first row 2 buttons and on the second only one */
|
||||
/* you have to put rows = 2, columns = 2 and pass an empty string to .text field */
|
||||
|
||||
ret = tgbot_send_message(bot, update->chat_id, WELCOME_MSG, "Markdown", keyboard, rows, columns);
|
||||
|
||||
/* Deallocate the keyboard */
|
||||
/* See sighandler() */
|
||||
ret = tgbot_send_message(bot, update->chat_id, WELCOME_MSG, "Markdown", keyboard);
|
||||
|
||||
if (ret != TGBOT_OK) {
|
||||
fprintf(stderr, "Failed to send message\n");
|
||||
@@ -77,7 +54,7 @@ void parse_command(tgbot *bot, tgbot_update *update) {
|
||||
}
|
||||
|
||||
/* Echo the message */
|
||||
ret = tgbot_send_message(bot, update->chat_id, update->text, "Markdown", NULL, 0, 0);
|
||||
ret = tgbot_send_message(bot, update->chat_id, update->text, "Markdown", NULL);
|
||||
if (ret != TGBOT_OK) {
|
||||
fprintf(stderr, "Failed to send message\n");
|
||||
}
|
||||
@@ -110,6 +87,18 @@ int main(void) {
|
||||
fprintf(stdout, "Running Telegram bot...\nPress Ctrl-C to close.\n");
|
||||
|
||||
tgbot_update update;
|
||||
/* Allocate the new inline keyboard (remember to free!) */
|
||||
keyboard = tgbot_new_inlinekeyboard(1, 2);
|
||||
if (keyboard == NULL) {
|
||||
tgbot_destroy(&bot);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Populate the InlineKeyboardMarkup */
|
||||
tgbot_inlinekeyboard_button(keyboard, 0, 0, "Google", "https://google.com", "");
|
||||
tgbot_inlinekeyboard_button(keyboard, 0, 1, "Callback", "", "test-callback");
|
||||
/* If you want 3 buttons on 2 rows, for example on the first row 2 buttons and on the second only one */
|
||||
/* you have to put rows = 2, columns = 2 and pass an empty string to .text field */
|
||||
|
||||
/* Main loop */
|
||||
while (run) {
|
||||
@@ -124,8 +113,8 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
tgbot_destroy_inlinekeyboard(keyboard);
|
||||
tgbot_destroy(&bot);
|
||||
tgbot_deallocate_inlinekeyboardmarkup(keyboard, rows);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@
|
||||
#include "types.h"
|
||||
#include <json-c/json.h>
|
||||
|
||||
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboardmarkup **keyboard, size_t rows, size_t columns);
|
||||
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboard *keyboard);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -14,9 +14,9 @@ tgbot_rc tgbot_request(tgbot *bot, char *url, struct memory_buffer **mb, json_ob
|
||||
|
||||
/* Methods */
|
||||
tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me);
|
||||
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse_mode, tgbot_inlinekeyboardmarkup **reply_markup, size_t rows, size_t columns);
|
||||
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse_mode, tgbot_inlinekeyboard *reply_markup);
|
||||
|
||||
/* Updating Methods */
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,17 +4,25 @@
|
||||
#include "common.h"
|
||||
|
||||
/**
|
||||
* A structure to represent InlineKeyboardMarkup
|
||||
* A structure to represent keyboard's buttons
|
||||
*/
|
||||
struct tgbot_inlinekeyboardmarkup_t {
|
||||
struct tgbot_inlinekeyboardbutton_t {
|
||||
char text[200]; /**< If this field is empty the button will be skipped */
|
||||
char url[200]; /**< (Optional) URL of the button */
|
||||
char callback_data[64]; /**< Callback data */
|
||||
};
|
||||
typedef struct tgbot_inlinekeyboardmarkup_t tgbot_inlinekeyboardmarkup;
|
||||
typedef struct tgbot_inlinekeyboardbutton_t tgbot_inlinekeyboardbutton;
|
||||
|
||||
tgbot_rc
|
||||
tgbot_allocate_inlinekeyboardmarkup(tgbot_inlinekeyboardmarkup ***keyboard, size_t rows, size_t columns);
|
||||
tgbot_rc tgbot_deallocate_inlinekeyboardmarkup(tgbot_inlinekeyboardmarkup **keyboard, size_t rows);
|
||||
struct tgbot_inlinekeyboard_t {
|
||||
size_t rows;
|
||||
size_t columns;
|
||||
struct tgbot_inlinekeyboardbutton_t *buttons;
|
||||
};
|
||||
typedef struct tgbot_inlinekeyboard_t tgbot_inlinekeyboard;
|
||||
|
||||
tgbot_inlinekeyboard *tgbot_new_inlinekeyboard(size_t rows, size_t columns);
|
||||
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 *tgbot_inlinekeyboard_button_at(tgbot_inlinekeyboard *keyboard, size_t row, size_t column);
|
||||
void tgbot_destroy_inlinekeyboard(tgbot_inlinekeyboard *keyboard);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,8 +10,6 @@ json_c_dep = dependency('json-c')
|
||||
|
||||
deps = [curl_dep, json_c_dep]
|
||||
|
||||
add_project_arguments('-O2', language: 'c')
|
||||
|
||||
install_headers('include/tgbot.h')
|
||||
#install_subdir('.', install_dir: 'C:/tgbot')
|
||||
lib = library(
|
||||
@@ -23,12 +21,11 @@ lib = library(
|
||||
)
|
||||
|
||||
# Example (windows)
|
||||
lib_inc_dir = include_directories('C:/include')
|
||||
#lib_inc_dir = include_directories('C:/include')
|
||||
executable(
|
||||
'example',
|
||||
'examples/echobot/echobot.c',
|
||||
'examples/inlinekeyboard/inlinekeyboard.c',
|
||||
dependencies: deps,
|
||||
link_with: lib,
|
||||
include_directories: lib_inc_dir,
|
||||
win_subsystem: 'console',
|
||||
include_directories: inc_dir,
|
||||
)
|
||||
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