How to send SystemMessage?
superaldo opened this issue ยท 2 comments
Hello ๐
I have searched in all the questions and I did not find anything that helps me, my questions is simple.
someone know how to send system message into the chat, I try do it with this code but its no work
//////////////////////////
void _sendSystemMessage(String idx) async {
final systemMessage = types.SystemMessage(
text: 'mensaje de sistema', id: idx, type: types.MessageType.system);
FirebaseChatCore.instance.updateMessage(
systemMessage,
widget.room.id,
);
}
////////////////////////
I know that changing the "type" field on my firestore to "text" to "system" changes the appearance of the message, and I suppose there must be a method to send system messages.
in the end i wrote this method to send a system type message, in case someone needs it.
void sendSystemMessage(dynamic partialMessage, String roomId) async {
auth.User? firebaseUser = auth.FirebaseAuth.instance.currentUser;
if (firebaseUser == null) return;
types.Message? message;
if (partialMessage is types.PartialText) {
message = types.TextMessage.fromPartial(
author: types.User(id: firebaseUser.uid),
id: '',
partialText: partialMessage,
);
}
if (message != null) {
final messageMap = message.toJson();
messageMap.removeWhere((key, value) => key == 'author' || key == 'id');
messageMap['authorId'] = firebaseUser.uid;
messageMap['createdAt'] = FieldValue.serverTimestamp();
messageMap['updatedAt'] = FieldValue.serverTimestamp();
messageMap['type'] = 'system';
FirebaseFirestore ref = FirebaseFirestore.instance;
ref.collection('rooms/$roomId/messages').add(messageMap);
ref
.collection('rooms')
.doc(roomId)
.update({'updatedAt': FieldValue.serverTimestamp()});
}
}
Yes, I think system messages were added later to the chat UI and I barely have time to look at chat UI now, so this one is outdated. But you got it right, it can be easily extended, it just uses plain firestore libs inside.