ConversationBuilder

Example

1import * as TalkRn from '@talkjs/react-native';
2
3const me: TalkRn.User = {
4 id: '123456789',
5 name: 'Alice',
6 email: '[email protected]',
7 photoUrl: 'https://talkjs.com/images/avatar-1.jpg',
8 welcomeMessage: 'Hey there! How are you? :-)',
9};
10
11const other: TalkRn.User = {
12 id: '432156789',
13 name: 'Sebastian',
14 email: '[email protected]',
15 photoUrl: 'https://talkjs.com/images/avatar-5.jpg',
16 welcomeMessage: 'Hey, how can I help? https://google.com',
17};
18
19const conversationId = TalkRn.oneOnOneId(me.id, other.id);
20const conversationBuilder = TalkRn.getConversationBuilder(conversationId);
21
22conversationBuilder.setParticipant(me);
23conversationBuilder.setParticipant(other, { notify: false });
24
25conversationBuilder.setAttributes({ subject: 'Random conversation' });

Properties

FieldTypeDescription
subject
(optional)
string | nullAn optional conversation subject which will be displayed in the chat header.
photoUrl
(optional)
string | nullAn optional URL to a photo which will be shown as the photo for the conversation.
welcomeMessages
(optional)
string[] | nullMessages which are sent at the beginning of a chat. In this case the messages will appear as system messages.
custom
(optional)
CustomData | nullAllows custom conversation metadata to be stored

Methods

leave

1leave(): Promise<void>

Removes the current user from this conversation.

Returns

Promise<boolean>

A promise that resolves with true upon success (indicating that the user has been removed from this conversation), or false when the user did not have the appropriate permissions, as defined in their role. You can find the permissions for a role on the Chat UI page of your TalkJS dashboard. Make sure to select the relevant role from the dropdown selector at the top of the page.

The promise may reject in case of an unexpected error.

setParticipant

1setParticipant(user: User, settings?: ParticipationSettings): void

Sets a participant of the conversation.

This method is idempotent and can be called multiple times.

Parameters

NameTypeDescription
user RequiredUserA User object that identifies the person who is a participant of the conversation. The user is uniquely identified by their id; all other fields (name, photo etc) are overwritten in the TalkJS database each time they change.
settingsParticipationSettingsAn optional setting of participation, can be an initial access right or if user should be notified.

Returns

void

setAttributes

1setAttributes(attributes: ConversationAttributes): void

Used to set certain attributes for a specific conversation.

Parameters

NameTypeDescription
attributes RequiredConversationAttributesConversation attributes

Returns

void

sendMessage

1sendMessage(text: string, options?: SendMessageOptions): Promise<void>

Sends a text message in a given conversation.

Parameters

NameTypeDescription
text RequiredstringThe message body that is to be sent.
optionsSendMessageOptionsOptional custom data you wish to be associated with the message.

Returns

Promise<void>

Used by


Type Definitions

SendMessageOptions

Example

1{
2 custom: {
3 location: 'here'
4 }
5}

Properties

FieldTypeDescription
customCustomDataAn object with any custom data that you may wish to associate with this message. The custom data is sent back to you via webhooks and the REST API.

ConversationAttributes

Example

1{
2 subject: 'Our conversation',
3 photoUrl: 'https://talkjs.com/images/avatar-1.jpg',
4 welcomeMessages: ['Hello'],
5 custom: {
6 store_id: 'g234jl'
7 }
8}

Properties

FieldTypeDescription
subject (optional)string | nullA human-readable subject of the conversation.
Supports formatted links in a Markdown-style syntax. URLs and email addresses are made clickable, and emojis made to work cross-platform.
photoUrl (optional)string | nullThe URL of a photo to be used for this conversation in the TalkJS UI in case there are more than 2 participants (TalkJS shows the photo of the other participant in a 1-on-1 conversation)
welcomeMessages (optional)string[] | nullMessages which are sent at the beginning of a chat. In this case the messages will appear as system messages.
custom (optional)CustomData | nullCustom metadata that is stored with the conversation

ParticipationSettings

Example

1{
2 access: 'Read',
3 notify: false
4}

Properties

FieldTypeDescription
access (optional)"Read" | "ReadWrite"Specifies the participant's access permission for a conversation.
notify (optional)boolean"MentionsOnly"Specifies the participant's notification settings.