Skip to contentSkip to navigationSkip to topbar
On this page

Conversations Attributes



Overview

overview page anchor

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:

  • Skill-based routing for Conversations
  • Avatar URLs in a User profile
  • Context about customers (i.e. referral source, product type, etc.)
  • Message replies/threading
  • Emoji reactions to messages
  • Profile information
  • Message metadata
  • Customer loyalty status
  • Non-chat Participant display name

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:

  • Participant (up to 4 KB)
  • User (up to 16 KB)
  • Conversation (up to 16 KB)
  • Message (up to 4 KiB)

Refer to Conversations length limits(link takes you to an external page) for more details.


Set or modify Attributes

set-or-modify-attributes page anchor

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.

Set or Modify AttributesLink to code sample: Set or Modify Attributes
1
/* Using Attributes */
2
3
// add attributes to a message
4
const message = await conversation.prepareMessage().setBody("message text");
5
6
await conversation.setAttributes("attribute");
7
await conversation.setAttributes(2);
8
await conversation.setAttributes(true);
9
await conversation.setAttributes({attributeKey: "attributeValue"});
10
await conversation.setAttributes(["attribute", "anotherAttribute"]);
11
12
// get the attributes
13
const messageAttributes = message.attributes;
14
15
// add participant to the conversation with attributes
16
await conversation.add("identity", "attribute");
17
await conversation.add("identity", 2);
18
await conversation.add("identity", true);
19
await conversation.add("identity", {attributeKey: "attributeValue"});
20
await conversation.add("identity", ["attribute", "anotherAttribute"]);
21
22
// get the attributes
23
const participant = await conversation.getParticipantByIdentity("identity");
24
const participantAttributes = participant.attributes;

Listen for changes to these objects

listen-for-changes-to-these-objects page anchor

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:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.