diff --git a/src/bot/commands/task.ts b/src/bot/commands/task.ts index ee4d4dc..65dff65 100644 --- a/src/bot/commands/task.ts +++ b/src/bot/commands/task.ts @@ -2,6 +2,7 @@ import { sendMessage } from '../utils/messaging.ts'; import { createTask, assignTask, completeTask, getPendingTasks } from '../../services/taskService'; import { setRemindersEnabled } from '../../services/userService'; import { normalizeUserIdentifier, extractUserFromJid, formatUserMention } from '../../utils/userUtils'; +import { isUserInCommunity } from '../../utils/communityUtils'; function formatTaskList(tasks: any[]) { if (tasks.length === 0) { @@ -31,9 +32,11 @@ export function handleTaskCommand(body: string, sender: string, groupId: string, console.log('Checking community membership for:', assignedUser); console.log('Linked groups:', Array.from(linkedGroups)); - if (!linkedGroups.has(assignedUser)) { + // Check if user is in any of the linked groups + const userInCommunity = await isUserInCommunity(assignedUser, linkedGroups); + if (!userInCommunity) { sendMessage(sender, `El usuario ${formatUserMention(assignedUser)} no está en la comunidad`); - console.log('User not found in community:', assignedUser); + console.log('User not found in any community groups:', assignedUser); return; } } catch (error) { diff --git a/src/bot/utils/messaging.ts b/src/bot/utils/messaging.ts index a12408e..d3c48ed 100644 --- a/src/bot/utils/messaging.ts +++ b/src/bot/utils/messaging.ts @@ -22,7 +22,11 @@ export async function sendMessage(phone: string, message: string, mentions: stri // Add mentions if provided if (mentions.length > 0) { payload.contextInfo = { - mentionedJid: mentions + mentionedJid: mentions, + participant: mentions[0], // The user being mentioned + quotedMessage: { + conversation: message + } }; } diff --git a/src/utils/communityUtils.ts b/src/utils/communityUtils.ts new file mode 100644 index 0000000..4ddd263 --- /dev/null +++ b/src/utils/communityUtils.ts @@ -0,0 +1,37 @@ +import axios from 'axios'; +import dotenv from 'dotenv'; + +dotenv.config(); + +const API_URL = process.env.API_URL; +const INSTANCE_NAME = process.env.INSTANCE_NAME; +const API_KEY = process.env.API_KEY; + +export async function isUserInCommunity(userJid: string, groupJids: Set): Promise { + try { + // Check each group for the user + for (const groupJid of groupJids) { + const response = await axios.get( + `${API_URL}/group/getParticipants/${INSTANCE_NAME}`, + { + params: { + groupJid: groupJid + }, + headers: { + apikey: API_KEY + } + } + ); + + // Check if user is in this group's participants + const participants = response.data?.participants || []; + if (participants.includes(userJid)) { + return true; + } + } + return false; + } catch (error) { + console.error('Error checking community membership:', error); + return false; + } +}