* [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates
@ 2025-12-15 18:23 Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 1/4] .vscode: add some C/C++ extension settings Muhammad Rizki
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Muhammad Rizki @ 2025-12-15 18:23 UTC (permalink / raw)
To: Ammar Faizi
Cc: Muhammad Rizki, Alviro Iskandar Setiawan, GNU/Weeb Mailing List
Hello, it's been a long time since my last contribution to GNUWeeb Community,
so here are some fixes and new updates for GNUWeebBot2 that I've made.
### Changelog
- Fixes memory leak in lib/curl.c and reduce threads usage in core/ring.c
- Add new properties for type User in lib/tgapi.h and lib/tgapi.c
- Create a new module `user` to get user information who invoked the command
Please review and test, thanks!
Muhammad Rizki (4):
.vscode: add some C/C++ extension settings
core:lib: fix memory leak and reduce threads
lib/tgapi: add new properties for type User
modules: create a `user` module
.vscode/settings.json | 10 +++-
configure | 1 +
core/ring.c | 4 +-
include/gw/lib/tgapi.h | 28 +++++++++-
lib/curl.c | 4 +-
lib/tgapi.c | 44 +++++++++++++---
modules/Makefile | 1 +
modules/user/Makefile | 7 +++
modules/user/mod_info.h | 8 +++
modules/user/user.c | 112 ++++++++++++++++++++++++++++++++++++++++
10 files changed, 207 insertions(+), 12 deletions(-)
create mode 100644 modules/user/Makefile
create mode 100644 modules/user/mod_info.h
create mode 100644 modules/user/user.c
base-commit: 3cd2665e15dce7125286f0806d3e788400bea9ea
--
Muhammad Rizki
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v1 1/4] .vscode: add some C/C++ extension settings
2025-12-15 18:23 [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Muhammad Rizki
@ 2025-12-15 18:23 ` Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 2/4] core:lib: fix memory leak and reduce threads Muhammad Rizki
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Muhammad Rizki @ 2025-12-15 18:23 UTC (permalink / raw)
To: Ammar Faizi
Cc: Muhammad Rizki, Alviro Iskandar Setiawan, GNU/Weeb Mailing List
Added some settings in .vscode/settings.json, below are the explanations:
**files.trimTrailingWhitespace**
Remove trailing whitespace on save.
**files.insertFinalNewline**
Ensure file ends with a newline on save even there is already a newline.
**files.trimFinalNewlines**
Remove final newlines at the end of file on save, this will
ensure there is only a single newline at the end of file.
**files.eol**
Set default end of line character to LF, not CRLF, this is the best
practice to make diff view cleaner.
**C_Cpp.inlayHints.parameterNames.enabled**
**C_Cpp.inlayHints.autoDeclarationTypes.enabled**
**C_Cpp.inlayHints.referenceOperator.enabled**
These 3 aboves enable inlay hints for better code readability.
**C_Cpp.clang_format_fallbackStyle**
This one force VSCode workspace settings to use existing coding
style.
Signed-off-by: Muhammad Rizki <kiizuha@gnuweeb.org>
---
.vscode/settings.json | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/.vscode/settings.json b/.vscode/settings.json
index ccd2c39..6452454 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -3,6 +3,10 @@
"editor.insertSpaces": false,
"editor.detectIndentation": false,
"editor.rulers": [80],
+ "files.trimTrailingWhitespace": true,
+ "files.insertFinalNewline": true,
+ "files.trimFinalNewlines": true,
+ "files.eol": "\n",
"files.associations": {
"string_view": "cpp",
"thread": "cpp",
@@ -58,5 +62,9 @@
"typeinfo": "cpp",
"curl.h": "c"
},
- "C_Cpp.errorSquiggles": "disabled"
+ "C_Cpp.errorSquiggles": "disabled",
+ "C_Cpp.inlayHints.parameterNames.enabled": true,
+ "C_Cpp.inlayHints.autoDeclarationTypes.enabled": true,
+ "C_Cpp.inlayHints.referenceOperator.enabled": true,
+ "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Visual Studio, IndentWidth: 8 }"
}
--
Muhammad Rizki
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v1 2/4] core:lib: fix memory leak and reduce threads
2025-12-15 18:23 [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 1/4] .vscode: add some C/C++ extension settings Muhammad Rizki
@ 2025-12-15 18:23 ` Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 3/4] lib/tgapi: add new properties for type User Muhammad Rizki
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Muhammad Rizki @ 2025-12-15 18:23 UTC (permalink / raw)
To: Ammar Faizi
Cc: Muhammad Rizki, Alviro Iskandar Setiawan, GNU/Weeb Mailing List
This commit fixes a memory leak in the lib/curl.c file where the
`current_thread_curl_handle` keeps creating a new thread cURL handles
overtime.
This commit also reduces the number of threads and max pending works in
core/ring.c.
Signed-off-by: Muhammad Rizki <kiizuha@gnuweeb.org>
Tested-by: Muhammad Rizki <kiizuha@gnuweeb.org>
Reported-by: Muhammad Rizki <kiizuha@gnuweeb.org>
---
core/ring.c | 4 ++--
lib/curl.c | 4 +++-
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/core/ring.c b/core/ring.c
index 9497cfd..fedec52 100644
--- a/core/ring.c
+++ b/core/ring.c
@@ -25,8 +25,8 @@ int gw_ring_init(struct gw_ring *ring, uint32_t size)
.name = "gw-ring-wq",
.flags = WQ_F_LAZY_THREAD_CREATION,
.max_threads = 1024u,
- .min_threads = 32u,
- .max_pending_works = 4096u,
+ .min_threads = 1u,
+ .max_pending_works = 128u,
};
uint32_t max = 2u;
int ret;
diff --git a/lib/curl.c b/lib/curl.c
index 09745d0..78606b9 100644
--- a/lib/curl.c
+++ b/lib/curl.c
@@ -29,7 +29,7 @@ int gw_curl_global_init(long flags)
if (curl_global_init(flags) != CURLE_OK)
return -ENOMEM;
-
+
cd = malloc(sizeof(*g_gw_curl_data));
if (!cd) {
ret = -ENOMEM;
@@ -108,6 +108,8 @@ void *gw_curl_thread_init(void)
return NULL;
}
}
+
+ current_thread_curl_handle = ret;
cd->handles[cd->nr_handles++] = ret;
mutex_unlock(&cd->mutex);
return ret;
--
Muhammad Rizki
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v1 3/4] lib/tgapi: add new properties for type User
2025-12-15 18:23 [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 1/4] .vscode: add some C/C++ extension settings Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 2/4] core:lib: fix memory leak and reduce threads Muhammad Rizki
@ 2025-12-15 18:23 ` Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 4/4] modules: create a `user` module Muhammad Rizki
2025-12-18 22:16 ` [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Ammar Faizi
4 siblings, 0 replies; 6+ messages in thread
From: Muhammad Rizki @ 2025-12-15 18:23 UTC (permalink / raw)
To: Ammar Faizi
Cc: Muhammad Rizki, Alviro Iskandar Setiawan, GNU/Weeb Mailing List
Added new properties for type User:
- is_premium
- can_read_all_group_messages
- can_connect_to_business
- supports_inline_queries
- added_to_attachment_menu
- has_main_web_app
Fixes typo:
- can_join_group -> can_join_groups
Oficial documentation says the correct field name of `can_join_groups`
is with (s) at the end.
This commit also supports `parse_mode`, previously, we cannot use
`parse_mode` when sending messages. Now we can use it to format
messages with Markdown, MarkdownV2 or HTML.
Signed-off-by: Muhammad Rizki <kiizuha@gnuweeb.org>
---
include/gw/lib/tgapi.h | 28 ++++++++++++++++++++++++++-
lib/tgapi.c | 44 +++++++++++++++++++++++++++++++++++-------
2 files changed, 64 insertions(+), 8 deletions(-)
diff --git a/include/gw/lib/tgapi.h b/include/gw/lib/tgapi.h
index 1a35a65..836e329 100644
--- a/include/gw/lib/tgapi.h
+++ b/include/gw/lib/tgapi.h
@@ -19,14 +19,40 @@ typedef uint64_t time64_t;
*/
struct tg_user {
uint64_t id;
+
const char *first_name;
+
+ // optional
const char *last_name;
+
+ // optional
const char *username;
+
+ // optional
const char *language_code;
+
bool is_bot;
- bool can_join_group;
+
+ // optional
+ bool is_premium;
+
+ // optional: only in getMe
+ bool can_join_groups;
+
+ // optional: only in getMe
bool can_read_all_group_messages;
+
+ // optional: only in getMe
+ bool can_connect_to_business;
+
+ // optional: only in getMe
bool supports_inline_queries;
+
+ // optional
+ bool added_to_attachment_menu;
+
+ // optional: only in getMe
+ bool has_main_web_app;
};
/*
diff --git a/lib/tgapi.c b/lib/tgapi.c
index 1c3a380..5ecab52 100644
--- a/lib/tgapi.c
+++ b/lib/tgapi.c
@@ -67,6 +67,27 @@ static int tgj_get_user(struct tg_user *user, json_object *juser)
if (json_object_object_get_ex(juser, "is_bot", &res))
user->is_bot = json_object_get_boolean(res) ? true : false;
+ if (json_object_object_get_ex(juser, "is_premium", &res))
+ user->is_premium = json_object_get_boolean(res) ? true : false;
+
+ if (json_object_object_get_ex(juser, "can_join_groups", &res))
+ user->can_join_groups = json_object_get_boolean(res) ? true : false;
+
+ if (json_object_object_get_ex(juser, "can_read_all_group_messages", &res))
+ user->can_read_all_group_messages = json_object_get_boolean(res) ? true : false;
+
+ if (json_object_object_get_ex(juser, "can_connect_to_business", &res))
+ user->can_connect_to_business = json_object_get_boolean(res) ? true : false;
+
+ if (json_object_object_get_ex(juser, "supports_inline_queries", &res))
+ user->supports_inline_queries = json_object_get_boolean(res) ? true : false;
+
+ if (json_object_object_get_ex(juser, "added_to_attachment_menu", &res))
+ user->added_to_attachment_menu = json_object_get_boolean(res) ? true : false;
+
+ if (json_object_object_get_ex(juser, "has_main_web_app", &res))
+ user->has_main_web_app = json_object_get_boolean(res) ? true : false;
+
return 0;
}
@@ -177,7 +198,7 @@ static int tgj_get_message_entities(struct tg_msg_entity *ent,
continue;
ent[i].url = json_object_get_string(res);
}
-
+
#endif
return (int)len;
}
@@ -232,7 +253,7 @@ static int tgj_arrange_message_variant(struct tg_message *msg,
ret = tgj_get_entities_and_alloc(&msg->entities, res);
if (unlikely(ret < 0))
return ret;
-
+
msg->entities_len = (size_t)ret;
return 0;
}
@@ -659,11 +680,20 @@ int tgapi_call_send_message(struct tg_api_ctx *ctx,
if (!escape_text)
return -ENOMEM;
- snprintf(url, sizeof(url),
- "https://api.telegram.org/bot%s/sendMessage?chat_id=%" PRId64
- "&text=%s&reply_to_message_id=%" PRId64,
- ctx->token, call->chat_id, escape_text,
- call->reply_to_message_id);
+ if (call->parse_mode && call->parse_mode[0] != '\0') {
+ snprintf(url, sizeof(url),
+ "https://api.telegram.org/bot%s/sendMessage?chat_id=%" PRId64
+ "&text=%s&parse_mode=%s&reply_to_message_id=%" PRId64,
+ ctx->token, call->chat_id, escape_text,
+ call->parse_mode, call->reply_to_message_id);
+ } else {
+ snprintf(url, sizeof(url),
+ "https://api.telegram.org/bot%s/sendMessage?chat_id=%" PRId64
+ "&text=%s&reply_to_message_id=%" PRId64,
+ ctx->token, call->chat_id, escape_text,
+ call->reply_to_message_id);
+ }
+
curl_free(escape_text);
printf("Curl to URL: %s\n", url);
--
Muhammad Rizki
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v1 4/4] modules: create a `user` module
2025-12-15 18:23 [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Muhammad Rizki
` (2 preceding siblings ...)
2025-12-15 18:23 ` [PATCH v1 3/4] lib/tgapi: add new properties for type User Muhammad Rizki
@ 2025-12-15 18:23 ` Muhammad Rizki
2025-12-18 22:16 ` [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Ammar Faizi
4 siblings, 0 replies; 6+ messages in thread
From: Muhammad Rizki @ 2025-12-15 18:23 UTC (permalink / raw)
To: Ammar Faizi
Cc: Muhammad Rizki, Alviro Iskandar Setiawan, GNU/Weeb Mailing List
This module provides a `/user` command that retrieves and displays
information about the user who invoked the command, this uses
`msg->from` field to get the user's information.
Signed-off-by: Muhammad Rizki <kiizuha@gnuweeb.org>
---
configure | 1 +
modules/Makefile | 1 +
modules/user/Makefile | 7 +++
modules/user/mod_info.h | 8 +++
modules/user/user.c | 112 ++++++++++++++++++++++++++++++++++++++++
5 files changed, 129 insertions(+)
create mode 100644 modules/user/Makefile
create mode 100644 modules/user/mod_info.h
create mode 100644 modules/user/user.c
diff --git a/configure b/configure
index 2f93056..cd4f39e 100755
--- a/configure
+++ b/configure
@@ -317,6 +317,7 @@ if test "${use_cpp_thread}" = "yes"; then
fi;
add_config "CONFIG_MODULE_PING";
+add_config "CONFIG_MODULE_USER";
add_make_var "CC" "${cc}";
add_make_var "CXX" "${cxx}";
diff --git a/modules/Makefile b/modules/Makefile
index 1cb9985..7cc2e77 100644
--- a/modules/Makefile
+++ b/modules/Makefile
@@ -7,6 +7,7 @@ OBJ_CC += \
GW_MODULE_LIST :=
include $(BASE_DIR)/modules/ping/Makefile
+include $(BASE_DIR)/modules/user/Makefile
$(BASE_DIR)/modules/module.o: $(BASE_DIR)/modules/module_list.h
$(BASE_DIR)/modules/module.o: $(BASE_DIR)/modules/module_table.h
diff --git a/modules/user/Makefile b/modules/user/Makefile
new file mode 100644
index 0000000..0f0c61b
--- /dev/null
+++ b/modules/user/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+ifeq ($(CONFIG_MODULE_USER),y)
+DEP_DIRS += $(BASE_DEP_DIR)/modules/user
+GW_MODULE_LIST += user
+OBJ_CC += $(BASE_DIR)/modules/user/user.o
+endif
diff --git a/modules/user/mod_info.h b/modules/user/mod_info.h
new file mode 100644
index 0000000..e60a841
--- /dev/null
+++ b/modules/user/mod_info.h
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Muhammad Rizki <kiizuha@gnuweeb.org>
+ */
+
+#include <gw/module.h>
+
+extern struct gw_bot_module gw_mod_info_user;
diff --git a/modules/user/user.c b/modules/user/user.c
new file mode 100644
index 0000000..4e4809e
--- /dev/null
+++ b/modules/user/user.c
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Muhammad Rizki <kiizuha@gnuweeb.org>
+ */
+
+#include "mod_info.h"
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <json-c/json.h>
+
+#define APPEND(fmt, ...) \
+ snprintf(msg_buf + strlen(msg_buf), \
+ sizeof(msg_buf) - strlen(msg_buf), \
+ fmt, ##__VA_ARGS__)
+
+static int user_init(struct tg_bot_ctx *ctx)
+{
+ (void)ctx;
+ return 0;
+}
+
+static int __user_handle(struct tg_bot_ctx *ctx, struct tg_message *msg)
+{
+ char msg_buf[1024] = "*User Info*\n```sh\n";
+
+ APPEND("ID : %" PRIu64 "\n", msg->from->id);
+ APPEND("First Name : %s\n", msg->from->first_name);
+
+ if (msg->from->last_name)
+ APPEND("Last Name : %s\n", msg->from->last_name);
+
+ if (msg->from->username)
+ APPEND("Username : %s\n", msg->from->username);
+
+ if (msg->from->language_code)
+ APPEND("Language Code : %s\n", msg->from->language_code);
+
+ APPEND("Is Bot : %s\n",
+ msg->from->is_bot ? "Yes" : "No");
+ APPEND("Is Premium : %s\n",
+ msg->from->is_premium ? "Yes" : "No");
+ APPEND("Can Join Groups : %s\n",
+ msg->from->can_join_groups ? "Yes" : "No");
+ APPEND("Can Read All Groups : %s\n",
+ msg->from->can_read_all_group_messages ? "Yes" : "No");
+ APPEND("Can Connect Business : %s\n",
+ msg->from->can_connect_to_business ? "Yes" : "No");
+ APPEND("Supports Inline Query : %s\n",
+ msg->from->supports_inline_queries ? "Yes" : "No");
+ APPEND("Added Attachment Menu : %s\n",
+ msg->from->added_to_attachment_menu ? "Yes" : "No");
+ APPEND("Has Main Web App : %s\n",
+ msg->from->has_main_web_app ? "Yes" : "No");
+ APPEND("```\n");
+
+
+ const struct tga_call_send_message call = {
+ .chat_id = msg->chat->id,
+ .text = msg_buf,
+ .parse_mode = "MarkdownV2",
+ .disable_web_page_preview = true,
+ .disable_notification = true,
+ .reply_to_message_id = msg->message_id,
+ .reply_markup = NULL,
+ };
+
+ return tgapi_call_send_message(&ctx->tctx, &call);
+}
+
+/*
+ * Commands: /user, .user, !user
+ * Description: Get user information who invoked the command.
+ */
+static int user_handle(struct tg_bot_ctx *ctx, struct tg_update *up)
+{
+ const char *text;
+
+ assert(up->type & TG_UPDATE_MESSAGE);
+
+ if (up->type != TG_UPDATE_MESSAGE)
+ return 0;
+
+ if (up->message.type != TG_MSG_TEXT)
+ return 0;
+
+ text = up->message.text;
+ if (!text)
+ return 0;
+
+ if (!(text[0] == '/' || text[0] == '!' || text[0] == '.'))
+ return 0;
+
+ if (!strcmp(&text[1], "user"))
+ return __user_handle(ctx, &up->message);
+
+ return 0;
+}
+
+static void user_shutdown(struct tg_bot_ctx *ctx)
+{
+ (void)ctx;
+}
+
+struct gw_bot_module gw_mod_info_user = {
+ .name = "user",
+ .listen_update_types = TG_UPDATE_MESSAGE,
+ .init = user_init,
+ .handle = user_handle,
+ .shutdown = user_shutdown,
+};
--
Muhammad Rizki
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates
2025-12-15 18:23 [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Muhammad Rizki
` (3 preceding siblings ...)
2025-12-15 18:23 ` [PATCH v1 4/4] modules: create a `user` module Muhammad Rizki
@ 2025-12-18 22:16 ` Ammar Faizi
4 siblings, 0 replies; 6+ messages in thread
From: Ammar Faizi @ 2025-12-18 22:16 UTC (permalink / raw)
To: Muhammad Rizki; +Cc: Alviro Iskandar Setiawan, GNU/Weeb Mailing List
On Tue, Dec 16, 2025 at 01:23:02AM +0700, Muhammad Rizki wrote:
> Hello, it's been a long time since my last contribution to GNUWeeb Community,
> so here are some fixes and new updates for GNUWeebBot2 that I've made.
Hello,
I halted the development of that project. I think maintaining a fast
moving Telegram bot library when I can't move fast is not productive.
So, instead of developing a Telegram bot library ourselves, we had
better use an external actively maintained library and contribute to
it. I recently contributed to this Telegram bot library:
https://github.com/reo7sp/tgbot-cpp/pull/331
It's also the Telegram bot library I am actively using today. While it
may not fully cover the latest upstream Telegram Bot API methods and
structs, at least we have a good enough framework to keep up the
upstream. You may want to examine the header files here to see how
kept up it is with the upstream:
https://github.com/reo7sp/tgbot-cpp/tree/master/include/tgbot/types
I think the current state of that library is adequate to create a
Telegram bot with rich features. So, my recommendation is: Let's use
that library and contribute to it.
Another good thing about using an external library is that, we have
more users, so we have more coverage. People report bugs that they find.
It helps us discover problems that we may not be aware yet.
Thanks,
--
Ammar Faizi
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-12-18 22:16 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-15 18:23 [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 1/4] .vscode: add some C/C++ extension settings Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 2/4] core:lib: fix memory leak and reduce threads Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 3/4] lib/tgapi: add new properties for type User Muhammad Rizki
2025-12-15 18:23 ` [PATCH v1 4/4] modules: create a `user` module Muhammad Rizki
2025-12-18 22:16 ` [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Ammar Faizi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox