Skip to content

Commit

Permalink
DEVEXP-346: Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Mar 18, 2024
1 parent 0cad94c commit fe8b2aa
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 22 deletions.
47 changes: 25 additions & 22 deletions examples/simple-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,31 @@ yarn run numbers:regions:list

### SMS

| Service | Sample application name and location | Required parameters |
|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
| Groups | [./src/sms/groups/list/list.ts](./src/sms/groups/list/list.ts) | |
| | [./src/sms/groups/create/create.ts](./src/sms/groups/create/create.ts) | |
| | [./src/sms/groups/get/get.ts](./src/sms/groups/get/get.ts) | `GROUP_ID` |
| | [./src/sms/groups/getPhoneNumbers/getPhoneNumbers.ts](./src/sms/groups/getPhoneNumbers/getPhoneNumbers.ts) | `GROUP_ID` |
| | [./src/sms/groups/replace/replace.ts](./src/sms/groups/replace/replace.ts) | `GROUP_ID` |
| | [./src/sms/groups/update/update.ts](./src/sms/groups/update/update.ts) | `GROUP_ID` |
| | [./src/sms/groups/delete/delete.ts](./src/sms/groups/delete/delete.ts) | `GROUP_ID` |
| Batches | [./src/sms/batches/send.ts](./src/sms/batches/send.ts) | `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/list.ts](./src/sms/batches/list.ts) | |
| | [./src/sms/batches/dry-run.ts](./src/sms/batches/dry-run.ts) | `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/get.ts](./src/sms/batches/get.ts) | `BATCH_ID` |
| | [./src/sms/batches/update.ts](./src/sms/batches/update.ts) | `BATCH_ID` |
| | [./src/sms/batches/replace.ts](./src/sms/batches/replace.ts) | `BATCH_ID` |
| | [./src/sms/batches/cancel.ts](./src/sms/batches/cancel.ts) | `BATCH_ID` |
| | [./src/sms/batches/delivery-feedback.ts](./src/sms/batches/delivery-feedback.ts) | `BATCH_ID` |
| DeliveryReports | [./src/sms/delivery-reports/list.ts](./src/sms/delivery-reports/list.ts) | |
| | [./src/sms/delivery-reports/getByBatchId.ts](./src/sms/delivery-reports/getByBatchId.ts) | `BATCH_ID` |
| | [./src/sms/delivery-reports/getByPhoneNumber.ts](./src/sms/delivery-reports/getByPhoneNumber.ts) | `BATCH_ID`, `RECIPIENT_PHONE_NUMBER` |
| Inbounds | [./src/sms/inbounds/list.ts](./src/sms/inbounds/list.ts) | |
| | [./src/sms/inbounds/get.ts](./src/sms/inbounds/get.ts) | `INBOUND_ID` |
| Service | Sample application name and location | Required parameters |
|-----------------|------------------------------------------------------------------------------------------------------------|------------------------------------------------|
| Groups | [./src/sms/groups/list/list.ts](./src/sms/groups/list/list.ts) | |
| | [./src/sms/groups/create/create.ts](./src/sms/groups/create/create.ts) | |
| | [./src/sms/groups/get/get.ts](./src/sms/groups/get/get.ts) | `GROUP_ID` |
| | [./src/sms/groups/getPhoneNumbers/getPhoneNumbers.ts](./src/sms/groups/getPhoneNumbers/getPhoneNumbers.ts) | `GROUP_ID` |
| | [./src/sms/groups/replace/replace.ts](./src/sms/groups/replace/replace.ts) | `GROUP_ID` |
| | [./src/sms/groups/update/update.ts](./src/sms/groups/update/update.ts) | `GROUP_ID` |
| | [./src/sms/groups/delete/delete.ts](./src/sms/groups/delete/delete.ts) | `GROUP_ID` |
| Batches | [./src/sms/batches/send.ts](./src/sms/batches/send.ts) | `PHONE_NUMBER` + `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/sendTextMessage.ts](./src/sms/batches/sendTextMessage.ts) | `PHONE_NUMBER` + `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/sendBinaryMessage.ts](./src/sms/batches/sendBinaryMessage.ts) | `PHONE_NUMBER` + `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/sendMediaMessage.ts](./src/sms/batches/sendMediaMessage.ts) | `PHONE_NUMBER` + `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/list.ts](./src/sms/batches/list.ts) | |
| | [./src/sms/batches/dry-run.ts](./src/sms/batches/dry-run.ts) | `RECIPIENT_PHONE_NUMBER` |
| | [./src/sms/batches/get.ts](./src/sms/batches/get.ts) | `BATCH_ID` |
| | [./src/sms/batches/update.ts](./src/sms/batches/update.ts) | `BATCH_ID` |
| | [./src/sms/batches/replace.ts](./src/sms/batches/replace.ts) | `BATCH_ID` |
| | [./src/sms/batches/cancel.ts](./src/sms/batches/cancel.ts) | `BATCH_ID` |
| | [./src/sms/batches/delivery-feedback.ts](./src/sms/batches/delivery-feedback.ts) | `BATCH_ID` |
| DeliveryReports | [./src/sms/delivery-reports/list.ts](./src/sms/delivery-reports/list.ts) | |
| | [./src/sms/delivery-reports/getByBatchId.ts](./src/sms/delivery-reports/getByBatchId.ts) | `BATCH_ID` |
| | [./src/sms/delivery-reports/getByPhoneNumber.ts](./src/sms/delivery-reports/getByPhoneNumber.ts) | `BATCH_ID`, `RECIPIENT_PHONE_NUMBER` |
| Inbounds | [./src/sms/inbounds/list.ts](./src/sms/inbounds/list.ts) | |
| | [./src/sms/inbounds/get.ts](./src/sms/inbounds/get.ts) | `INBOUND_ID` |

