Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions conversations/add-participant/addParticipant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Adding Participants (chat and non-chat) */

// add chat participant to the conversation by its identity
await conversation.add("identity");

// add a non-chat participant to the conversation
const proxyAddress = "+11222333";
const address = "+12345678";
await conversation.addNonChatParticipant(proxyAddress, address);

// adds yourself as a conversations sdk user to this conversation
// use after creating the conversation from the SDK
await conversation.join();

conversation.on("participantJoined", (participant) => {
// fired when a participant has joined the conversation
});
18 changes: 18 additions & 0 deletions conversations/add-participant/addParticipant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Adding Participants (chat and non-chat) */
import {Participant} from "@twilio/conversations";

// add chat participant to the conversation by its identity
await conversation.add("identity");

// add a non-chat participant to the conversation
const proxyAddress: string = "+11222333";
const address: string = "+12345678";
await conversation.addNonChatParticipant(proxyAddress, address);

// adds yourself as a conversations sdk user to this conversation
// use after creating the conversation from the SDK
await conversation.join();

conversation.on("participantJoined", (participant: Participant) => {
// fired when a participant has joined the conversation
});
24 changes: 24 additions & 0 deletions conversations/attributes/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Using Attributes */

// add attributes to a message
const message = await conversation.prepareMessage().setBody("message text");

await conversation.setAttributes("attribute");
await conversation.setAttributes(2);
await conversation.setAttributes(true);
await conversation.setAttributes({attributeKey: "attributeValue"});
await conversation.setAttributes(["attribute", "anotherAttribute"]);

// get the attributes
const messageAttributes = message.attributes;

// add participant to the conversation with attributes
await conversation.add("identity", "attribute");
await conversation.add("identity", 2);
await conversation.add("identity", true);
await conversation.add("identity", {attributeKey: "attributeValue"});
await conversation.add("identity", ["attribute", "anotherAttribute"]);

// get the attributes
const participant = await conversation.getParticipantByIdentity("identity");
const participantAttributes = participant.attributes;
26 changes: 26 additions & 0 deletions conversations/attributes/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Using Attributes */

import {MessageBuilder, Participant, JSONValue} from "@twilio/conversations";

// add attributes to a message
const message: MessageBuilder = await conversation.prepareMessage().setBody("message text");

await conversation.setAttributes("attribute");
await conversation.setAttributes(2);
await conversation.setAttributes(true);
await conversation.setAttributes({attributeKey: "attributeValue"});
await conversation.setAttributes(["attribute", "anotherAttribute"]);

// get the attributes
const messageAttributes: JSONValue = message.attributes;

// add participant to the conversation with attributes
await conversation.add("identity", "attribute");
await conversation.add("identity", 2);
await conversation.add("identity", true);
await conversation.add("identity", {attributeKey: "attributeValue"});
await conversation.add("identity", ["attribute", "anotherAttribute"]);

// get the attributes
const participant: Participant = await conversation.getParticipantByIdentity("identity");
const participantAttributes: JSONValue = participant.attributes;
8 changes: 8 additions & 0 deletions conversations/create-conversation/createConversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Creating Conversation */

// create new conversation, all the parameters are optional
await client.createConversation({
attributes: {},
friendlyName: "new conversation",
uniqueName: "new conversation",
});
8 changes: 8 additions & 0 deletions conversations/create-conversation/createConversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Creating Conversation */

// create new conversation, all the parameters are optional
await client.createConversation({
attributes: {},
friendlyName: "new conversation",
uniqueName: "new conversation",
});
16 changes: 16 additions & 0 deletions conversations/deleting/deleting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Deleting and updating Conversations/Messages/Participants */

/* Conversations */

// destroys the conversation, with all its messages and attached media and removes all participants
await conversation.delete();

/* Messages */

// remove a message from the conversation, destroying any attached media
await message.remove();

/* Participants */

// remove participant from the conversation
await participant.remove();
16 changes: 16 additions & 0 deletions conversations/deleting/deleting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Deleting Conversations/Messages/Participants */

/* Conversations */

// destroys the conversation, with all its messages and attached media and removes all participants
await conversation.delete();

/* Messages */

// remove a message from the conversation, destroying any attached media
await message.remove();

/* Participants */

// remove participant from the conversation
await participant.remove();
29 changes: 29 additions & 0 deletions conversations/delivery-receipt-error/deliveryReceiptError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Checking delivery receipts for errors */

