|
| 1 | +// bot_loop.zig — Main poll → parse → dispatch → repeat loop |
| 2 | +const std = @import("std"); |
| 3 | +const telegram_api = @import("telegram_api.zig"); |
| 4 | +const json_utils = @import("json_utils.zig"); |
| 5 | +const command_parser = @import("command_parser.zig"); |
| 6 | +const handlers = @import("handlers.zig"); |
| 7 | + |
| 8 | +const BotConfig = telegram_api.BotConfig; |
| 9 | + |
| 10 | +/// Run the bot loop: poll Telegram, parse commands, dispatch handlers. |
| 11 | +/// Never returns (infinite loop). |
| 12 | +pub fn run(allocator: std.mem.Allocator, config: BotConfig) void { |
| 13 | + var last_update_id: i64 = 0; |
| 14 | + |
| 15 | + // Announce startup |
| 16 | + telegram_api.sendMessage(allocator, config.bot_token, config.chat_id, "\xf0\x9f\xa4\x96 TRI BOT online! Send /help for commands."); |
| 17 | + |
| 18 | + std.debug.print("[tri-bot] Started. Polling Telegram...\n", .{}); |
| 19 | + |
| 20 | + while (true) { |
| 21 | + const body = telegram_api.getUpdates(allocator, config.bot_token, last_update_id + 1) orelse { |
| 22 | + // Network error — wait and retry |
| 23 | + std.Thread.sleep(5 * std.time.ns_per_s); |
| 24 | + continue; |
| 25 | + }; |
| 26 | + defer allocator.free(body); |
| 27 | + |
| 28 | + // Process each update |
| 29 | + const Context = struct { |
| 30 | + allocator: std.mem.Allocator, |
| 31 | + config: BotConfig, |
| 32 | + max_id: i64, |
| 33 | + }; |
| 34 | + var ctx = Context{ |
| 35 | + .allocator = allocator, |
| 36 | + .config = config, |
| 37 | + .max_id = last_update_id, |
| 38 | + }; |
| 39 | + |
| 40 | + // We can't use closures in Zig, so use a global-style dispatch. |
| 41 | + // Instead, manually iterate updates with a simple loop. |
| 42 | + processUpdates(allocator, config, body, &ctx.max_id); |
| 43 | + last_update_id = ctx.max_id; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fn processUpdates(allocator: std.mem.Allocator, config: BotConfig, body: []const u8, max_id: *i64) void { |
| 48 | + // Find each "update_id": block manually |
| 49 | + var pos: usize = 0; |
| 50 | + while (pos < body.len) { |
| 51 | + const needle = "\"update_id\":"; |
| 52 | + const idx = std.mem.indexOfPos(u8, body, pos, needle) orelse break; |
| 53 | + |
| 54 | + // Determine block boundary (next update_id or end) |
| 55 | + const next_idx = std.mem.indexOfPos(u8, body, idx + needle.len + 1, needle) orelse body.len; |
| 56 | + const block = body[idx..next_idx]; |
| 57 | + |
| 58 | + // Extract update_id |
| 59 | + const uid = json_utils.extractInt(block, "update_id") orelse { |
| 60 | + pos = idx + needle.len; |
| 61 | + continue; |
| 62 | + }; |
| 63 | + |
| 64 | + // Update max |
| 65 | + if (uid > max_id.*) { |
| 66 | + max_id.* = uid; |
| 67 | + } |
| 68 | + |
| 69 | + // Extract chat_id |
| 70 | + const chat_id_val = blk: { |
| 71 | + const chat_needle = "\"chat\":{\"id\":"; |
| 72 | + const ci = std.mem.indexOf(u8, block, chat_needle) orelse break :blk @as(i64, 0); |
| 73 | + const cs = ci + chat_needle.len; |
| 74 | + var ce = cs; |
| 75 | + while (ce < block.len and ((block[ce] >= '0' and block[ce] <= '9') or block[ce] == '-')) : (ce += 1) {} |
| 76 | + break :blk std.fmt.parseInt(i64, block[cs..ce], 10) catch 0; |
| 77 | + }; |
| 78 | + |
| 79 | + // Auth check: only respond to configured chat_id |
| 80 | + const expected_chat_id = std.fmt.parseInt(i64, config.chat_id, 10) catch 0; |
| 81 | + if (chat_id_val != expected_chat_id) { |
| 82 | + std.debug.print("[tri-bot] Ignoring update from chat {d} (expected {d})\n", .{ chat_id_val, expected_chat_id }); |
| 83 | + pos = next_idx; |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // Extract text |
| 88 | + const text = json_utils.extractString(block, "text") orelse { |
| 89 | + pos = next_idx; |
| 90 | + continue; |
| 91 | + }; |
| 92 | + |
| 93 | + std.debug.print("[tri-bot] Update {d}: \"{s}\"\n", .{ uid, text }); |
| 94 | + |
| 95 | + // Parse command |
| 96 | + const cmd = command_parser.parse(text); |
| 97 | + |
| 98 | + // Dispatch |
| 99 | + dispatch(allocator, config, cmd); |
| 100 | + |
| 101 | + pos = next_idx; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn dispatch(allocator: std.mem.Allocator, config: BotConfig, cmd: command_parser.Command) void { |
| 106 | + if (std.mem.eql(u8, cmd.name, "help")) { |
| 107 | + handlers.handleHelp(allocator, config); |
| 108 | + } else if (std.mem.eql(u8, cmd.name, "ask")) { |
| 109 | + handlers.handleAsk(allocator, config, cmd.args); |
| 110 | + } else if (std.mem.eql(u8, cmd.name, "continue")) { |
| 111 | + handlers.handleContinue(allocator, config, cmd.args); |
| 112 | + } else if (std.mem.eql(u8, cmd.name, "status")) { |
| 113 | + handlers.handleStatus(allocator, config); |
| 114 | + } else if (std.mem.eql(u8, cmd.name, "stop")) { |
| 115 | + telegram_api.sendMessage(allocator, config.bot_token, config.chat_id, "\xe2\x9b\x94 /stop not yet implemented (Phase 2)"); |
| 116 | + } else if (cmd.name.len > 0) { |
| 117 | + // Unknown command |
| 118 | + var buf: [256]u8 = undefined; |
| 119 | + telegram_api.sendFmt(allocator, config.bot_token, config.chat_id, &buf, "\xe2\x9d\x93 Unknown command: /{s}. Try /help", .{cmd.name}); |
| 120 | + } |
| 121 | + // Plain text (no command) — ignore silently |
| 122 | +} |
0 commit comments