### Verification

Expand Down
3 changes: 3 additions & 0 deletions examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
"sms:groups:update": "ts-node src/sms/groups/update/update.sinch-client.ts",
"sms:groups:delete": "ts-node src/sms/groups/delete/delete.sinch-client.ts",
"sms:batches:send": "ts-node src/sms/batches/send.ts",
"sms:batches:sendTextMessage": "ts-node src/sms/batches/sendTextMessage.ts",
"sms:batches:sendBinaryMessage": "ts-node src/sms/batches/sendBinaryMessage.ts",
"sms:batches:sendMediaMessage": "ts-node src/sms/batches/sendMediaMessage.ts",
"sms:batches:list": "ts-node src/sms/batches/list.ts",
"sms:batches:dry-run": "ts-node src/sms/batches/dry-run.ts",
"sms:batches:get": "ts-node src/sms/batches/get.ts",
Expand Down
9 changes: 9 additions & 0 deletions examples/simple-examples/src/sms/batches/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ import { SendSMSRequestData } from '@sinch/sdk-core';

if (printFormat === 'pretty') {
console.log(`The SMS has been sent successfully. Here is the batch id: ${response.id}`);
if(response.type === 'mt_text') {
console.log(`The message was: '${response.body}'`);
}
if(response.type === 'mt_binary') {
console.log(`The message (decoded) was: '${atob(response.body!)}'`);
}
if(response.type === 'mt_media') {
console.log(`The message was: '${response.body?.message}'`);
}
} else {
printFullResponse(response);
}
Expand Down
42 changes: 42 additions & 0 deletions examples/simple-examples/src/sms/batches/sendBinaryMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { SendBinaryMessageRequestData, textToHex } from '@sinch/sdk-core';
import {
getPhoneNumberFromConfig,
getPrintFormat,
getRecipientPhoneNumberFromConfig,
initSmsClient,
printFullResponse,
} from '../../config';

(async () => {
console.log('**************');
console.log('* SendBinary *');
console.log('**************');

const recipientPhoneNumber = getRecipientPhoneNumberFromConfig();
const senderPhoneNumber = getPhoneNumberFromConfig();

const requestData: SendBinaryMessageRequestData = {
sendSMSRequestBody: {
type: 'mt_binary',
to: [
recipientPhoneNumber,
],
from: senderPhoneNumber,
body: 'SGVsbG8sIHRoaXMgaXMgYSBTTVMgZnJvbSBTaW5jaA==',
udh: textToHex('UserDataHeader'),
delivery_report: 'full',
},
};

const sinchClient = initSmsClient();
const response = await sinchClient.sms.batches.sendBinaryMessage(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The SMS has been sent successfully. Here is the batch id: ${response.id}\nThe message (decoded) was: '${atob(response.body!)}'`);
} else {
printFullResponse(response);
}
console.log(`You may want to update your .env file with the following value: BATCH_ID=${response.id}`);
})();
51 changes: 51 additions & 0 deletions examples/simple-examples/src/sms/batches/sendMediaMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { SendMediaMessageRequestData } from '@sinch/sdk-core';
import {
getPhoneNumberFromConfig,
getPrintFormat,
getRecipientPhoneNumberFromConfig,
initSmsClient,
printFullResponse,
} from '../../config';

(async () => {
console.log('********************');
console.log('* SendMediaMessage *');
console.log('********************');

const recipientPhoneNumber = getRecipientPhoneNumberFromConfig();
const senderPhoneNumber = getPhoneNumberFromConfig();

const requestData: SendMediaMessageRequestData = {
sendSMSRequestBody: {
type: 'mt_media',
to: [
recipientPhoneNumber,
],
from: senderPhoneNumber,
parameters: {
name: {
[recipientPhoneNumber]: 'John',
default: 'there',
},
},
body: {
url: 'https://media.body.url',
message: 'Text message coming along with the media file',
},
delivery_report: 'full',
strict_validation: true,
},
};

const sinchClient = initSmsClient();
const response = await sinchClient.sms.batches.sendMediaMessage(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The MMS has been sent successfully. Here is the batch id: ${response.id}\nThe message was: '${response.body?.message}'`);
} else {
printFullResponse(response);
}
console.log(`You may want to update your .env file with the following value: BATCH_ID=${response.id}`);
})();
47 changes: 47 additions & 0 deletions examples/simple-examples/src/sms/batches/sendTextMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SendTextMessageRequestData } from '@sinch/sdk-core';
import {
getPhoneNumberFromConfig,
getPrintFormat,
getRecipientPhoneNumberFromConfig,
initSmsClient,
printFullResponse,
} from '../../config';

(async () => {
console.log('*******************');
console.log('* SendTextMessage *');
console.log('*******************');

const recipientPhoneNumber = getRecipientPhoneNumberFromConfig();
const senderPhoneNumber = getPhoneNumberFromConfig();

const requestData: SendTextMessageRequestData = {
sendSMSRequestBody: {
type: 'mt_text',
to: [
recipientPhoneNumber,
],
from: senderPhoneNumber,
parameters: {
name: {
[recipientPhoneNumber]: 'John',
default: 'there',
},
},
body: 'Hi ${name}!',
delivery_report: 'full',
},
};

const sinchClient = initSmsClient();
const response = await sinchClient.sms.batches.sendTextMessage(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The SMS has been sent successfully. Here is the batch id: ${response.id}\nThe message was: '${response.body}'`);
} else {
printFullResponse(response);
}
console.log(`You may want to update your .env file with the following value: BATCH_ID=${response.id}`);
})();

0 comments on commit fe8b2aa

Please sign in to comment.