// get the list of aggregated delivery receipts
const aggregatedDeliveryReceipt = message.aggregatedDeliveryReceipt;

// retrieve delivery receipt status
if (aggregatedDeliveryReceipt.failed !== "none" || aggregatedDeliveryReceipt.undelivered !== "none") {
// handle error
}

// get the list of delivery receipts
const detailedDeliveryReceipts = await message.getDetailedDeliveryReceipts();

detailedDeliveryReceipts.map((detailedDeliveryReceipt) => {
// check delivery receipt status
if (!detailedDeliveryReceipt.status === "undelivered" && !detailedDeliveryReceipt.status === "failed") {
return;
}

// handle error. the error codes page: https://www.twilio.com/docs/sms/api/message-resource#delivery-related-errors
if (detailedDeliveryReceipt.errorCode === 30006) {
alert("The destination number is unable to receive this message.");
return;
}

if (detailedDeliveryReceipt.errorCode === 30007) {
alert("Your message was flagged as objectionable by the carrier.");
}
});
31 changes: 31 additions & 0 deletions conversations/delivery-receipt-error/deliveryReceiptError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Checking delivery receipts for errors */

// get the list of aggregated delivery receipts
import {AggregatedDeliveryReceipt, DetailedDeliveryReceipt} from "@twilio/conversations";

const aggregatedDeliveryReceipt: AggregatedDeliveryReceipt = message.aggregatedDeliveryReceipt;

// retrieve delivery receipt status
if (aggregatedDeliveryReceipt.failed !== "none" || aggregatedDeliveryReceipt.undelivered !== "none") {
// handle error
}

// get the list of delivery receipts
const detailedDeliveryReceipts: DetailedDeliveryReceipt[] = await message.getDetailedDeliveryReceipts();

detailedDeliveryReceipts.map((detailedDeliveryReceipt: DetailedDeliveryReceipt) => {
// check delivery receipt status
if (!detailedDeliveryReceipt.status === "undelivered" && !detailedDeliveryReceipt.status === "failed") {
return;
}

// handle error. the error codes page: https://www.twilio.com/docs/sms/api/message-resource#delivery-related-errors
if (detailedDeliveryReceipt.errorCode === 30006) {
alert("The destination number is unable to receive this message.");
return;
}

if (detailedDeliveryReceipt.errorCode === 30007) {
alert("Your message was flagged as objectionable by the carrier.");
}
});
25 changes: 25 additions & 0 deletions conversations/delivery-receipts/deliveryReceipts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Retrieving Delivery Receipts (aggregated and detailed) for rendering */

const aggregatedDeliveryReceipt = message.aggregatedDeliveryReceipt;

// get amount (DeliveryAmount) of participants with particular delivery status
const deliveredReceipts = aggregatedDeliveryReceipt?.delivered;
const failedReceipts = aggregatedDeliveryReceipt?.failed;
const readReceipts = aggregatedDeliveryReceipt?.read;
const sentReceipts = aggregatedDeliveryReceipt?.sent;
const undeliveredReceipts = aggregatedDeliveryReceipt?.undelivered;
// get the amount of participants which have the status for the message
const totalReceipts = aggregatedDeliveryReceipt?.total;

if (undeliveredReceipts !== "none") {
// some delivery problems
alert(`Out of ${totalReceipts} sent messages, ${deliveredReceipts} were delivered, ${failedReceipts} have failed.`);
}

// get the list of of delivery receipts
const detailedDeliveryReceipts = await message.getDetailedDeliveryReceipts();

detailedDeliveryReceipts.map((detailedDeliveryReceipt) => {
// get status of the delivery receipts
const receiptStatus = detailedDeliveryReceipt.status;
});
32 changes: 32 additions & 0 deletions conversations/delivery-receipts/deliveryReceipts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Retrieving Delivery Receipts (aggregated and detailed) for rendering */

import {
AggregatedDeliveryReceipt,
DeliveryAmount,
DeliveryStatus,
DetailedDeliveryReceipt
} from "@twilio/conversations";

const aggregatedDeliveryReceipt: AggregatedDeliveryReceipt | null = message.aggregatedDeliveryReceipt;

