Skip to content

Commit

Permalink
DEVEXP-557: Add snippets for SMS API
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Sep 16, 2024
1 parent 960c736 commit 104f676
Show file tree
Hide file tree
Showing 34 changed files with 5,051 additions and 836 deletions.
3 changes: 3 additions & 0 deletions .github/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ jobs:
- name: Install dependencies
run: npm install

- name: Run TypeScript compiler
run: tsc

- name: Lint and format check
run: npm run lint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
node_modules
**/tsconfig.tsbuildinfo
3 changes: 1 addition & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export default [...compat.extends('eslint:recommended', 'google', 'prettier'), {
},

rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-var-requires': 'off',
semi: 'warn',
'comma-dangle': 'warn',
quotes: ['warn', 'single'],
Expand All @@ -58,5 +56,6 @@ export default [...compat.extends('eslint:recommended', 'google', 'prettier'), {
}],

'new-cap': 'off',
'no-unused-vars': 'off'
},
}];
5,211 changes: 4,382 additions & 829 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
"lint": "npx eslint ."
},
"devDependencies": {
"globals": "^15.9.0",
"@eslint/js": "^9.10.0",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.10.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-node": "^11.1.0"
"globals": "^15.9.0",
"typescript": "^5.6.2"
}
}
4 changes: 2 additions & 2 deletions snippets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions snippets/sms/batches/cancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const batchId = 'the_batch_id_to_cancel';

/** @type {Sms.CancelBatchMessageRequestData} */
const requestData= {
batch_id: batchId,
};

const response = await smsService.batches.cancel(requestData);

console.log(`Response:\n${JSON.stringify(response, null, 2)}`);
};
23 changes: 23 additions & 0 deletions snippets/sms/batches/delivery-feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const batchId = 'the_batch_id';
const recipientPhoneNumber = 'YOUR_recipient_phone_number';

/** @type {Sms.DeliveryFeedbackRequestData} */
const requestData= {
batch_id: batchId,
deliveryFeedbackRequestBody: {
recipients: [
recipientPhoneNumber,
],
},
};

await smsService.batches.sendDeliveryFeedback(requestData);

console.log('The delivery feedback has been sent');
};
26 changes: 26 additions & 0 deletions snippets/sms/batches/dry-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const from = 'YOUR_sinch_phone_number';
const recipient = 'YOUR_recipient_phone_number';
const body = 'This is a test SMS message using the Sinch Node.js SDK.';

/** @type {Sms.DryRunRequestData} */
const requestData= {
dryRunRequestBody: {
type: 'mt_text',
from,
to: [
recipient,
],
body,
},
};

const response = await smsService.batches.dryRun(requestData);

console.log(`Response:\n${JSON.stringify(response, null, 2)}`);
};
17 changes: 17 additions & 0 deletions snippets/sms/batches/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const batchId = 'the_batch_id';

/** @type {Sms.GetBatchMessageRequestData} */
const requestData= {
batch_id: batchId,
};

const response = await smsService.batches.get(requestData);

console.log(`Batch details:\n${JSON.stringify(response, null, 2)}`);
};
23 changes: 23 additions & 0 deletions snippets/sms/batches/list-auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
oneWeekAgo.setHours(0, 0, 0, 0);

/** @type {Sms.ListBatchesRequestData} */
const requestData= {
start_date: oneWeekAgo,
page_size: 2,
};

console.log('List of batches printed one by one:');
// Use the iterator and fetch data from all the pages automatically
for await (const batch of smsService.batches.list(requestData)) {
console.log(JSON.stringify(batch, null, 2));
}

};
30 changes: 30 additions & 0 deletions snippets/sms/batches/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
oneWeekAgo.setHours(0, 0, 0, 0);

/** @type {Sms.ListBatchesRequestData} */
const requestData= {
start_date: oneWeekAgo,
page_size: 2,
};

/** @type {Sms.SendSMSResponse[]} */
const batchesList = [];
// Fetch a page of Batches
let response = await smsService.batches.list(requestData);
// Fetch the data page by page manually
while (response.hasNextPage) {
batchesList.push(...response.data);
response = await response.nextPage();
}
batchesList.push(...response.data);

console.log(`Full list of batches printed at once (length = ${batchesList.length}):\n${JSON.stringify(batchesList, null, 2)}`);

};
29 changes: 29 additions & 0 deletions snippets/sms/batches/replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService, textToHex } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const from = 'YOUR_sinch_phone_number';
const recipient = 'YOUR_recipient_phone_number';
const batchId = 'the_batch_id_scheduled_in_the_future';

