summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2025-08-03 12:54:12 +0300
committerGravatar Uko Kokņevičs2025-08-03 12:54:12 +0300
commite5185f65051f881bf61e88542a1acd4957f8383b (patch)
treea030a8b32cd13ed6a7d9ed736f8fc626501c2749 /src/main.zig
parentMoved inline bot handling to a new file (diff)
downloadukkobot-e5185f65051f881bf61e88542a1acd4957f8383b.tar.gz
ukkobot-e5185f65051f881bf61e88542a1acd4957f8383b.tar.xz
ukkobot-e5185f65051f881bf61e88542a1acd4957f8383b.zip
Move bot configuration to SQL land
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig74
1 files changed, 65 insertions, 9 deletions
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 @@
1const inline_bots = @import("inline_bots.zig");
1const std = @import("std"); 2const std = @import("std");
2const types = @import("types.zig"); 3const types = @import("types.zig");
3const utils = @import("utils.zig"); 4const utils = @import("utils.zig");
@@ -6,10 +7,9 @@ const Allocator = std.mem.Allocator;
6const ArrayList = std.ArrayList; 7const ArrayList = std.ArrayList;
7const Bot = @import("Bot.zig"); 8const Bot = @import("Bot.zig");
8const Config = @import("Config.zig"); 9const Config = @import("Config.zig");
10const DB = @import("DB.zig");
9const GPA = std.heap.GeneralPurposeAllocator(.{}); 11const GPA = std.heap.GeneralPurposeAllocator(.{});
10 12
11const onInlineBot = @import("inline_bots.zig").onInlineBot;
12
13pub fn main() !void { 13pub fn main() !void {
14 defer std.log.info("We're done", .{}); 14 defer std.log.info("We're done", .{});
15 15
@@ -22,7 +22,11 @@ pub fn main() !void {
22 defer config.deinit(); 22 defer config.deinit();
23 try config.merge("config.json"); 23 try config.merge("config.json");
24 24
25 var bot = try Bot.init(allocator, config.config); 25 var db = try DB.init(config.config.db_path);
26 defer db.deinit();
27 try db.upgrade();
28
29 var bot = try Bot.init(allocator, config.config, &db);
26 defer bot.deinit(); 30 defer bot.deinit();
27 31
28 // TODO: Catch fatal errors, report them 32 // TODO: Catch fatal errors, report them
@@ -48,15 +52,15 @@ fn loadConfig(allocator: Allocator, filename: []const u8) !std.json.Parsed(Confi
48 ); 52 );
49} 53}
50 54
51fn reportError(bot: *Bot, msg: types.Message, err: anyerror) !void { 55fn reportError(bot: *Bot, evt: anytype, err: anyerror) !void {
52 std.log.err("While handling {}: {}", .{ msg, err }); 56 std.log.err("While handling {}: {}", .{ evt, err });
53 const msgStr = try std.json.stringifyAlloc(bot.allocator, msg, .{ 57 const evtStr = try std.json.stringifyAlloc(bot.allocator, evt, .{
54 .whitespace = .indent_2, 58 .whitespace = .indent_2,
55 .emit_null_optional_fields = false, 59 .emit_null_optional_fields = false,
56 }); 60 });
57 defer bot.allocator.free(msgStr); 61 defer bot.allocator.free(evtStr);
58 62
59 const devMsg = try std.fmt.allocPrint(bot.allocator, "<code>{}</code> while handling\n<pre>{s}</pre>", .{ err, msgStr }); 63 const devMsg = try std.fmt.allocPrint(bot.allocator, "<code>{}</code> while handling\n<pre>{s}</pre>", .{ err, evtStr });
60 defer bot.allocator.free(devMsg); 64 defer bot.allocator.free(devMsg);
61 65
62 bot.sendMessage_(.{ 66 bot.sendMessage_(.{
@@ -90,6 +94,12 @@ fn wrappedMain(bot: *Bot) !void {
90 try reportError(bot, message, err); 94 try reportError(bot, message, err);
91 }; 95 };
92 } 96 }
97
98 if (update.callback_query) |cb| {
99 onCallbackQuery(bot, cb) catch |err| {
100 try reportError(bot, cb, err);
101 };
102 }
93 } 103 }
94 } 104 }
95 105
@@ -104,9 +114,55 @@ fn wrappedMain(bot: *Bot) !void {
104 }); 114 });
105} 115}
106 116
117fn onCallbackQuery(bot: *Bot, cb: types.CallbackQuery) !void {
118 if (cb.data) |cb_data| blk: {
119 if (std.mem.startsWith(u8, cb_data, "bbl:")) {
120 if (cb.from.id != bot.config.owner) {
121 break :blk;
122 }
123
124 const inline_bot_id = try std.fmt.parseInt(i64, cb_data[4..], 10);
125 try inline_bots.blacklistBot(bot, inline_bot_id);
126 if (cb.message) |msg| {
127 try bot.deleteMessage(.{
128 .chat_id = msg.chat.id,
129 .message_id = msg.message_id,
130 });
131 }
132 } else if (std.mem.startsWith(u8, cb_data, "bwl:")) {
133 if (cb.from.id != bot.config.owner) {
134 break :blk;
135 }
136
137 const inline_bot_id = try std.fmt.parseInt(i64, cb_data[4..], 10);
138 try inline_bots.whitelistBot(bot, inline_bot_id);
139 if (cb.message) |msg| {
140 try bot.deleteMessage(.{
141 .chat_id = msg.chat.id,
142 .message_id = msg.message_id,
143 });
144 }
145 } else {
146 break :blk;
147 }
148
149 return bot.answerCallbackQuery(.{
150 .callback_query_id = cb.id,
151 .text = "OK",
152 });
153 }
154
155 std.log.info("Unrecognised callback query data: {?s}", .{ cb.data });
156 return bot.answerCallbackQuery(.{
157 .callback_query_id = cb.id,
158 .text = "Unallowed callback query, don't press the button again",
159 .show_alert = true,
160 });
161}
162
107fn onMessage(bot: *Bot, msg: types.Message) !void { 163fn onMessage(bot: *Bot, msg: types.Message) !void {
108 if (msg.via_bot) |via| { 164 if (msg.via_bot) |via| {
109 if (!try onInlineBot(bot, msg, via)) { 165 if (!try inline_bots.onInlineBot(bot, msg, via)) {
110 return; 166 return;
111 } 167 }
112 } 168 }