Wix Chat: Accessing Messages

Question:
How to access the message sent on chat on js?

Product:
Wix Editor

What are you trying to achieve:
Well, I am a beginner in all this. I was expecting that I won’t need to code anything for my website but unfortunately, Wix hasn’t provided enough functionality.
I need to access (and detect) the messages that are being sent to visitor and sent by visitor. This will be used as custom analytics and also I’m trying to generate custom notifications (that I can show/hide based on messages.)
I am familiar with coding but not with web dev.

What have you already tried:
I have different tried sample codes I found but they aren’t working. Well the functions must be working but I am not sure how to use them properly.

If anyone can share a snippet, where they can access or detect chat messages, that would be really helpful. Please let me know if I am missing anything. Thank you.

Hey there,

If you’re familiar with coding, this shouldn’t be too difficult for you (:

Turn on Dev Mode in your website. Then from the Code Panel on the left, under Public and Backend, click on Handle Backend Events.

This should add a new file to your code panel called events.js

Click on the Wix Chat icon, and from the properties section in the code panel, add the export function named wixChat_onMessage. This will add a bit of default code in the file.

Replace the code with the one below:

export function wixChat_onMessage(event) {

  const message = event.payload.text;
  const participant = event.participantId;

  if (message== "Hi") {
  //execute your function here
  } else  if (message== "ABCD") {
  //execute your function here
  }

}

In this way, you can trigger various events based on the message received.

1 Like

Hi and THANKS A LOT this is working finally!!

I was trying to use the functions from wix chat api (onMessageSent(Message), onMessageReceived(Message), getChannel(channelInfo), onMessageSentToParticipate(), etc, but I kept getting all different sorts of error in them.

Like in the first two it was a type error with Message, even though the same function was working in ebook of @Jorge_Guerra_Pires. Well, I must be missing something i guess. And in the later ones, the functions kept throwing exception and weren’t receiving any information…

But after following these instructions, it seems to be working… really thanks a lot.
Regards,

2 Likes

I have a free course in English using this Wix chat
Creating your own website, no coding needed
learn to create a fully functional website, no need to be a programmer (for PhD students, instructors, and alike)
https://www.udemy.com/course/creating-your-own-website-no-coding-needed/?referralCode=430E72C402E6D98038A7

1 Like

Sorry Pratham, but I am actually not able to make this work. :smiling_face_with_tear: lol

I was viewing the logs before and there were all sorts of messages. I could see the message that I was sending in the logs. But I guess that may have been due to creating the events.js file.

The problem arises when trying to add the export function to the chat icon. I click and open it’s properties but there is no option to add the export function.

I can see the event handlers for other elements like a button in the properties pannel. (I can’t attach a second screenshot due to limit. )

But it’s not working for the chat… What am I doing wrong?


Also, thanks Jorge I’ll check it out

2 Likes

Sorry, my bad, Yes this option does not show up on the properties panel. The code can be added directly to the events.js file without the need to export the function.

However after doing a bit of testing myself, I discovered that showing or hiding stuff on the site itself is not possible since this is a backend function, and this does not allow us to actually read a message on the site itself.

This looked promising but this is throwing up an error when used directly in the frontend, apparently because this is an ‘event’ and can only be used inside events.js using the previous method. As for channels, I am getting a blank payload, even after setting the type to Focused or Business (probably a bug I guess).

For this, I’d suggest you could use Wix Automations that will allow you to connect to various services, and perform numerous actions based on the message received.

Good luck!

Hi Pratham, thanks a lot for your detailed response.

Ahh okay, I have been trying all of those and was getting some sort error on all of them… same with the getChannel… didn’t give me any of the messages whether I used Business or Focused… it was driving me mad.

Initially, that’s what I was doing. However, their automations aren’t that customizable. Like trying to set a conversion based on chat message, or in case of mobile chat, the notifications won’t make any sound, neither will chat pop up on receiving new message. I have tested all settings and even contacted their support, but there wasn’t anything that could be done to customize the notifications.

Kind of disappointed in this service now… good for making a basic website, but not customizable at all.

Anyhow, thanks a lot for your help.

1 Like