// get amount (DeliveryAmount) of participants with particular delivery status
const deliveredReceipts: DeliveryAmount = aggregatedDeliveryReceipt?.delivered;
const failedReceipts: DeliveryAmount = aggregatedDeliveryReceipt?.failed;
const readReceipts: DeliveryAmount = aggregatedDeliveryReceipt?.read;
const sentReceipts: DeliveryAmount = aggregatedDeliveryReceipt?.sent;
const undeliveredReceipts: DeliveryAmount = aggregatedDeliveryReceipt?.undelivered;
// get the amount of participants which have the status for the message
const totalReceipts: number = aggregatedDeliveryReceipt?.total;

if (undeliveredReceipts !== "none") {
// some delivery problems
alert(`Out of ${totalReceipts} sent messages, ${deliveredReceipts} were delivered, ${failedReceipts} have failed.`);
}

// get the list of of delivery receipts
const detailedDeliveryReceipts: DetailedDeliveryReceipt[] = await message.getDetailedDeliveryReceipts();

detailedDeliveryReceipts.map((detailedDeliveryReceipt) => {
// get status of the delivery receipts
const receiptStatus: DeliveryStatus = detailedDeliveryReceipt.status;
});
13 changes: 13 additions & 0 deletions conversations/event-handlers/eventHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* event handler examples */

client.on("conversationUpdated", ({conversation, updateReasons}) => {
// Fired when the attributes or the metadata of a conversation have been updated
});

conversation.on("messageUpdated", ({message, updateReasons}) => {
// Fired when data of a message has been updated.
});

participant.on("updated", ({participant, updateReasons}) => {
// Fired when the fields of the participant have been updated.
});
31 changes: 31 additions & 0 deletions conversations/event-handlers/eventHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* event handler examples */

import {
Conversation,
ConversationUpdateReason,
Message,
MessageUpdateReason,
Participant,
ParticipantUpdateReason
} from "@twilio/conversations";

client.on("conversationUpdated", ({conversation, updateReasons}: {
conversation: Conversation,
updateReasons: ConversationUpdateReason[]
}) => {
// Fired when the attributes or the metadata of a conversation have been updated
});

message.on("updated", ({message, updateReasons}: {
message: Message,
updateReasons: MessageUpdateReason[]
}) => {
// Fired when data of a message has been updated.
});

participant.on("updated", ({participant, updateReasons}: {
participant: Participant,
updateReasons: ParticipantUpdateReason[]
}) => {
// Fired when the fields of the participant have been updated.
});
7 changes: 7 additions & 0 deletions conversations/get-conversation/getConversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Get Conversation by SID/Unique Name */

// by SID
const conversation = await client.getConversationBySid("bar");

// by Unique Name
const conversation = await client.getConversationByUniqueName("foo");
8 changes: 8 additions & 0 deletions conversations/get-conversation/getConversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Get Conversation by SID/Unique Name */
import {Conversation} from "@twilio/conversations";

// by SID
const conversation: Conversation = await client.getConversationBySid("bar");

// by Unique Name
const conversation: Conversation = await client.getConversationByUniqueName("foo");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Handle client state change */

client.on("connectionStateChanged", ({state}) => {
// handle new connection state
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Handle client state change */

import {ConnectionState} from "@twilio/conversations";

client.on("connectionStateChanged", ({state}: {
state: ConnectionState
}) => {
// handle new connection state
});
14 changes: 14 additions & 0 deletions conversations/initialization/initialization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Initialization */
import {Client} from "@twilio/conversations";

const client = new Client("token");
client.on("stateChanged", (state) => {
if (state === "failed") {
// The client failed to initialize
return;
}

if (state === "initialized") {
// Use the client
}
});
14 changes: 14 additions & 0 deletions conversations/initialization/initialization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Initialization */
import {Client, State} from "@twilio/conversations";

const client: Client = new Client("token");
client.on('stateChanged', (state: State) => {
if (state === "failed") {
// The client failed to initialize
return;
}

if (state === 'initialized') {
// Use the client
}
});
20 changes: 20 additions & 0 deletions conversations/listening-to-conversation/listeningToConversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Listening to Conversation state change (inactive/closed) */

client.on("conversationUpdated", ({conversation, updateReasons}) => {
if (!updateReasons.includes("state")) {
// conversation state was not updated
return;
}

if (conversation.state.current === "inactive") {
// handle inactive conversation
}

if (conversation.state.current === "closed") {
// handle closed conversation
}
});

client.on("conversationAdded", (conversation) => {
// event triggers when new conversation was created or a conversation becomes visible to the client
});
Loading