stream-chat-js
stream-chat-js copied to clipboard
Support sending system messages
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
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,
Hi,
I need to implement something like this:

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.
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,
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.
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.
+1 for request this as well...and agree with the assertion by @kainosnoema that Campaigns would not be an appropriate solution.
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
+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 :/
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 });
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.
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.