From 1d1df7b540721d58db174764a0f720e3c638a67b Mon Sep 17 00:00:00 2001
From: Uko Kokņevičs
Date: Tue, 22 Jul 2025 08:23:22 +0300
Subject: Whitelist allowed inline bots
Also update to Zig 0.14.1
Also don't send the animations rn
---
src/main.zig | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 106 insertions(+), 14 deletions(-)
(limited to 'src/main.zig')
diff --git a/src/main.zig b/src/main.zig
index b164284..9e1de47 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -46,6 +46,27 @@ fn loadConfig(allocator: Allocator, filename: []const u8) !std.json.Parsed(Confi
);
}
+fn reportError(bot: *Bot, msg: types.Message, err: anyerror) !void {
+ std.log.err("While handling {}: {}", .{ msg, err });
+ const msgStr = try std.json.stringifyAlloc(bot.allocator, msg, .{
+ .whitespace = .indent_2,
+ .emit_null_optional_fields = false,
+ });
+ defer bot.allocator.free(msgStr);
+
+ const devMsg = try std.fmt.allocPrint(bot.allocator, "{} while handling\n
{s}", .{ err, msgStr });
+ defer bot.allocator.free(devMsg);
+
+ bot.sendMessage_(.{
+ .chat_id = bot.config.dev_group,
+ .text = devMsg,
+ .parse_mode = .html,
+ }) catch |err2| {
+ std.log.err("While trying to report the error: {}", .{err2});
+ return err2;
+ };
+}
+
fn wrappedMain(bot: *Bot) !void {
try bot.sendMessage_(.{
.chat_id = bot.config.dev_group,
@@ -63,8 +84,9 @@ fn wrappedMain(bot: *Bot) !void {
defer gup.offset = update.update_id + 1;
if (update.message) |message| {
- // TODO: Catch minor errors, report them
- try onMessage(bot, message);
+ onMessage(bot, message) catch |err| {
+ try reportError(bot, message, err);
+ };
}
}
}
@@ -80,7 +102,63 @@ fn wrappedMain(bot: *Bot) !void {
});
}
+const allowed_inline_bots = [_]i64{
+ 90832338, // @vid
+ 109158646, // @bing
+ 114528005, // @pic
+ 140267078, // @gif
+ 154595593, // @wiki
+ 184730458, // @UnitConversionBot
+ 296635833, // @lastfmrobot
+ 595898211, // @DeezerMusicBot
+ 870410041, // @HowGayBot
+ 7904498194, // @tanstiktokbot
+};
+
+comptime {
+ std.testing.expect(std.sort.isSorted(
+ i64,
+ &allowed_inline_bots,
+ {},
+ std.sort.asc(i64),
+ )) catch unreachable;
+}
+
+fn orderI64(a: i64, b: i64) std.math.Order {
+ return std.math.order(a, b);
+}
+
+fn isAllowedInlineBot(id: i64) bool {
+ return std.sort.binarySearch(
+ i64,
+ &allowed_inline_bots,
+ id,
+ orderI64,
+ ) != null;
+}
+
fn onMessage(bot: *Bot, msg: types.Message) !void {
+ if (msg.via_bot) |via| {
+ if (!isAllowedInlineBot(via.id)) {
+ std.log.info("Deleting an unallowed inline bot message from {?s} {}", .{ via.username, via.id });
+ try bot.deleteMessage(.{
+ .chat_id = msg.chat.id,
+ .message_id = msg.message_id,
+ });
+
+ const text = try std.fmt.allocPrint(bot.allocator, "Deleted a message sent by inline bot @{?s} {}", .{ via.username, via.id });
+ defer bot.allocator.free(text);
+
+ try bot.sendMessage_(.{
+ .chat_id = bot.config.dev_group,
+ .text = text,
+ .parse_mode = .html,
+ });
+
+ return;
+ }
+ }
+
if (msg.text) |text| {
try onTextMessage(bot, msg, text);
}
@@ -94,12 +172,14 @@ fn onMessage(bot: *Bot, msg: types.Message) !void {
fn onNewMember(bot: *Bot, msg: types.Message, new_member: types.User) !void {
if (new_member.id == try bot.getId()) {
+ return;
+ // TODO:
// Bot is added to a new group
- return bot.sendAnimation_(.{
- .chat_id = msg.chat.id,
- // TODO: lol
- .animation = "CgACAgQAAx0CVcPEEgACDC5mo7YHMgOE2n3qo3e9UOyd4N-uxQACNAMAAlbuDFMRWj9LxNLBkDUE",
- });
+ // return bot.sendAnimation_(.{
+ // .chat_id = msg.chat.id,
+ // // TODO: lol
+ // .animation = "CgACAgQAAx0CVcPEEgACDC5mo7YHMgOE2n3qo3e9UOyd4N-uxQACNAMAAlbuDFMRWj9LxNLBkDUE",
+ // });
}
var sb = ArrayList(u8).init(bot.allocator);
@@ -111,13 +191,25 @@ fn onNewMember(bot: *Bot, msg: types.Message, new_member: types.User) !void {
try new_member.writeFormattedName(w);
try w.writeAll("! Be on your bestest behaviour now!!");
- try bot.sendAnimation_(.{
+ // TODO:
+ // try bot.sendAnimation_(.{
+ // .chat_id = msg.chat.id,
+ // // TODO: lol
+ // .animation = "CgACAgQAAx0CVcPEEgACC9Vmo6_zCxMp3ZNXSMM1nI6nMkIhgwACNwMAAtDmDFMop6BHmV7lUTUE",
+ // .caption = sb.items,
+ // .parse_mode = .html,
+ // .show_caption_above_media = true,
+ // .reply_parameters = .{
+ // .allow_sending_without_reply = true,
+ // .message_id = msg.message_id,
+ // .chat_id = msg.chat.id,
+ // },
+ // });
+
+ try bot.sendMessage_(.{
.chat_id = msg.chat.id,
- // TODO: lol
- .animation = "CgACAgQAAx0CVcPEEgACC9Vmo6_zCxMp3ZNXSMM1nI6nMkIhgwACNwMAAtDmDFMop6BHmV7lUTUE",
- .caption = sb.items,
+ .text = sb.items,
.parse_mode = .html,
- .show_caption_above_media = true,
.reply_parameters = .{
.allow_sending_without_reply = true,
.message_id = msg.message_id,
@@ -201,8 +293,8 @@ fn onTextMessage(bot: *Bot, msg: types.Message, text: []const u8) !void {
} else if (std.ascii.startsWithIgnoreCase(text, "big ")) {
const the_text = text[4..];
if (!try utils.isTgWhitespaceStr(the_text)) {
- const cd = try utils.getCD();
- const uppercased = try cd.toUpperStr(bot.allocator, the_text);
+ const lc = try utils.getLetterCasing();
+ const uppercased = try lc.toUpperStr(bot.allocator, the_text);
defer bot.allocator.free(uppercased);
var output = ArrayList(u8).init(bot.allocator);
--
cgit v1.2.3