diff options
| -rw-r--r-- | src/Bot.zig | 13 | ||||
| -rw-r--r-- | src/main.zig | 17 | ||||
| -rw-r--r-- | src/types/User.zig | 8 | ||||
| -rw-r--r-- | src/utils.zig (renamed from src/textutils.zig) | 8 |
4 files changed, 32 insertions, 14 deletions
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 @@ | |||
| 1 | const types = @import("types.zig"); | ||
| 2 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const types = @import("types.zig"); | ||
| 3 | const utils = @import("utils.zig"); | ||
| 3 | 4 | ||
| 4 | const Allocator = std.mem.Allocator; | 5 | const Allocator = std.mem.Allocator; |
| 5 | const ArrayList = std.ArrayList; | 6 | const ArrayList = std.ArrayList; |
| @@ -176,14 +177,6 @@ fn call( | |||
| 176 | } | 177 | } |
| 177 | } | 178 | } |
| 178 | 179 | ||
| 179 | inline fn isNull(value: anytype) bool { | ||
| 180 | return switch (@typeInfo(@TypeOf(value))) { | ||
| 181 | .Null => true, | ||
| 182 | .Optional => value == null, | ||
| 183 | else => false, | ||
| 184 | }; | ||
| 185 | } | ||
| 186 | |||
| 187 | fn intoQueryString(allocator: Allocator, data: anytype) !?[]u8 { | 180 | fn intoQueryString(allocator: Allocator, data: anytype) !?[]u8 { |
| 188 | return switch (@typeInfo(@TypeOf(data))) { | 181 | return switch (@typeInfo(@TypeOf(data))) { |
| 189 | .Null => null, | 182 | .Null => null, |
| @@ -195,7 +188,7 @@ fn intoQueryString(allocator: Allocator, data: anytype) !?[]u8 { | |||
| 195 | var counter: usize = 0; | 188 | var counter: usize = 0; |
| 196 | 189 | ||
| 197 | inline for (s.fields) |field| { | 190 | inline for (s.fields) |field| { |
| 198 | if (!isNull(@field(data, field.name))) { | 191 | if (!utils.isNull(@field(data, field.name))) { |
| 199 | counter += 1; | 192 | counter += 1; |
| 200 | 193 | ||
| 201 | try sb.ensureUnusedCapacity(field.name.len + 2); | 194 | 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 | |||
| 324 | .chat_id = msg.chat.id, | 324 | .chat_id = msg.chat.id, |
| 325 | }, | 325 | }, |
| 326 | }); | 326 | }); |
| 327 | } else if (std.mem.eql(u8, simple_cmd, "msginfo")) { | ||
| 328 | if (msg.reply_to_message) |replied| { | ||
| 329 | const str_data = try std.json.stringifyAlloc(bot.allocator, replied.*, .{ | ||
| 330 | .whitespace = .indent_2, | ||
| 331 | .emit_null_optional_fields = false, | ||
| 332 | }); | ||
| 333 | defer bot.allocator.free(str_data); | ||
| 334 | |||
| 335 | try bot.sendMessage_(.{ | ||
| 336 | .chat_id = msg.chat.id, | ||
| 337 | .text = str_data, | ||
| 338 | .reply_parameters = .{ | ||
| 339 | .message_id = msg.message_id, | ||
| 340 | .chat_id = msg.chat.id, | ||
| 341 | }, | ||
| 342 | }); | ||
| 343 | } | ||
| 327 | } else if (std.mem.eql(u8, simple_cmd, "ping")) { | 344 | } else if (std.mem.eql(u8, simple_cmd, "ping")) { |
| 328 | var timer = try std.time.Timer.start(); | 345 | var timer = try std.time.Timer.start(); |
| 329 | 346 | ||
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 @@ | |||
| 1 | const textutils = @import("../textutils.zig"); | 1 | const utils = @import("../utils.zig"); |
| 2 | 2 | ||
| 3 | const User = @This(); | 3 | const User = @This(); |
| 4 | 4 | ||
| @@ -17,16 +17,16 @@ can_connect_to_business: bool = false, | |||
| 17 | 17 | ||
| 18 | pub fn writeFormattedName(self: User, w: anytype) !void { | 18 | pub fn writeFormattedName(self: User, w: anytype) !void { |
| 19 | try w.print("<a href=\"tg://user?id={}\"><i>", .{self.id}); | 19 | try w.print("<a href=\"tg://user?id={}\"><i>", .{self.id}); |
| 20 | try textutils.escapeXml(w, self.first_name); | 20 | try utils.escapeXml(w, self.first_name); |
| 21 | if (self.last_name) |last_name| { | 21 | if (self.last_name) |last_name| { |
| 22 | try w.writeByte(' '); | 22 | try w.writeByte(' '); |
| 23 | try textutils.escapeXml(w, last_name); | 23 | try utils.escapeXml(w, last_name); |
| 24 | } | 24 | } |
| 25 | try w.writeAll("</i>"); | 25 | try w.writeAll("</i>"); |
| 26 | 26 | ||
| 27 | if (self.username) |username| { | 27 | if (self.username) |username| { |
| 28 | try w.writeAll(" @"); | 28 | try w.writeAll(" @"); |
| 29 | try textutils.escapeXml(w, username); | 29 | try utils.escapeXml(w, username); |
| 30 | } | 30 | } |
| 31 | try w.print("</a> [<code>{}</code>]", .{self.id}); | 31 | try w.print("</a> [<code>{}</code>]", .{self.id}); |
| 32 | } | 32 | } |
diff --git a/src/textutils.zig b/src/utils.zig index 41dd5f5..c6e8508 100644 --- a/src/textutils.zig +++ b/src/utils.zig | |||
| @@ -14,3 +14,11 @@ pub fn escapeXml(writer: anytype, text: []const u8) !void { | |||
| 14 | }; | 14 | }; |
| 15 | } | 15 | } |
| 16 | } | 16 | } |
| 17 | |||
| 18 | pub inline fn isNull(value: anytype) bool { | ||
| 19 | return switch (@typeInfo(@TypeOf(value))) { | ||
| 20 | .Null => true, | ||
| 21 | .Optional => value == null, | ||
| 22 | else => false, | ||
| 23 | }; | ||
| 24 | } | ||