diff --git a/docker-compose.yml b/docker-compose.yml index 6484fc4..8a754bc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,6 +42,7 @@ services: # Discord Bot Configuration - DISCORD_WEBHOOK_URL=${DISCORD_WEBHOOK_URL} + - DISCORD_THREAD_ID=${DISCORD_THREAD_ID:-} - MESH_REGION=${MESH_REGION:-seattle} - POLL_INTERVAL=${POLL_INTERVAL:-1000} - MAX_ROWS_PER_POLL=${MAX_ROWS_PER_POLL:-50} diff --git a/scripts/discord-bot.ts b/scripts/discord-bot.ts index 74291b9..a783819 100644 --- a/scripts/discord-bot.ts +++ b/scripts/discord-bot.ts @@ -16,6 +16,7 @@ import { DiscordWebhookClient, formatMeshcoreMessageForDiscord } from './lib/dis interface BotConfig { webhookUrl: string; + threadId?: string; region: string; pollInterval: number; maxRowsPerPoll: number; @@ -30,7 +31,7 @@ class MeshCoreDiscordBot { constructor(config: BotConfig) { this.config = config; - this.discordClient = new DiscordWebhookClient(config.webhookUrl); + this.discordClient = new DiscordWebhookClient(config.webhookUrl, config.threadId); } async start() { @@ -44,6 +45,9 @@ class MeshCoreDiscordBot { console.log(`Region: ${this.config.region}`); console.log(`Poll interval: ${this.config.pollInterval}ms`); console.log(`Max rows per poll: ${this.config.maxRowsPerPoll}`); + if (this.config.threadId) { + console.log(`Thread ID: ${this.config.threadId}`); + } // Create streaming configuration const streamerConfig = createChatMessagesStreamerConfig(undefined, this.config.region); @@ -130,6 +134,7 @@ class MeshCoreDiscordBot { async function main() { // Get configuration from environment variables const webhookUrl = process.env.DISCORD_WEBHOOK_URL; + const threadId = process.env.DISCORD_THREAD_ID; const region = process.env.MESH_REGION || 'seattle'; const pollInterval = parseInt(process.env.POLL_INTERVAL || '1000', 10); const maxRowsPerPoll = parseInt(process.env.MAX_ROWS_PER_POLL || '50', 10); @@ -156,6 +161,9 @@ async function main() { console.log('Configuration:'); console.log(` Webhook URL: ${webhookUrl.substring(0, 50)}...`); + if (threadId) { + console.log(` Thread ID: ${threadId}`); + } console.log(` Region: ${region}`); console.log(` Poll interval: ${pollInterval}ms`); console.log(` Max rows per poll: ${maxRowsPerPoll}`); @@ -164,6 +172,7 @@ async function main() { // Create and start the bot const bot = new MeshCoreDiscordBot({ webhookUrl, + threadId, region, pollInterval, maxRowsPerPoll, diff --git a/scripts/lib/discord.ts b/scripts/lib/discord.ts index 3e678a5..e4191ea 100644 --- a/scripts/lib/discord.ts +++ b/scripts/lib/discord.ts @@ -40,10 +40,12 @@ export interface DiscordWebhookResponse { export class DiscordWebhookClient { private webhookUrl: string; + private threadId?: string; private messageIdMap: Map = new Map(); // message_id -> discord_message_id - constructor(webhookUrl: string) { + constructor(webhookUrl: string, threadId?: string) { this.webhookUrl = webhookUrl; + this.threadId = threadId; } /** @@ -54,6 +56,11 @@ export class DiscordWebhookClient { const url = new URL(this.webhookUrl); url.searchParams.set('wait', 'true'); + // Add thread_id if configured + if (this.threadId) { + url.searchParams.set('thread_id', this.threadId); + } + const response = await fetch(url.toString(), { method: 'POST', headers: { @@ -74,7 +81,14 @@ export class DiscordWebhookClient { */ async updateMessage(discordMessageId: string, message: DiscordWebhookMessage): Promise { // Use the correct URL format for updating messages - const updateUrl = `${this.webhookUrl}/messages/${discordMessageId}`; + let updateUrl = `${this.webhookUrl}/messages/${discordMessageId}`; + + // Add thread_id parameter if configured + if (this.threadId) { + const url = new URL(updateUrl); + url.searchParams.set('thread_id', this.threadId); + updateUrl = url.toString(); + } const response = await fetch(updateUrl, { method: 'PATCH',