stream-chat-js icon indicating copy to clipboard operation
stream-chat-js copied to clipboard

Support sending system messages

Open ivan-rodriguez-ag opened this issue 4 years ago • 9 comments

Hi,

I am trying to send system message from my node server with the following code:

const { StreamChat } = require('stream-chat'); 

const client = StreamChat.getInstance('1234','1234');
const channel = client.channel('commerce', '1234') 

channel.sendMessage({ 
    text: 'Try', 
    type: 'system',
}).then(response => console.log({ response }));

The sendMessage action fails with the error:

UnhandledPromiseRejectionWarning: Error: StreamChat error code 4: SendMessage failed with error: "message.type is a reserved field"

As i see in the documentation this field is available in server side API, i imagine the stream-chat packages is what i have to use because the documentation says that this is the backend client but in this repo i see that is described as JS / Browser Client.

Documentation: https://getstream.io/chat/docs/node/message_format/?language=javascript#message-types

What am i doing wrong?

gz#9470

ivan-rodriguez-ag avatar Feb 17 '21 10:02 ivan-rodriguez-ag

Hi,

Thanks for reaching out. The stream-chat JS client can be both used for server-side or client-side usage.

Regarding your issue, I believe it is due to a mistake in the docs as the information provided for the type field is wrong. message.type is indeed a reserved field that is only set by the API backend. It is not possible to set or update it on a message yourself. Still, you can introduce your own message custom field in the message, e.g message_type Sorry for the confusion it caused, the docs will be updated very soon with the correct information.

Best,

SeniYG avatar Feb 17 '21 11:02 SeniYG

Hi,

I need to implement something like this: image

My problem is the "blue" message that is not related to any user. When i send a message i need to add a user_id, i thought if i set type "system" user is not required. If i use the approach you tell me the message is related to a user not as a globarl or not related message.

I have tried this in the GO library and the type is not copied in the GO client: https://github.com/GetStream/stream-chat-go/blob/d80c989fdc19b5aacb47d8134bcbf3263561e27a/message.go#L73-L94

Is this a shared behavior in stream chat? How it is possible to do bots if we can't change the type? I am a little bit lost with all of this and maybe introduce GO in this conversation is not right but i tried with both and with any of them is possible to do it.

ivan-rodriguez-ag avatar Feb 17 '21 15:02 ivan-rodriguez-ag

Hi there,

Sorry for the delay on this. Indeed, the API does not support sending system messages to push marketing content for example on behalf of a master app system user. Our engineers are currently building a Campaigns feature that allows customers to reach their users directly via marketing and transactional campaign messages with fine control over the specific audience targeting.

The feature is currently under active development but it’s approaching a “beta” stage, we'll make an announcement once the feature is available.

Best regards,

SeniYG avatar Feb 23 '21 17:02 SeniYG

Hey @irodriguezagentero

There's actually a way to do what you want, but it will require changing channel data. Update channel docs

When performing a channel update you can specify a message which will be sent as a system message to the channel. The only caveat is that you will have to change some data to make it work. If channel data is not modified as a result of the request, the message won't be sent.

UPD: you can also change message type using Before message send webhook. Although this is probably not what you want.

AnatolyRugalev avatar Feb 26 '21 19:02 AnatolyRugalev

FWIW we also need a way to send messages that weren't triggered by a specific user... for example to notify channel members of events related to the channel but triggered by other events in our product.

The "Campaigns" feature mentioned earlier doesn't seem appropriate for this as it's not marketing related—we're building a chat tool so the rest of the channel functionality is important, we just need the ability to send automated messages that aren't tied to a specific user.

The update channel workaround above is a little hacky, but it might work... could just change a timestamp property on the channel so the message is sent every time. Just wanted to add our vote to first-class support for this as it's something we're needing.

kainosnoema avatar Mar 27 '21 05:03 kainosnoema

+1 for request this as well...and agree with the assertion by @kainosnoema that Campaigns would not be an appropriate solution.

jamesholcomb avatar Apr 08 '21 20:04 jamesholcomb

I am also getting this problem.

I am trying to build a chat bot where the first message has type ephemeral to allow the user to cancel the action. Similar to the UX of the /giphy command, except there will be multiple ephemeral stages.

Is there a way I can this without having access to the type prop on the message?

This is example I have been following which also seems to try and access these props

peterferguson avatar May 21 '21 22:05 peterferguson

+1 for this as well.

Also there is no info on how to construct an ephemeral message so while it succeeds to update the type in the presend webhook it never shows up for a user for me :/

plwalters avatar Jul 26 '21 21:07 plwalters

I know this thread is old. But apparently, you can update the channel metadata and attach a system message that will be posted with the update. We literally set the same channel name and update some hidden property to a new value to get a nice system message in chat:

  channel.update({
    name: channel.data?.name ?? 'Chat',
    someWtfParamNameThatMakesNoSense: '<RANDOM_STRING_GOES_HERE>',
  }, { 
    text: 'This is an example of a system message',
    user_id: 'XXXXXX', // only if you are going to send the system message from the backend
    silent: true,
  }, { skip_push: true });

dmitriiivashko avatar Apr 08 '22 21:04 dmitriiivashko

The workarounds mentioned in the thread will work for the time being. We've also added this as a feature request to our backlog.

Because this issue describes a backend feature and is not specific to the javascript SDK, I'm going to close this issue.

miagilepner avatar Aug 24 '22 08:08 miagilepner

Has there been any updates to this? I am running into the same issues as others have mentioned in this thread and I'm looking for a neat way to send a system message to chat after a specific event has occured.

CloudMunk avatar Sep 27 '22 10:09 CloudMunk