/** @type {Sms.ReplaceBatchMessageRequestData} */
const requestData= {
batch_id: batchId,
replaceBatchMessageRequestBody: {
type: 'mt_binary',
from,
to: [
recipient,
],
udh: textToHex('UserDataHeader'),
body: btoa('This is a replaced message'),
client_reference: 'Sinch Node.js SDK snippet',
},
};

const response = await smsService.batches.replace(requestData);

console.log(`Replaced batch:\n${JSON.stringify(response, null, 2)}`);
};
27 changes: 27 additions & 0 deletions snippets/sms/batches/send-binary-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService, textToHex } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const from = 'YOUR_sinch_phone_number';
const recipient = 'YOUR_recipient_phone_number';

/** @type {Sms.SendBinarySMSRequestData} */
const requestData= {
sendSMSRequestBody: {
type: 'mt_binary',
from,
to: [
recipient,
],
body: 'SGVsbG8sIHRoaXMgaXMgYSBTTVMgZnJvbSBTaW5jaA==',
udh: textToHex('UserDataHeader'),
delivery_report: 'full',
},
};

const response = await smsService.batches.sendBinaryMessage(requestData);

console.log(`Response for binary batch:\n${JSON.stringify(response, null, 2)}`);
};
31 changes: 31 additions & 0 deletions snippets/sms/batches/send-media-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const from = 'YOUR_sinch_phone_number';
const recipient = 'YOUR_recipient_phone_number';

/** @type {Sms.SendMediaSMSRequestData} */
const requestData= {
sendSMSRequestBody: {
type: 'mt_media',
from,
to: [
recipient,
],
body: {
url: 'https://media.body.url',
message: 'Text message coming along with the media file',
},
delivery_report: 'full',
strict_validation: true,
},
};

const response = await smsService.batches.sendMediaMessage(requestData);

console.log(`Response for MMS batch:\n${JSON.stringify(response, null, 2)}`);
};

31 changes: 31 additions & 0 deletions snippets/sms/batches/send-text-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const from = 'YOUR_sinch_phone_number';
const recipient = 'YOUR_recipient_phone_number';

/** @type {Sms.SendTextSMSRequestData} */
const requestData= {
sendSMSRequestBody: {
type: 'mt_text',
from,
to: [
recipient,
],
body: 'Hi ${name}!',
parameters: {
name: {
[recipient]: 'John',
default: 'there',
},
},
},
};

const response = await smsService.batches.sendTextMessage(requestData);

console.log(`Response for SMS batch:\n${JSON.stringify(response, null, 2)}`);
};
24 changes: 24 additions & 0 deletions snippets/sms/batches/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const batchId = 'the_batch_id_scheduled_in_the_future';
const from = 'YOUR_sinch_phone_number';

/** @type {Sms.UpdateBatchMessageRequestData} */
const requestData= {
batch_id: batchId,
updateBatchMessageRequestBody: {
type: 'mt_text',
from,
body: 'Hi ${name}! This is an updated message',
delivery_report: 'per_recipient',
},
};

const response = await smsService.batches.update(requestData);

console.log(`Response:\n${JSON.stringify(response, null, 2)}`);
};
17 changes: 17 additions & 0 deletions snippets/sms/delivery-reports/get-by-batch-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const batchId = 'the_batch_id';

/** @type {Sms.GetDeliveryReportByBatchIdRequestData} */
const requestData= {
batch_id: batchId,
};

const response = await smsService.deliveryReports.get(requestData);

console.log(`Delivery report:\n${JSON.stringify(response, null, 2)}`);
};
19 changes: 19 additions & 0 deletions snippets/sms/delivery-reports/get-for-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// eslint-disable-next-line no-unused-vars
import { Sms, SmsService } from '@sinch/sdk-core';

/** @param {SmsService} smsService */
export const execute = async (smsService) => {

const batchId = 'the_batch_id';
const recipient = 'YOUR_recipient_phone_number';

/** @type {Sms.GetDeliveryReportByPhoneNumberRequestData} */
const requestData= {
batch_id: batchId,
recipient_msisdn: recipient,
};

const response = await smsService.deliveryReports.getForNumber(requestData);

console.log(`Recipient delivery report:\n${JSON.stringify(response, null, 2)}`);
};
Loading

0 comments on commit 104f676

Please sign in to comment.