Closed spymaster20 closed 6 months ago
I haven't tested for long period of time but been for more than a day, here is my suggested code for chat.ts(Might need improvement since I did it very quickly because of curiosity):
import { GroupChat, Message } from "whatsapp-web.js";
import { stripIndents } from "common-tags";
import { whatsapp } from "../../clients/whatsapp";
export async function getChatContext(message: Message) {
let chat = await message.getChat();
let chatContext = "";
let publicUserName: string;
if (chat.isGroup) {
const groupChat = chat as GroupChat;
const contact = await whatsapp.getContactById(message.author as string);
publicUserName = contact.pushname;
// start of added code for group Participants
try {
const participantNames = await Promise.all(
groupChat.participants.map(async(participant) => {
//For other uncaught errors
if (participant.id && participant.id._serialized) {
const participantId = participant.id._serialized;
const participantContact = await whatsapp.getContactById(participantId);
return participantContact.pushname;
} else {
// Handle the case where participant.id or participant.id._serialized is undefined or not valid
//return "Unknown Participant";
}
}));
chatContext = stripIndents `- You are in a group chat
- There are ${groupChat.participants.length} participants in the group
- The group's name is '${groupChat.name}'
- '${publicUserName}' is who sent this message
- Participants in group: '${participantNames.join(', ')}'
`;
} catch (error) {
console.log("Error retrieving participant code: ", error);
//return possible errors;
}
// end of added code for group Participants
} else {
publicUserName = (await message.getContact()).pushname;
chatContext = stripIndents `- You are in a private chat
- The user's name is '${publicUserName}'
`;
}
return chatContext;
}
I will try to add it together with OpenRouter
Done, should be added tonight along with others things
I ended up with:
import { GroupChat, Message } from "whatsapp-web.js";
import { stripIndents } from "common-tags";
import { whatsapp } from "../../clients/whatsapp";
export async function getChatContext(message: Message) {
let chat = await message.getChat();
let chatContext = "";
let publicUserName: string;
if (chat.isGroup) {
const groupChat = chat as GroupChat;
const contact = await whatsapp.getContactById(message.author as string);
publicUserName = contact.pushname;
const groupContacts = await Promise.all(
groupChat.participants.map((participant) =>
whatsapp.getContactById(participant.id._serialized)
)
);
const groupContactNames = groupContacts
.map((contact) => contact.pushname)
.join(", ");
console.log("groupContactNames:", groupContactNames);
chatContext = stripIndents`- You are in a group chat
- There are ${groupChat.participants.length} participants in the group
- The group's name is '${groupChat.name}'
- The group's participants are: ${groupContactNames}
- '${publicUserName}' is who sent this message
`;
} else {
publicUserName = (await message.getContact()).pushname;
chatContext = stripIndents`- You are in a private chat
- The user's name is '${publicUserName}'
`;
}
return chatContext;
}
Added in #248
The bot cannot tell who is in the chat, which makes it address inaccurately time to time. There already some context added to the chat which makes me wonder why not add the participants names? Can this be added as feature? or something like this in src\handlers\context\chat.ts
const participantNames = await Promise.all( groupChat.participants.map(async(participant) => { //For other uncaught errors