From e5185f65051f881bf61e88542a1acd4957f8383b Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sun, 3 Aug 2025 12:54:12 +0300 Subject: Move bot configuration to SQL land --- src/main.zig | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 9 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 942fd90..5931250 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,3 +1,4 @@ +const inline_bots = @import("inline_bots.zig"); const std = @import("std"); const types = @import("types.zig"); const utils = @import("utils.zig"); @@ -6,10 +7,9 @@ const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; const Bot = @import("Bot.zig"); const Config = @import("Config.zig"); +const DB = @import("DB.zig"); const GPA = std.heap.GeneralPurposeAllocator(.{}); -const onInlineBot = @import("inline_bots.zig").onInlineBot; - pub fn main() !void { defer std.log.info("We're done", .{}); @@ -22,7 +22,11 @@ pub fn main() !void { defer config.deinit(); try config.merge("config.json"); - var bot = try Bot.init(allocator, config.config); + var db = try DB.init(config.config.db_path); + defer db.deinit(); + try db.upgrade(); + + var bot = try Bot.init(allocator, config.config, &db); defer bot.deinit(); // TODO: Catch fatal errors, report them @@ -48,15 +52,15 @@ 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, .{ +fn reportError(bot: *Bot, evt: anytype, err: anyerror) !void { + std.log.err("While handling {}: {}", .{ evt, err }); + const evtStr = try std.json.stringifyAlloc(bot.allocator, evt, .{ .whitespace = .indent_2, .emit_null_optional_fields = false, }); - defer bot.allocator.free(msgStr); + defer bot.allocator.free(evtStr); - const devMsg = try std.fmt.allocPrint(bot.allocator, "{} while handling\n
{s}
", .{ err, msgStr }); + const devMsg = try std.fmt.allocPrint(bot.allocator, "{} while handling\n
{s}
", .{ err, evtStr }); defer bot.allocator.free(devMsg); bot.sendMessage_(.{ @@ -90,6 +94,12 @@ fn wrappedMain(bot: *Bot) !void { try reportError(bot, message, err); }; } + + if (update.callback_query) |cb| { + onCallbackQuery(bot, cb) catch |err| { + try reportError(bot, cb, err); + }; + } } } @@ -104,9 +114,55 @@ fn wrappedMain(bot: *Bot) !void { }); } +fn onCallbackQuery(bot: *Bot, cb: types.CallbackQuery) !void { + if (cb.data) |cb_data| blk: { + if (std.mem.startsWith(u8, cb_data, "bbl:")) { + if (cb.from.id != bot.config.owner) { + break :blk; + } + + const inline_bot_id = try std.fmt.parseInt(i64, cb_data[4..], 10); + try inline_bots.blacklistBot(bot, inline_bot_id); + if (cb.message) |msg| { + try bot.deleteMessage(.{ + .chat_id = msg.chat.id, + .message_id = msg.message_id, + }); + } + } else if (std.mem.startsWith(u8, cb_data, "bwl:")) { + if (cb.from.id != bot.config.owner) { + break :blk; + } + + const inline_bot_id = try std.fmt.parseInt(i64, cb_data[4..], 10); + try inline_bots.whitelistBot(bot, inline_bot_id); + if (cb.message) |msg| { + try bot.deleteMessage(.{ + .chat_id = msg.chat.id, + .message_id = msg.message_id, + }); + } + } else { + break :blk; + } + + return bot.answerCallbackQuery(.{ + .callback_query_id = cb.id, + .text = "OK", + }); + } + + std.log.info("Unrecognised callback query data: {?s}", .{ cb.data }); + return bot.answerCallbackQuery(.{ + .callback_query_id = cb.id, + .text = "Unallowed callback query, don't press the button again", + .show_alert = true, + }); +} + fn onMessage(bot: *Bot, msg: types.Message) !void { if (msg.via_bot) |via| { - if (!try onInlineBot(bot, msg, via)) { + if (!try inline_bots.onInlineBot(bot, msg, via)) { return; } } -- cgit v1.2.3