Sara
Sara•3w ago

Are you willing to share a snippet in a

Are you willing to share a snippet in a thread?
14 Replies
The shujaa
The shujaa•3w ago
give me a minute.
The shujaa
The shujaa•3w ago
am using this code snippet such that when a whatsapp message is received, we do some checks and then store the message, then, we call a function to process the request from whatsapp through
console.log("scheduling onInbound for message", insertedId)
await ctx.scheduler.runAfter(0, api.actions.assist.onInbound, {
instanceId: args.instanceId,
chatJid: messageData.chatJid,
});
console.log("scheduling onInbound for message", insertedId)
await ctx.scheduler.runAfter(0, api.actions.assist.onInbound, {
instanceId: args.instanceId,
chatJid: messageData.chatJid,
});
the function handles getting the message and processing the message through an llm, then sends the message back to the number, but am getting a response sometimes and not all times.
The shujaa
The shujaa•3w ago
initially I thought maybe the llm is not calling tools that are supposed to send the message but when I added console logs, I realized the function is not being called in the first place, and as I said it does this sometimes and sometimes it does not, under the same conditions, without changing any code, and am messaging from the same number.
The shujaa
The shujaa•3w ago
here is a snippet of the inbound function
Sara
SaraOP•3w ago
You definitely don't need try catch in convex functions But that's not the problem
The shujaa
The shujaa•3w ago
what could be the problem?
Sara
SaraOP•3w ago
I'm still reading the code that's too much to take in one go in the first message you have two if statements doing the same thing, if (needsCache) { and } else if (needsCache) { and the schedulers aren't getting called unless if media !messageData.fromMe It looks like that's what's causing your issue, and this is what you should look into, if its true, it works but if the second if statements doesn't go through, it would also not work you should do some debugging from the for loop. -> first if statment -> second if statment And a snippet is a minimal reproduction, not your whole code base 😂
The shujaa
The shujaa•3w ago
well these needsCache are scoped differently, I dont know If you read that correctly, the first one is if the message is from me, we and i have sent an image through whatsapp, we cache the media but we dont respond because I dont want it to get into the loop of answering every message that comes in including the one sent by the bot, but if the message is not from me, i.e from a different number, we also cache if there is any media that has been sent, but then we schedule a function call for responding to that user.
Sara
SaraOP•3w ago
They looked under the same if statement, sorry I'm reading this on a phone
The shujaa
The shujaa•3w ago
so
needsCache
needsCache
is not being wrongly called. Then am using 2 different numbers for testing, one is connected to be the automatic bot answering, and the other one is for sending the message, so
!messageData.fromMe
!messageData.fromMe
should also not be an issue because am using 2 different numbers for testing. but when send a message to the other number sometimes it responds sometimes it does not. I have tried to cross check the code for any logic errors but am not finding any, and even if there were logic errors, that is not the problem, the problem is the code is running until I see the
console.log("scheduling onInbound for message", insertedId)
console.log("scheduling onInbound for message", insertedId)
message, which is directly immediate to the scheduling code, yet the console log in the function is not appearing meaning no function was scheduled. I dont know if you understood the point?
Sara
SaraOP•3w ago
You need to log inside each individual if statements to see if they're being called
The shujaa
The shujaa•3w ago
Even if the logic was wrong, but we got to the point of calling the console.log directly above the scheduling function i would assume the function needs to be scheduled anyway, irregardless of whether the logic was wrong or not. because am getting the message from the console.log directly above the scheduling function but not the console.log message directly at the beginning of the inbound function, meaning the function was not scheduled and it did not run.
erquhart
erquhart•3w ago
Can you share logs showing the working-then-not-working in action
Sara
SaraOP•3w ago
Ngl, it's the if statements they got doing some :discord_sparkles: magical things :discord_sparkles:

Did you find this page helpful?