add sendDice (testing)
This commit is contained in:
@@ -49,6 +49,7 @@ You can find some examples [here](./examples/).
|
||||
- `getMe`
|
||||
- `sendMessage`
|
||||
- `editMessageText`
|
||||
- `sendDice`
|
||||
|
||||
## Roadmap
|
||||
- `sendPhoto`
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tgbot.h>
|
||||
|
||||
bool run = true;
|
||||
@@ -35,7 +37,12 @@ int main(void) {
|
||||
|
||||
while (run) {
|
||||
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...");
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "tgbot.h"
|
||||
#include "types.h"
|
||||
#include <tgbot.h>
|
||||
|
||||
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
||||
|
||||
|
||||
@@ -78,6 +78,8 @@ enum tgbot_rc {
|
||||
TGBOT_GETUPDATES_ERROR,
|
||||
TGBOT_GETME_ERROR,
|
||||
TGBOT_SENDMESSAGE_ERROR,
|
||||
TGBOT_EDITMESSAGETEXT_ERROR,
|
||||
TGBOT_SENDDICE_ERROR,
|
||||
TGBOT_TELEGRAM_OK_ERROR,
|
||||
};
|
||||
typedef enum tgbot_rc tgbot_rc;
|
||||
|
||||
@@ -14,9 +14,10 @@ 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_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 */
|
||||
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
|
||||
|
||||
@@ -24,7 +24,7 @@ lib = library(
|
||||
#lib_inc_dir = include_directories('C:/include')
|
||||
executable(
|
||||
'example',
|
||||
'examples/inlinekeyboard/inlinekeyboard.c',
|
||||
'examples/echobot/echobot.c',
|
||||
dependencies: deps,
|
||||
link_with: lib,
|
||||
include_directories: inc_dir,
|
||||
|
||||
@@ -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_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];
|
||||
|
||||
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;
|
||||
tgbot_rc ret = tgbot_request(bot, url, &mb, rjson);
|
||||
json_object_put(rjson);
|
||||
if (ret != TGBOT_OK) {
|
||||
free(mb->data);
|
||||
free(mb);
|
||||
|
||||
return TGBOT_SENDMESSAGE_ERROR;
|
||||
}
|
||||
|
||||
free(mb->data);
|
||||
free(mb);
|
||||
|
||||
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, 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];
|
||||
|
||||
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;
|
||||
tgbot_request(bot, url, &mb, rjson);
|
||||
tgbot_rc ret = tgbot_request(bot, url, &mb, rjson);
|
||||
json_object_put(rjson);
|
||||
free(mb->data);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user