add sendDice (testing)

This commit is contained in:
2025-05-22 02:56:23 +02:00
parent eebacd5fb9
commit bd63a1292e
8 changed files with 52 additions and 16 deletions

1
.clangd Normal file
View File

@@ -0,0 +1 @@
HeaderInsertion: Never

View File

@@ -49,6 +49,7 @@ You can find some examples [here](./examples/).
- `getMe` - `getMe`
- `sendMessage` - `sendMessage`
- `editMessageText` - `editMessageText`
- `sendDice`
## Roadmap ## Roadmap
- `sendPhoto` - `sendPhoto`

View File

@@ -1,6 +1,8 @@
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <tgbot.h> #include <tgbot.h>
bool run = true; bool run = true;
@@ -35,7 +37,12 @@ int main(void) {
while (run) { while (run) {
tgbot_get_update(&bot, &update, NULL); tgbot_get_update(&bot, &update, NULL);
echo_message(&bot, &update); if (strcmp(update.text, "/start") == 0) {
/* Send dice if /start otherwise echo the message */
tgbot_send_dice(&bot, update.chat_id, NULL);
} else {
echo_message(&bot, &update);
}
} }
fprintf(stdout, "Closing..."); fprintf(stdout, "Closing...");

View File

@@ -4,8 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "tgbot.h" #include <tgbot.h>
#include "types.h"
#define WELCOME_MSG "Hi there! This bot is coded in C." #define WELCOME_MSG "Hi there! This bot is coded in C."

View File

@@ -78,6 +78,8 @@ enum tgbot_rc {
TGBOT_GETUPDATES_ERROR, TGBOT_GETUPDATES_ERROR,
TGBOT_GETME_ERROR, TGBOT_GETME_ERROR,
TGBOT_SENDMESSAGE_ERROR, TGBOT_SENDMESSAGE_ERROR,
TGBOT_EDITMESSAGETEXT_ERROR,
TGBOT_SENDDICE_ERROR,
TGBOT_TELEGRAM_OK_ERROR, TGBOT_TELEGRAM_OK_ERROR,
}; };
typedef enum tgbot_rc tgbot_rc; typedef enum tgbot_rc tgbot_rc;

View File

@@ -14,9 +14,10 @@ tgbot_rc tgbot_request(tgbot *bot, char *url, struct memory_buffer **mb, json_ob
/* Methods */ /* Methods */
tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me); 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_inlinekeyboard *reply_markup); tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const char *parse_mode, tgbot_inlinekeyboard *reply_markup);
tgbot_rc tgbot_send_dice(tgbot *bot, int64_t chat_id, const char *emoji);
/* Updating Methods */ /* Updating Methods */
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, char *text, tgbot_inlinekeyboard *keyboard); tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard *keyboard);
#endif #endif

View File

@@ -24,7 +24,7 @@ lib = library(
#lib_inc_dir = include_directories('C:/include') #lib_inc_dir = include_directories('C:/include')
executable( executable(
'example', 'example',
'examples/inlinekeyboard/inlinekeyboard.c', 'examples/echobot/echobot.c',
dependencies: deps, dependencies: deps,
link_with: lib, link_with: lib,
include_directories: inc_dir, include_directories: inc_dir,

View File

@@ -212,7 +212,7 @@ tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me) {
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse_mode, tgbot_inlinekeyboard *keyboard) { tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const char *parse_mode, tgbot_inlinekeyboard *keyboard) {
char url[1024]; char url[1024];
json_object *rjson = json_object_new_object(); json_object *rjson = json_object_new_object();
@@ -230,20 +230,17 @@ tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, char *text, char *parse
struct memory_buffer *mb; struct memory_buffer *mb;
tgbot_rc ret = tgbot_request(bot, url, &mb, rjson); tgbot_rc ret = tgbot_request(bot, url, &mb, rjson);
json_object_put(rjson); json_object_put(rjson);
if (ret != TGBOT_OK) {
free(mb->data);
free(mb);
return TGBOT_SENDMESSAGE_ERROR;
}
free(mb->data); free(mb->data);
free(mb); free(mb);
if (ret != TGBOT_OK) {
return TGBOT_SENDMESSAGE_ERROR;
}
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, char *text, tgbot_inlinekeyboard *keyboard) { tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard *keyboard) {
char url[1024]; char url[1024];
json_object *rjson = json_object_new_object(); json_object *rjson = json_object_new_object();
@@ -259,10 +256,38 @@ tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, c
} }
struct memory_buffer *mb; struct memory_buffer *mb;
tgbot_request(bot, url, &mb, rjson); tgbot_rc ret = tgbot_request(bot, url, &mb, rjson);
json_object_put(rjson); json_object_put(rjson);
free(mb->data); free(mb->data);
free(mb); free(mb);
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];
json_object *rjson = json_object_new_object();
json_object_object_add(rjson, "chat_id", json_object_new_int64(chat_id));
if (emoji != NULL) {
json_object_object_add(rjson, "emoji", json_object_new_string(emoji));
}
snprintf(url, sizeof(url), "%ssendDice", bot->api);
struct memory_buffer *mb;
tgbot_rc ret = tgbot_request(bot, url, &mb, rjson);
json_object_put(rjson);
free(mb->data);
free(mb);
if (ret != TGBOT_OK) {
return TGBOT_SENDDICE_ERROR;
}
return TGBOT_OK; return TGBOT_OK;
} }