The attributes property unlocks the potential to add additional context about specific objects. You can use this functionality to build many capabilities into your application. Some common use cases you could build are:
This property is optional, and the field value is a JSON object that can store up to 16 KB depending on the object. If the attributes are not set, then an empty object is returned.
You can use the attributes
field in any of the following Conversations objects:
Refer to Conversations length limits for more details.
You can set the attributes of an object by calling the update method on the object you want to add to. For example, you can add attributes to an existing Message by passing the correct method and specifying the attributes on that Message object.
You can also add attributes to objects when you create them. For example, you can set attributes when you add a Participant to a Conversation.
To retrieve the attributes for an object, just make sure to invoke the corresponding method or property. The attributes will be returned in JSON format.
1/* Using Attributes */23// add attributes to a message4const message = await conversation.prepareMessage().setBody("message text");56await conversation.setAttributes("attribute");7await conversation.setAttributes(2);8await conversation.setAttributes(true);9await conversation.setAttributes({attributeKey: "attributeValue"});10await conversation.setAttributes(["attribute", "anotherAttribute"]);1112// get the attributes13const messageAttributes = message.attributes;1415// add participant to the conversation with attributes16await conversation.add("identity", "attribute");17await conversation.add("identity", 2);18await conversation.add("identity", true);19await conversation.add("identity", {attributeKey: "attributeValue"});20await conversation.add("identity", ["attribute", "anotherAttribute"]);2122// get the attributes23const participant = await conversation.getParticipantByIdentity("identity");24const participantAttributes = participant.attributes;
To receive events about updates to object attributes, listen for "update" events at the Client object (e.g. conversationUpdated
, messageUpdated
, etc.). The event update reason will indicate that the attributes were changed and include the updated object.
Well done! You can continue learning more about Conversations by checking out the following guides: