You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import express from 'express';
|
|
import { handleTaskCommand } from '../bot/commands/task';
|
|
import { linkedGroups } from '../bot/bot'; // Ensure the import path is correct
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
// Webhook endpoint
|
|
app.post('/webhook', (req, res) => {
|
|
const event = req.body;
|
|
console.log('Received event:', JSON.stringify(event, null, 2)); // Log the full event
|
|
|
|
// Handle the event
|
|
if (event.event === 'messages.upsert') {
|
|
const messageData = event.data;
|
|
|
|
// Ignore messages sent by the bot itself
|
|
if (messageData.key.fromMe) {
|
|
console.log('Ignoring message sent by the bot itself');
|
|
return res.status(200).send('OK');
|
|
}
|
|
|
|
// Handle the message
|
|
handleTaskCommand(
|
|
messageData.message?.conversation || '', // Fixed typo: "conversation" instead of "conversation"
|
|
messageData.key.remoteJid,
|
|
messageData.key.remoteJid,
|
|
linkedGroups
|
|
);
|
|
} else if (event.event === 'group_create' && event.data.parentGroupId === process.env.COMMUNITY_ID) {
|
|
// A new group was created in the community
|
|
const groupId = event.data.groupId;
|
|
linkedGroups.add(groupId);
|
|
addBotToGroup(groupId);
|
|
}
|
|
|
|
res.status(200).send('OK');
|
|
});
|
|
|
|
export function startServer(port: number) {
|
|
app.listen(port, () => {
|
|
console.log(`Webhook server running on port ${port}`);
|
|
});
|
|
}
|