From 00a07a4de190edfad5f16e22e238c4b5c37e2cba Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Fri, 26 Jul 2024 17:26:39 +0300 Subject: Added msginfo command --- src/Bot.zig | 13 +++---------- src/main.zig | 17 +++++++++++++++++ src/textutils.zig | 16 ---------------- src/types/User.zig | 8 ++++---- src/utils.zig | 24 ++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 src/textutils.zig create mode 100644 src/utils.zig diff --git a/src/Bot.zig b/src/Bot.zig index 45717e1..0346d2c 100644 --- a/src/Bot.zig +++ b/src/Bot.zig @@ -1,5 +1,6 @@ -const types = @import("types.zig"); const std = @import("std"); +const types = @import("types.zig"); +const utils = @import("utils.zig"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; @@ -176,14 +177,6 @@ fn call( } } -inline fn isNull(value: anytype) bool { - return switch (@typeInfo(@TypeOf(value))) { - .Null => true, - .Optional => value == null, - else => false, - }; -} - fn intoQueryString(allocator: Allocator, data: anytype) !?[]u8 { return switch (@typeInfo(@TypeOf(data))) { .Null => null, @@ -195,7 +188,7 @@ fn intoQueryString(allocator: Allocator, data: anytype) !?[]u8 { var counter: usize = 0; inline for (s.fields) |field| { - if (!isNull(@field(data, field.name))) { + if (!utils.isNull(@field(data, field.name))) { counter += 1; try sb.ensureUnusedCapacity(field.name.len + 2); diff --git a/src/main.zig b/src/main.zig index 4bd15fe..203f4ab 100644 --- a/src/main.zig +++ b/src/main.zig @@ -324,6 +324,23 @@ fn onTextCommand(bot: *Bot, msg: types.Message, text: []const u8, cmd: []const u .chat_id = msg.chat.id, }, }); + } else if (std.mem.eql(u8, simple_cmd, "msginfo")) { + if (msg.reply_to_message) |replied| { + const str_data = try std.json.stringifyAlloc(bot.allocator, replied.*, .{ + .whitespace = .indent_2, + .emit_null_optional_fields = false, + }); + defer bot.allocator.free(str_data); + + try bot.sendMessage_(.{ + .chat_id = msg.chat.id, + .text = str_data, + .reply_parameters = .{ + .message_id = msg.message_id, + .chat_id = msg.chat.id, + }, + }); + } } else if (std.mem.eql(u8, simple_cmd, "ping")) { var timer = try std.time.Timer.start(); diff --git a/src/textutils.zig b/src/textutils.zig deleted file mode 100644 index 41dd5f5..0000000 --- a/src/textutils.zig +++ /dev/null @@ -1,16 +0,0 @@ -const std = @import("std"); - -const Allocator = std.mem.Allocator; -const ArrayList = std.ArrayList; - -pub fn escapeXml(writer: anytype, text: []const u8) !void { - for (text) |ch| { - try switch (ch) { - '<' => writer.writeAll("<"), - '>' => writer.writeAll(">"), - '&' => writer.writeAll("&"), - '"' => writer.writeAll("""), - else => writer.writeByte(ch), - }; - } -} diff --git a/src/types/User.zig b/src/types/User.zig index dc06097..2641b0f 100644 --- a/src/types/User.zig +++ b/src/types/User.zig @@ -1,4 +1,4 @@ -const textutils = @import("../textutils.zig"); +const utils = @import("../utils.zig"); const User = @This(); @@ -17,16 +17,16 @@ can_connect_to_business: bool = false, pub fn writeFormattedName(self: User, w: anytype) !void { try w.print("", .{self.id}); - try textutils.escapeXml(w, self.first_name); + try utils.escapeXml(w, self.first_name); if (self.last_name) |last_name| { try w.writeByte(' '); - try textutils.escapeXml(w, last_name); + try utils.escapeXml(w, last_name); } try w.writeAll(""); if (self.username) |username| { try w.writeAll(" @"); - try textutils.escapeXml(w, username); + try utils.escapeXml(w, username); } try w.print(" [{}]", .{self.id}); } diff --git a/src/utils.zig b/src/utils.zig new file mode 100644 index 0000000..c6e8508 --- /dev/null +++ b/src/utils.zig @@ -0,0 +1,24 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; +const ArrayList = std.ArrayList; + +pub fn escapeXml(writer: anytype, text: []const u8) !void { + for (text) |ch| { + try switch (ch) { + '<' => writer.writeAll("<"), + '>' => writer.writeAll(">"), + '&' => writer.writeAll("&"), + '"' => writer.writeAll("""), + else => writer.writeByte(ch), + }; + } +} + +pub inline fn isNull(value: anytype) bool { + return switch (@typeInfo(@TypeOf(value))) { + .Null => true, + .Optional => value == null, + else => false, + }; +} -- cgit v1.2.3