discord bot: support threads

This commit is contained in:
ajvpot
2025-09-15 04:14:58 +02:00
parent 9f1056095b
commit 0aac045c1b
3 changed files with 27 additions and 3 deletions

View File

@@ -42,6 +42,7 @@ services:
# Discord Bot Configuration # Discord Bot Configuration
- DISCORD_WEBHOOK_URL=${DISCORD_WEBHOOK_URL} - DISCORD_WEBHOOK_URL=${DISCORD_WEBHOOK_URL}
- DISCORD_THREAD_ID=${DISCORD_THREAD_ID:-}
- MESH_REGION=${MESH_REGION:-seattle} - MESH_REGION=${MESH_REGION:-seattle}
- POLL_INTERVAL=${POLL_INTERVAL:-1000} - POLL_INTERVAL=${POLL_INTERVAL:-1000}
- MAX_ROWS_PER_POLL=${MAX_ROWS_PER_POLL:-50} - MAX_ROWS_PER_POLL=${MAX_ROWS_PER_POLL:-50}

View File

@@ -16,6 +16,7 @@ import { DiscordWebhookClient, formatMeshcoreMessageForDiscord } from './lib/dis
interface BotConfig { interface BotConfig {
webhookUrl: string; webhookUrl: string;
threadId?: string;
region: string; region: string;
pollInterval: number; pollInterval: number;
maxRowsPerPoll: number; maxRowsPerPoll: number;
@@ -30,7 +31,7 @@ class MeshCoreDiscordBot {
constructor(config: BotConfig) { constructor(config: BotConfig) {
this.config = config; this.config = config;
this.discordClient = new DiscordWebhookClient(config.webhookUrl); this.discordClient = new DiscordWebhookClient(config.webhookUrl, config.threadId);
} }
async start() { async start() {
@@ -44,6 +45,9 @@ class MeshCoreDiscordBot {
console.log(`Region: ${this.config.region}`); console.log(`Region: ${this.config.region}`);
console.log(`Poll interval: ${this.config.pollInterval}ms`); console.log(`Poll interval: ${this.config.pollInterval}ms`);
console.log(`Max rows per poll: ${this.config.maxRowsPerPoll}`); console.log(`Max rows per poll: ${this.config.maxRowsPerPoll}`);
if (this.config.threadId) {
console.log(`Thread ID: ${this.config.threadId}`);
}
// Create streaming configuration // Create streaming configuration
const streamerConfig = createChatMessagesStreamerConfig(undefined, this.config.region); const streamerConfig = createChatMessagesStreamerConfig(undefined, this.config.region);
@@ -130,6 +134,7 @@ class MeshCoreDiscordBot {
async function main() { async function main() {
// Get configuration from environment variables // Get configuration from environment variables
const webhookUrl = process.env.DISCORD_WEBHOOK_URL; const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
const threadId = process.env.DISCORD_THREAD_ID;
const region = process.env.MESH_REGION || 'seattle'; const region = process.env.MESH_REGION || 'seattle';
const pollInterval = parseInt(process.env.POLL_INTERVAL || '1000', 10); const pollInterval = parseInt(process.env.POLL_INTERVAL || '1000', 10);
const maxRowsPerPoll = parseInt(process.env.MAX_ROWS_PER_POLL || '50', 10); const maxRowsPerPoll = parseInt(process.env.MAX_ROWS_PER_POLL || '50', 10);
@@ -156,6 +161,9 @@ async function main() {
console.log('Configuration:'); console.log('Configuration:');
console.log(` Webhook URL: ${webhookUrl.substring(0, 50)}...`); console.log(` Webhook URL: ${webhookUrl.substring(0, 50)}...`);
if (threadId) {
console.log(` Thread ID: ${threadId}`);
}
console.log(` Region: ${region}`); console.log(` Region: ${region}`);
console.log(` Poll interval: ${pollInterval}ms`); console.log(` Poll interval: ${pollInterval}ms`);
console.log(` Max rows per poll: ${maxRowsPerPoll}`); console.log(` Max rows per poll: ${maxRowsPerPoll}`);
@@ -164,6 +172,7 @@ async function main() {
// Create and start the bot // Create and start the bot
const bot = new MeshCoreDiscordBot({ const bot = new MeshCoreDiscordBot({
webhookUrl, webhookUrl,
threadId,
region, region,
pollInterval, pollInterval,
maxRowsPerPoll, maxRowsPerPoll,

View File

@@ -40,10 +40,12 @@ export interface DiscordWebhookResponse {
export class DiscordWebhookClient { export class DiscordWebhookClient {
private webhookUrl: string; private webhookUrl: string;
private threadId?: string;
private messageIdMap: Map<string, string> = new Map(); // message_id -> discord_message_id private messageIdMap: Map<string, string> = new Map(); // message_id -> discord_message_id
constructor(webhookUrl: string) { constructor(webhookUrl: string, threadId?: string) {
this.webhookUrl = webhookUrl; this.webhookUrl = webhookUrl;
this.threadId = threadId;
} }
/** /**
@@ -54,6 +56,11 @@ export class DiscordWebhookClient {
const url = new URL(this.webhookUrl); const url = new URL(this.webhookUrl);
url.searchParams.set('wait', 'true'); 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(), { const response = await fetch(url.toString(), {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -74,7 +81,14 @@ export class DiscordWebhookClient {
*/ */
async updateMessage(discordMessageId: string, message: DiscordWebhookMessage): Promise<DiscordWebhookResponse> { async updateMessage(discordMessageId: string, message: DiscordWebhookMessage): Promise<DiscordWebhookResponse> {
// Use the correct URL format for updating messages // 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, { const response = await fetch(updateUrl, {
method: 'PATCH', method: 'PATCH',