Skip to content

Commit fe2947f

Browse files
authored
Merge pull request TwilioDevEd#966 from TwilioDevEd/sdk-samples
new js and ts sdk samples
2 parents 05f2b35 + e68f0cb commit fe2947f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+917
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Adding Participants (chat and non-chat) */
2+
3+
// add chat participant to the conversation by its identity
4+
await conversation.add("identity");
5+
6+
// add a non-chat participant to the conversation
7+
const proxyAddress = "+11222333";
8+
const address = "+12345678";
9+
await conversation.addNonChatParticipant(proxyAddress, address);
10+
11+
// adds yourself as a conversations sdk user to this conversation
12+
// use after creating the conversation from the SDK
13+
await conversation.join();
14+
15+
conversation.on("participantJoined", (participant) => {
16+
// fired when a participant has joined the conversation
17+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Adding Participants (chat and non-chat) */
2+
import {Participant} from "@twilio/conversations";
3+
4+
// add chat participant to the conversation by its identity
5+
await conversation.add("identity");
6+
7+
// add a non-chat participant to the conversation
8+
const proxyAddress: string = "+11222333";
9+
const address: string = "+12345678";
10+
await conversation.addNonChatParticipant(proxyAddress, address);
11+
12+
// adds yourself as a conversations sdk user to this conversation
13+
// use after creating the conversation from the SDK
14+
await conversation.join();
15+
16+
conversation.on("participantJoined", (participant: Participant) => {
17+
// fired when a participant has joined the conversation
18+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Using Attributes */
2+
3+
import {MessageBuilder, Participant, JSONValue} from "@twilio/conversations";
4+
5+
// add attributes to a message
6+
const message: MessageBuilder = await conversation.prepareMessage().setBody("message text");
7+
8+
await conversation.setAttributes("attribute");
9+
await conversation.setAttributes(2);
10+
await conversation.setAttributes(true);
11+
await conversation.setAttributes({attributeKey: "attributeValue"});
12+
await conversation.setAttributes(["attribute", "anotherAttribute"]);
13+
14+
// get the attributes
15+
const messageAttributes: JSONValue = message.attributes;
16+
17+
// add participant to the conversation with attributes
18+
await conversation.add("identity", "attribute");
19+
await conversation.add("identity", 2);
20+
await conversation.add("identity", true);
21+
await conversation.add("identity", {attributeKey: "attributeValue"});
22+
await conversation.add("identity", ["attribute", "anotherAttribute"]);
23+
24+
// get the attributes
25+
const participant: Participant = await conversation.getParticipantByIdentity("identity");
26+
const participantAttributes: JSONValue = participant.attributes;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Creating Conversation */
2+
3+
// create new conversation, all the parameters are optional
4+
await client.createConversation({
5+
attributes: {},
6+
friendlyName: "new conversation",
7+
uniqueName: "new conversation",
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Creating Conversation */
2+
3+
// create new conversation, all the parameters are optional
4+
await client.createConversation({
5+
attributes: {},
6+
friendlyName: "new conversation",
7+
uniqueName: "new conversation",
8+
});

conversations/deleting/deleting.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Deleting and updating Conversations/Messages/Participants */
2+
3+
/* Conversations */
4+
5+
// destroys the conversation, with all its messages and attached media and removes all participants
6+
await conversation.delete();
7+
8+
/* Messages */
9+
10+
// remove a message from the conversation, destroying any attached media
11+
await message.remove();
12+
13+
/* Participants */
14+
15+
// remove participant from the conversation
16+
await participant.remove();

conversations/deleting/deleting.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Deleting Conversations/Messages/Participants */
2+
3+
/* Conversations */
4+
5+
// destroys the conversation, with all its messages and attached media and removes all participants
6+
await conversation.delete();
7+
8+
/* Messages */
9+
10+
// remove a message from the conversation, destroying any attached media
11+
await message.remove();
12+
13+
/* Participants */
14+
15+
// remove participant from the conversation
16+
await participant.remove();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Checking delivery receipts for errors */
2+
3+
// get the list of aggregated delivery receipts
4+
const aggregatedDeliveryReceipt = message.aggregatedDeliveryReceipt;
5+
6+
// retrieve delivery receipt status
7+
if (aggregatedDeliveryReceipt.failed !== "none" || aggregatedDeliveryReceipt.undelivered !== "none") {
8+
// handle error
9+
}
10+
11+
// get the list of delivery receipts
12+
const detailedDeliveryReceipts = await message.getDetailedDeliveryReceipts();
13+
14+
detailedDeliveryReceipts.map((detailedDeliveryReceipt) => {
15+
// check delivery receipt status
16+
if (!detailedDeliveryReceipt.status === "undelivered" && !detailedDeliveryReceipt.status === "failed") {
17+
return;
18+
}
19+
20+
// handle error. the error codes page: https://www.twilio.com/docs/sms/api/message-resource#delivery-related-errors
21+
if (detailedDeliveryReceipt.errorCode === 30006) {
22+
alert("The destination number is unable to receive this message.");
23+
return;
24+
}
25+
26+
if (detailedDeliveryReceipt.errorCode === 30007) {
27+
alert("Your message was flagged as objectionable by the carrier.");
28+
}
29+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Checking delivery receipts for errors */
2+
3+
// get the list of aggregated delivery receipts
4+
import {AggregatedDeliveryReceipt, DetailedDeliveryReceipt} from "@twilio/conversations";
5+
6+
const aggregatedDeliveryReceipt: AggregatedDeliveryReceipt = message.aggregatedDeliveryReceipt;
7+
8+
// retrieve delivery receipt status
9+
if (aggregatedDeliveryReceipt.failed !== "none" || aggregatedDeliveryReceipt.undelivered !== "none") {
10+
// handle error
11+
}
12+
13+
// get the list of delivery receipts
14+
const detailedDeliveryReceipts: DetailedDeliveryReceipt[] = await message.getDetailedDeliveryReceipts();
15+
16+
detailedDeliveryReceipts.map((detailedDeliveryReceipt: DetailedDeliveryReceipt) => {
17+
// check delivery receipt status
18+
if (!detailedDeliveryReceipt.status === "undelivered" && !detailedDeliveryReceipt.status === "failed") {
19+
return;
20+
}
21+
22+
// handle error. the error codes page: https://www.twilio.com/docs/sms/api/message-resource#delivery-related-errors
23+
if (detailedDeliveryReceipt.errorCode === 30006) {
24+
alert("The destination number is unable to receive this message.");
25+
return;
26+
}
27+
28+
if (detailedDeliveryReceipt.errorCode === 30007) {
29+
alert("Your message was flagged as objectionable by the carrier.");
30+
}
31+
});

0 commit comments

Comments
 (0)