public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Muhammad Rizki <kiizuha@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Muhammad Rizki <kiizuha@gnuweeb.org>,
	Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH v1 4/4] modules: create a `user` module
Date: Tue, 16 Dec 2025 01:23:06 +0700	[thread overview]
Message-ID: <20251215182308.48766-5-kiizuha@gnuweeb.org> (raw)
In-Reply-To: <20251215182308.48766-1-kiizuha@gnuweeb.org>

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


  parent reply	other threads:[~2025-12-15 18:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Muhammad Rizki [this message]
2025-12-18 22:16 ` [PATCH v1 0/4] GNUWeebBot2 Fixes and New Updates Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251215182308.48766-5-kiizuha@gnuweeb.org \
    --to=kiizuha@gnuweeb.org \
    --cc=alviro.iskandar@gnuweeb.org \
    --cc=ammarfaizi2@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox