-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Emit chat state changed event when chat state changes (e.g. one contact starts typing) #3374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
emits events when a chat state changes, possible states are: [available, unavailable, typing, recording]
nice contribution |
The biggest problem with this function was beign reliable enough for production. Have you tested with changes on more than 10 chats at one time ? Does it auto subscribe for the chat "typping" event ? |
@tuyuribr I did not test for multiple chats at the same time. I also only tested using it does not auto subscribe for the "typing" event, instead it "subscribes" to changes on the |
@lucas-almeida026 one question though, Does it also work when user who is connected starts typing? |
Wouldn't it make sense to separate the statuses into two categories: 'Action' (Typing, Recording, Standby) and 'Status' (Available, Unavailable)? Right now, we mix these states, which creates confusion—especially when trying to detect if the user is offline. For example, during some tests with this PR, when Puppeteer navigates to another chat, it triggers an 'Unavailable' status even though the user is actually in 'Typing'. This leads to misleading behavior and makes it harder to handle presence accurately. |
I totally agree with you, it makes sense to separate these two kinds of statuses. If anyone is interested in continue the work there is a couple of points I think is useful to share:
|
I understand. I'm personally interested in this functionality because I'm working with WhatsApp integrated with AIs. My project focuses on humanizing the AI, and often the person chatting doesn't even realize they're not talking to a human due to several subtle details. I noticed and tested the issues you mentioned about it not working. Part of the rendering seems to depend on the user's activity. I might be biased, but I believe it's just a WhatsApp resource-saving mechanism to avoid constantly showing "recording..." or "typing..." What I noticed is that recently clicked or opened chats (those that have been selected and opened) have this status change feature active. I looked through part of the repository, and I'm really trying to overcome the laziness to push a modification using a getter that wraps the underlying class function to retrieve: get isTyping() {
// Logic...
}
get isRecording() {
// Logic
} All of this inside the PrivateChat class. If I can finish up my current SaaS project, I’ll aim to build and submit a PR for this part by next weekend |
Hello everyone, any updates here? |
PR Details
The PR adds the functionality requested on #82 and on related issues: #1137, #2636, #3372.
This feature introduces the ability to detect chat states such as 'available', 'unavailable', 'typing' and 'recording', addressing a longstanding request from the community
Description
3 main changes were made:
CHAT_STATE_CHANGED
event on the Events object fromsrc/utiol/Contants.js
src/Client.js:249
injectPresenceModelInstance
: this function wraps the underlying modelClass from the Store.Presence object. the modelClass is used to generate all the models that represents a presence for a given chat.This is done to "inject" the get and set properties on every single instance generate my the model class to be able to call the emit function every time the model chat state property is update
execPresenceModelInjection
: this function is called when the Store.Presence object becomes available (it takes a couple of milliseconds for it to load) and calls theinjectPresenceModelInstance
to inject every model already created.The function also makes sure to maintain the prototype chain of every injected object to avoid breaking underlying functionality
Motivation and Context
The main objective is to have one more way to improve user experience of apps built using whatapp-web.js. By providing a way to handle events related to chat state changes developers will be able to create interaction flows with a finer level of control.
An example use case would be: prevent an automatic response from being sent if the target user is typing, so it doesn't interrupt the user.
How Has This Been Tested
I tested manually using the
shell
command from thepackage.json
file.Actions:
npm run shell
client.on('chat_state_changed', console.log)
{ chatId: '*********@c.us', chatState: 'typing' }
{ chatId: '*********@c.us', chatState: 'unavailable' }
Environment
Types of changes
Checklist
I have updated the documentation accordingly (index.d.ts).