summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Bot.zig13
-rw-r--r--src/main.zig17
-rw-r--r--src/types/User.zig8
-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 @@
1const types = @import("types.zig");
2const std = @import("std"); 1const std = @import("std");
2const types = @import("types.zig");
3const utils = @import("utils.zig");
3 4
4const Allocator = std.mem.Allocator; 5const Allocator = std.mem.Allocator;
5const ArrayList = std.ArrayList; 6const ArrayList = std.ArrayList;
@@ -176,14 +177,6 @@ fn call(
176 } 177 }
177} 178}
178 179
179inline fn isNull(value: anytype) bool {
180 return switch (@typeInfo(@TypeOf(value))) {
181 .Null => true,
182 .Optional => value == null,
183 else => false,
184 };
185}
186
187fn intoQueryString(allocator: Allocator, data: anytype) !?[]u8 { 180fn 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 @@
1const textutils = @import("../textutils.zig"); 1const utils = @import("../utils.zig");
2 2
3const User = @This(); 3const User = @This();
4 4
@@ -17,16 +17,16 @@ can_connect_to_business: bool = false,
17 17
18pub fn writeFormattedName(self: User, w: anytype) !void { 18pub 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
18pub inline fn isNull(value: anytype) bool {
19 return switch (@typeInfo(@TypeOf(value))) {
20 .Null => true,
21 .Optional => value == null,
22 else => false,
23 };
24}