diff --git a/docs/getting-started/conversation/respond-to-incoming-message/.env.example b/docs/getting-started/conversation/respond-to-incoming-message/.env.example new file mode 100644 index 00000000..de583fd6 --- /dev/null +++ b/docs/getting-started/conversation/respond-to-incoming-message/.env.example @@ -0,0 +1,12 @@ +# Express related configuration +PORT=3001 + +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" + +# The region where is configured your Conversation App ID. +SINCH_CONVERSATION_REGION="The conversation region (e.g.: `us` or `eu`)" diff --git a/docs/getting-started/conversation/respond-to-incoming-message/README.md b/docs/getting-started/conversation/respond-to-incoming-message/README.md new file mode 100644 index 00000000..9ca4938e --- /dev/null +++ b/docs/getting-started/conversation/respond-to-incoming-message/README.md @@ -0,0 +1,26 @@ +# Sinch Getting Started: Receive and respond to incoming messages using Conversation API (Node.js) + +This code is related to [Receive a message with the Node.js SDK](https://developers.sinch.com/docs/conversation/getting-started#5-handle-incoming-messages). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Express server and the controller. + +### Server port + +- `PORT`: the port to be used to listen to incoming requests. Default is `3001` if not set. + +### API credentials to send a message using the Conversation API +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + - `SINCH_CONVERSATION_REGION`= The region where is configured your Conversation App ID (e.g. `us`, `eu`) + +## Usage — Starting the server + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own parameters (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/server.js` diff --git a/docs/getting-started/conversation/respond-to-incoming-message/package.json b/docs/getting-started/conversation/respond-to-incoming-message/package.json new file mode 100644 index 00000000..4860c0d7 --- /dev/null +++ b/docs/getting-started/conversation/respond-to-incoming-message/package.json @@ -0,0 +1,15 @@ +{ + "name": "@sinch/getting-started-conversation_respond-to-incoming-message", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/server.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5", + "express": "^4.20.0" + } +} diff --git a/docs/getting-started/conversation/respond-to-incoming-message/src/conversation/controller.js b/docs/getting-started/conversation/respond-to-incoming-message/src/conversation/controller.js new file mode 100644 index 00000000..7bd6669e --- /dev/null +++ b/docs/getting-started/conversation/respond-to-incoming-message/src/conversation/controller.js @@ -0,0 +1,22 @@ +import { ConversationCallbackWebhooks } from '@sinch/sdk-core'; +import { handleConversationEvent } from './serverBusinessLogic.js'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +export const conversationController = (app, sinchClientParameters) => { + + app.post('/ConversationEvent', async (req, res) => { + try { + const event = ConversationCallbackWebhooks.parseEvent(req.body); + if (event.trigger === 'MESSAGE_INBOUND') { + await handleConversationEvent(event, sinchClientParameters); + } else { + res.status(200).json({ message: `Unexpected event type for this tutorial: ${event.trigger}` }); + } + } catch (error) { + console.error('Error parsing event:', error); + return res.status(400).json({ error: 'Invalid event format' }); + } + res.status(200).json(); + }); +}; diff --git a/docs/getting-started/conversation/respond-to-incoming-message/src/conversation/serverBusinessLogic.js b/docs/getting-started/conversation/respond-to-incoming-message/src/conversation/serverBusinessLogic.js new file mode 100644 index 00000000..86145658 --- /dev/null +++ b/docs/getting-started/conversation/respond-to-incoming-message/src/conversation/serverBusinessLogic.js @@ -0,0 +1,37 @@ +import { SinchClient } from '@sinch/sdk-core'; + +/** + * Handles the incoming Conversation event by echoing what has been received to the sender. + * @param { MessageInboundEvent } messageInboundEvent - The incoming Conversation message event object + * @param sinchClientParameters - the Conversation service instance from the Sinch SDK containing the API methods + */ +export const handleConversationEvent = async (messageInboundEvent, sinchClientParameters) => { + console.log(`Handling event: ${JSON.stringify(messageInboundEvent, null, 2)}`); + + /** @type {Conversation.SendTextMessageRequestData} */ + const sendMessageRequest = { + sendMessageRequestBody: { + app_id: messageInboundEvent.app_id, + message: { + text_message: { + text: `You sent: ${messageInboundEvent.message.contact_message.text_message.text}`, + }, + }, + recipient: { + identified_by: { + channel_identities: [ + { + channel: 'SMS', + identity: messageInboundEvent.message.channel_identity.identity, + }, + ], + }, + }, + } + }; + + console.log(`Replying with: ${JSON.stringify(sendMessageRequest, null, 2)}`); + + const sinchClient = new SinchClient(sinchClientParameters); + await sinchClient.conversation.messages.sendTextMessage(sendMessageRequest); +}; diff --git a/docs/getting-started/conversation/respond-to-incoming-message/src/server.js b/docs/getting-started/conversation/respond-to-incoming-message/src/server.js new file mode 100644 index 00000000..f0477f2e --- /dev/null +++ b/docs/getting-started/conversation/respond-to-incoming-message/src/server.js @@ -0,0 +1,23 @@ +import express from 'express'; +import { conversationController } from './conversation/controller.js'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +const app = express(); +const port = process.env.PORT || 3001; + +/** @type {import('@sinch/sdk-core').SinchClientParameters} */ +const sinchClientParameters = { + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, + conversationRegion: process.env.SINCH_CONVERSATION_REGION, +}; + +app.use(express.json()); + +conversationController(app, sinchClientParameters); + +app.listen(port, () => { + console.log(`Server is listening on port ${port}`); +}); diff --git a/docs/getting-started/conversation/send-text-message/.env.example b/docs/getting-started/conversation/send-text-message/.env.example new file mode 100644 index 00000000..0aa0015d --- /dev/null +++ b/docs/getting-started/conversation/send-text-message/.env.example @@ -0,0 +1,9 @@ +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" + +# The region where is configured your Conversation App ID. +SINCH_CONVERSATION_REGION="The conversation region (e.g.: `us` or `eu`)" diff --git a/docs/getting-started/conversation/send-text-message/README.md b/docs/getting-started/conversation/send-text-message/README.md new file mode 100644 index 00000000..1e1af587 --- /dev/null +++ b/docs/getting-started/conversation/send-text-message/README.md @@ -0,0 +1,20 @@ +# Sinch Getting started + +This code is related to [Send an Text Message with Node.js SDK](https://developers.sinch.com/docs/conversation/getting-started#4-send-the-message). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + - `SINCH_CONVERSATION_REGION`= The region where is configured your Conversation App ID (e.g. `us`, `eu`) + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/conversation/send-text-message/package.json b/docs/getting-started/conversation/send-text-message/package.json new file mode 100644 index 00000000..44b4de63 --- /dev/null +++ b/docs/getting-started/conversation/send-text-message/package.json @@ -0,0 +1,14 @@ +{ + "name": "@sinch/getting-started-conversation_send-text-message", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5" + } +} diff --git a/docs/getting-started/conversation/send-text-message/src/app.js b/docs/getting-started/conversation/send-text-message/src/app.js new file mode 100644 index 00000000..ceec4aa4 --- /dev/null +++ b/docs/getting-started/conversation/send-text-message/src/app.js @@ -0,0 +1,14 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { ConversationSample } from './conversation/conversationSample.js'; +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const sinchClient = new SinchClient({ + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, + conversationRegion: process.env.SINCH_CONVERSATION_REGION, +}); + +new ConversationSample(sinchClient.conversation).start(); diff --git a/docs/getting-started/conversation/send-text-message/src/conversation/conversationSample.js b/docs/getting-started/conversation/send-text-message/src/conversation/conversationSample.js new file mode 100644 index 00000000..0829f63e --- /dev/null +++ b/docs/getting-started/conversation/send-text-message/src/conversation/conversationSample.js @@ -0,0 +1,45 @@ +/** + * Class to send a demo SMS message through the Conversation API using the Sinch Node.js SDK. + */ +export class ConversationSample { + /** + * @param { ConversationService } conversationService - the ConversationService instance from the Sinch SDK containing the API methods. + */ + constructor(conversationService) { + this.conversationService = conversationService; + } + + async start() { + const appId = 'CONVERSATION_APPLICATION_ID'; + const from = 'SINCH_VIRTUAL_PHONE_NUMBER'; + const to = 'RECIPIENT_PHONE_NUMBER'; + + const body= 'This is a test Conversation message sent using the Sinch Node.js SDK.'; + + const response = await this.conversationService.messages.sendTextMessage({ + sendMessageRequestBody: { + app_id: appId, + message: { + text_message: { + text: body, + }, + }, + recipient: { + identified_by: { + channel_identities: [ + { + channel: 'SMS', + identity: to, + }, + ], + }, + }, + channel_properties: { + SMS_SENDER: from, + }, + }, + }); + + console.log('Response:', response); + } +} diff --git a/docs/getting-started/numbers/rent-and-configure/.env.example b/docs/getting-started/numbers/rent-and-configure/.env.example new file mode 100644 index 00000000..ea2a1e24 --- /dev/null +++ b/docs/getting-started/numbers/rent-and-configure/.env.example @@ -0,0 +1,6 @@ +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" diff --git a/docs/getting-started/numbers/rent-and-configure/README.md b/docs/getting-started/numbers/rent-and-configure/README.md new file mode 100644 index 00000000..53fa1fa8 --- /dev/null +++ b/docs/getting-started/numbers/rent-and-configure/README.md @@ -0,0 +1,19 @@ +# Sinch Getting started: Rent and Configure a Virtual Number (Node.js) + +This code is related to [Rent and configure your virtual number using the Node.js SDK](https://developers.sinch.com/docs/numbers/getting-started). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/numbers/rent-and-configure/package.json b/docs/getting-started/numbers/rent-and-configure/package.json new file mode 100644 index 00000000..9a73a54a --- /dev/null +++ b/docs/getting-started/numbers/rent-and-configure/package.json @@ -0,0 +1,14 @@ +{ + "name": "@sinch/getting-started-numbers_rent-and-configure-number", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5" + } +} diff --git a/docs/getting-started/numbers/rent-and-configure/src/app.js b/docs/getting-started/numbers/rent-and-configure/src/app.js new file mode 100644 index 00000000..ac4394a1 --- /dev/null +++ b/docs/getting-started/numbers/rent-and-configure/src/app.js @@ -0,0 +1,13 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { NumbersSample } from './numbers/numbersSample.js'; +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const sinchClient = new SinchClient({ + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, +}); + +new NumbersSample(sinchClient.numbers).start(); diff --git a/docs/getting-started/numbers/rent-and-configure/src/numbers/numbersSample.js b/docs/getting-started/numbers/rent-and-configure/src/numbers/numbersSample.js new file mode 100644 index 00000000..623d29e1 --- /dev/null +++ b/docs/getting-started/numbers/rent-and-configure/src/numbers/numbersSample.js @@ -0,0 +1,29 @@ +/** + * Class to rent a specific Sinch Virtual Number through the Numbers API using the Sinch Node.js SDK. + */ +export class NumbersSample { + /** + * @param { NumbersService } numbersService - the NumbersService instance from the Sinch SDK containing the API methods. + */ + constructor(numbersService) { + this.numbersService = numbersService; + } + + async start() { + // Available numbers list can be retrieved by using searchForAvailableNumbers() function from Numbers service, see: + // https://developers.sinch.com/docs/numbers/getting-started + const phoneNumberToBeRented = 'AVAILABLE_PHONE_NUMBER_TO_BE_RENTED'; + const servicePlanIdToAssociateWithTheNumber = 'MY_SERVICE_PLAN_ID'; + + const response = await this.numbersService.rent({ + phoneNumber: phoneNumberToBeRented, + rentNumberRequestBody: { + smsConfiguration: { + servicePlanId: servicePlanIdToAssociateWithTheNumber, + }, + }, + }); + + console.log(`Successfully rented number: ${response.phoneNumber}`); + } +} diff --git a/docs/getting-started/numbers/rent-first-available-number/.env.example b/docs/getting-started/numbers/rent-first-available-number/.env.example new file mode 100644 index 00000000..ea2a1e24 --- /dev/null +++ b/docs/getting-started/numbers/rent-first-available-number/.env.example @@ -0,0 +1,6 @@ +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" diff --git a/docs/getting-started/numbers/rent-first-available-number/README.md b/docs/getting-started/numbers/rent-first-available-number/README.md new file mode 100644 index 00000000..7e4e15d2 --- /dev/null +++ b/docs/getting-started/numbers/rent-first-available-number/README.md @@ -0,0 +1,19 @@ +# Sinch Getting started: Rent First Available Number (Node.js) + +This code is related to [Rent the first available number using the Node.js SDK](https://developers.sinch.com/docs/numbers/getting-started). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/numbers/rent-first-available-number/package.json b/docs/getting-started/numbers/rent-first-available-number/package.json new file mode 100644 index 00000000..b941fd8a --- /dev/null +++ b/docs/getting-started/numbers/rent-first-available-number/package.json @@ -0,0 +1,14 @@ +{ + "name": "@sinch/getting-started-numbers_rent-first-available-number", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5" + } +} diff --git a/docs/getting-started/numbers/rent-first-available-number/src/app.js b/docs/getting-started/numbers/rent-first-available-number/src/app.js new file mode 100644 index 00000000..ac4394a1 --- /dev/null +++ b/docs/getting-started/numbers/rent-first-available-number/src/app.js @@ -0,0 +1,13 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { NumbersSample } from './numbers/numbersSample.js'; +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const sinchClient = new SinchClient({ + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, +}); + +new NumbersSample(sinchClient.numbers).start(); diff --git a/docs/getting-started/numbers/rent-first-available-number/src/numbers/numbersSample.js b/docs/getting-started/numbers/rent-first-available-number/src/numbers/numbersSample.js new file mode 100644 index 00000000..95e67e93 --- /dev/null +++ b/docs/getting-started/numbers/rent-first-available-number/src/numbers/numbersSample.js @@ -0,0 +1,28 @@ +/** + * Class to rent any Sinch Virtual Number through the Numbers API using the Sinch Node.js SDK. + */ +export class NumbersSample { + /** + * @param { NumbersService } numbersService - the NumbersService instance from the Sinch SDK containing the API methods. + */ + constructor(numbersService) { + this.numbersService = numbersService; + } + + async start() { + + const servicePlanIdToAssociateWithTheNumber = 'MY_SERVICE_PLAN_ID'; + + const response = await this.numbersService.rentAny({ + rentAnyNumberRequestBody: { + regionCode: 'US', + type: 'LOCAL', + smsConfiguration: { + servicePlanId: servicePlanIdToAssociateWithTheNumber, + }, + }, + }); + + console.log('Response:', response); + } +} diff --git a/docs/getting-started/numbers/search-available-number/.env.example b/docs/getting-started/numbers/search-available-number/.env.example new file mode 100644 index 00000000..ea2a1e24 --- /dev/null +++ b/docs/getting-started/numbers/search-available-number/.env.example @@ -0,0 +1,6 @@ +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" diff --git a/docs/getting-started/numbers/search-available-number/README.md b/docs/getting-started/numbers/search-available-number/README.md new file mode 100644 index 00000000..3974e2ba --- /dev/null +++ b/docs/getting-started/numbers/search-available-number/README.md @@ -0,0 +1,19 @@ +# Sinch Getting started: Search for Virtual Numbers (Node.js) + +This code is related to [Search for virtual numbers using the Node.js SDK](https://developers.sinch.com/docs/numbers/getting-started). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/numbers/search-available-number/package.json b/docs/getting-started/numbers/search-available-number/package.json new file mode 100644 index 00000000..507a86e9 --- /dev/null +++ b/docs/getting-started/numbers/search-available-number/package.json @@ -0,0 +1,14 @@ +{ + "name": "@sinch/getting-started-numbers_search-available-number", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5" + } +} diff --git a/docs/getting-started/numbers/search-available-number/src/app.js b/docs/getting-started/numbers/search-available-number/src/app.js new file mode 100644 index 00000000..ac4394a1 --- /dev/null +++ b/docs/getting-started/numbers/search-available-number/src/app.js @@ -0,0 +1,13 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { NumbersSample } from './numbers/numbersSample.js'; +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const sinchClient = new SinchClient({ + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, +}); + +new NumbersSample(sinchClient.numbers).start(); diff --git a/docs/getting-started/numbers/search-available-number/src/numbers/numbersSample.js b/docs/getting-started/numbers/search-available-number/src/numbers/numbersSample.js new file mode 100644 index 00000000..2014ece5 --- /dev/null +++ b/docs/getting-started/numbers/search-available-number/src/numbers/numbersSample.js @@ -0,0 +1,21 @@ +/** + * Class to list some Sinch Virtual Numbers through the Conversation API using the Sinch Node.js SDK. + */ +export class NumbersSample { + /** + * @param { NumbersService } numbersService - the NumbersService instance from the Sinch SDK containing the API methods. + */ + constructor(numbersService) { + this.numbersService = numbersService; + } + + async start() { + + const response = await this.numbersService.searchForAvailableNumbers({ + regionCode: 'US', + type: 'LOCAL', + }); + + console.log('Response:', response.availableNumbers); + } +} diff --git a/docs/getting-started/sms/respond-to-incoming-message/.env.example b/docs/getting-started/sms/respond-to-incoming-message/.env.example new file mode 100644 index 00000000..221a65d4 --- /dev/null +++ b/docs/getting-started/sms/respond-to-incoming-message/.env.example @@ -0,0 +1,17 @@ +# Express related configuration +PORT=3001 + +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" + +# The region where is configured your SMS service plan. +SINCH_SMS_REGION="The SMS region (e.g.: `us` or `eu`)" + +# SMS Service Plan ID related credentials +# if set, these credentials will be used and enable to use other regionsthan only US/EU +#SINCH_SERVICE_PLAN_ID= +#SINCH_API_TOKEN= diff --git a/docs/getting-started/sms/respond-to-incoming-message/README.md b/docs/getting-started/sms/respond-to-incoming-message/README.md new file mode 100644 index 00000000..eb3b5353 --- /dev/null +++ b/docs/getting-started/sms/respond-to-incoming-message/README.md @@ -0,0 +1,27 @@ +# Sinch Getting started + +This code is related to [Receive an SMS Message with Node.js SDK](https://developers.sinch.com/docs/sms/getting-started/node-sdk/handle-incoming/). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the parameters that will be used to configure the Express server and the controller. + +### Server port + +- `PORT`: the port to be used to listen to incoming requests. Default is `3001` if not set. + +### API credentials to send an SMS + +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + - `SINCH_SMS_REGION`= The SMS region of your Service plan (e.g. `us`, `eu`) + +## Usage — Starting the server + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own parameters (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/server.js` diff --git a/docs/getting-started/sms/respond-to-incoming-message/package.json b/docs/getting-started/sms/respond-to-incoming-message/package.json new file mode 100644 index 00000000..d28acfba --- /dev/null +++ b/docs/getting-started/sms/respond-to-incoming-message/package.json @@ -0,0 +1,15 @@ +{ + "name": "@sinch/getting-started_respond-to-incoming-message", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/server.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5", + "express": "^4.20.0" + } +} diff --git a/docs/getting-started/sms/respond-to-incoming-message/src/server.js b/docs/getting-started/sms/respond-to-incoming-message/src/server.js new file mode 100644 index 00000000..d5f9a6fc --- /dev/null +++ b/docs/getting-started/sms/respond-to-incoming-message/src/server.js @@ -0,0 +1,25 @@ +import express from 'express'; +import { smsController } from './sms/controller.js'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +const app = express(); +const port = process.env.PORT || 3001; + +/** @type {import('@sinch/sdk-core').SinchClientParameters} */ +const sinchClientParameters = { + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, + servicePlanId: process.env.SINCH_SERVICE_PLAN_ID, + apiToken: process.env.SINCH_API_TOKEN, + smsRegion: process.env.SINCH_SMS_REGION, +}; + +app.use(express.json()); + +smsController(app, sinchClientParameters); + +app.listen(port, () => { + console.log(`Server is listening on port ${port}`); +}); diff --git a/docs/getting-started/sms/respond-to-incoming-message/src/sms/controller.js b/docs/getting-started/sms/respond-to-incoming-message/src/sms/controller.js new file mode 100644 index 00000000..c02087cf --- /dev/null +++ b/docs/getting-started/sms/respond-to-incoming-message/src/sms/controller.js @@ -0,0 +1,22 @@ +import { SmsCallbackWebhooks } from '@sinch/sdk-core'; +import { handleSmsEvent } from './serverBusinessLogic.js'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +export const smsController = (app, sinchClientParameters) => { + + app.post('/SmsEvent', async (req, res) => { + try { + const event = SmsCallbackWebhooks.parseEvent(req.body); + if (event.type === 'mo_text') { + await handleSmsEvent(event, sinchClientParameters); + } else { + res.status(200).json({ message: `Unexpected event type for this tutorial: ${event.type}` }); + } + } catch (error) { + console.error('Error parsing event:', error); + return res.status(400).json({ error: 'Invalid event format' }); + } + res.status(200).json(); + }); +}; diff --git a/docs/getting-started/sms/respond-to-incoming-message/src/sms/serverBusinessLogic.js b/docs/getting-started/sms/respond-to-incoming-message/src/sms/serverBusinessLogic.js new file mode 100644 index 00000000..0afa9373 --- /dev/null +++ b/docs/getting-started/sms/respond-to-incoming-message/src/sms/serverBusinessLogic.js @@ -0,0 +1,25 @@ +// eslint-disable-next-line no-unused-vars +import { SinchClient, Sms } from '@sinch/sdk-core'; + +/** + * Handles the incoming SMS event by echoing what has been received to the sender. + * @param { Sms.MOText } incomingTextMessage - The incoming SMS message event object + * @param { SinchClientParameters } sinchClientParameters - the SMS service instance from the Sinch SDK containing the API methods + */ +export const handleSmsEvent = async (incomingTextMessage, sinchClientParameters) => { + console.log(`Handling event: ${JSON.stringify(incomingTextMessage, null, 2)}`); + + /** @type {Sms.SendSMSRequestData} */ + const smsRequest = { + sendSMSRequestBody: { + to: [incomingTextMessage.from], + body: `You sent: ${incomingTextMessage.body}`, + from: incomingTextMessage.to, + }, + }; + + console.log(`Replying with: ${JSON.stringify(smsRequest, null, 2)}`); + + const sinchClient = new SinchClient(sinchClientParameters); + await sinchClient.sms.batches.send(smsRequest); +}; diff --git a/docs/getting-started/sms/send-sms-message/.env.example b/docs/getting-started/sms/send-sms-message/.env.example new file mode 100644 index 00000000..10538f4a --- /dev/null +++ b/docs/getting-started/sms/send-sms-message/.env.example @@ -0,0 +1,9 @@ +# The project ID where are defined the resources you want to use. +SINCH_PROJECT_ID="Your Sinch Project ID" + +# The Access Key ID and secret to authenticate your requests to the Sinch API. +SINCH_KEY_ID="Your Sinch Key ID" +SINCH_KEY_SECRET="Your Sinch Key Secret" + +# The region where is configured your SMS service plan. +SINCH_SMS_REGION="The SMS region (e.g.: `us` or `eu`)" diff --git a/docs/getting-started/sms/send-sms-message/README.md b/docs/getting-started/sms/send-sms-message/README.md new file mode 100644 index 00000000..ebeb23a0 --- /dev/null +++ b/docs/getting-started/sms/send-sms-message/README.md @@ -0,0 +1,20 @@ +# Sinch Getting started + +This code is related to [Send an SMS Message with Node.js SDK](https://developers.sinch.com/docs/sms/getting-started/node-sdk/send-message). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your Sinch account: + - `SINCH_PROJECT_ID`= Your Sinch Project ID + - `SINCH_KEY_ID`= Your Sinch Access Key ID + - `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key + - `SINCH_SMS_REGION`= The SMS region of your Service plan (e.g. `us`, `eu`) + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/sms/send-sms-message/package.json b/docs/getting-started/sms/send-sms-message/package.json new file mode 100644 index 00000000..eb4e63f4 --- /dev/null +++ b/docs/getting-started/sms/send-sms-message/package.json @@ -0,0 +1,14 @@ +{ + "name": "@sinch/getting-started-sms_send-sms-message", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5" + } +} diff --git a/docs/getting-started/sms/send-sms-message/src/app.js b/docs/getting-started/sms/send-sms-message/src/app.js new file mode 100644 index 00000000..9fe90a77 --- /dev/null +++ b/docs/getting-started/sms/send-sms-message/src/app.js @@ -0,0 +1,14 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { SmsSample } from './sms/smsSample.js'; +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const sinchClient = new SinchClient({ + projectId: process.env.SINCH_PROJECT_ID, + keyId: process.env.SINCH_KEY_ID, + keySecret: process.env.SINCH_KEY_SECRET, + smsRegion: process.env.SINCH_SMS_REGION, +}); + +new SmsSample(sinchClient.sms).start(); diff --git a/docs/getting-started/sms/send-sms-message/src/sms/smsSample.js b/docs/getting-started/sms/send-sms-message/src/sms/smsSample.js new file mode 100644 index 00000000..5af80608 --- /dev/null +++ b/docs/getting-started/sms/send-sms-message/src/sms/smsSample.js @@ -0,0 +1,27 @@ +/** + * Class to send a demo SMS message using the Sinch Node.js SDK. + */ +export class SmsSample { + /** + * @param { SmsService } smsService - the SmsService instance from the Sinch SDK containing the API methods. + */ + constructor(smsService) { + this.smsService = smsService; + } + + async start() { + const sender = 'SENDER_NUMBER'; + const recipient = 'RECIPIENT_PHONE_NUMBER'; + const body = 'This is a test SMS message using the Sinch Node.js SDK.'; + + const response = await this.smsService.batches.sendTextMessage({ + sendSMSRequestBody: { + from: sender, + to: [recipient], + body, + }, + }); + + console.log('Batch ID:', response.id); + } +} diff --git a/docs/getting-started/verification/user-verification-using-sms-pin/.env.example b/docs/getting-started/verification/user-verification-using-sms-pin/.env.example new file mode 100644 index 00000000..fbecf4d3 --- /dev/null +++ b/docs/getting-started/verification/user-verification-using-sms-pin/.env.example @@ -0,0 +1,3 @@ +# The Verification Application ID and secret to authenticate your requests to the Sinch API. +SINCH_APPLICATION_KEY="Your Sinch Voice Application Key" +SINCH_APPLICATION_SECRET="Your Sinch Voice Application Secret" diff --git a/docs/getting-started/verification/user-verification-using-sms-pin/README.md b/docs/getting-started/verification/user-verification-using-sms-pin/README.md new file mode 100644 index 00000000..a013acc1 --- /dev/null +++ b/docs/getting-started/verification/user-verification-using-sms-pin/README.md @@ -0,0 +1,18 @@ +# Sinch Getting started: User Verification Using SMS PIN (Node.js) + +Code is related to [Verify a user using SMS PIN with the Node.js SDK](https://developers.sinch.com/docs/verification/getting-started/node-sdk/sms-verification/). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your [Sinch account](https://dashboard.sinch.com/verification/apps): + - `SINCH_APPLICATION_KEY`= Your Sinch Voice Application Key + - `SINCH_APPLICATION_SECRET`= Your Sinch Application Secret + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/verification/user-verification-using-sms-pin/package.json b/docs/getting-started/verification/user-verification-using-sms-pin/package.json new file mode 100644 index 00000000..76a85591 --- /dev/null +++ b/docs/getting-started/verification/user-verification-using-sms-pin/package.json @@ -0,0 +1,15 @@ +{ + "name": "@sinch/getting-started_user-verification-using-sms-pin", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5", + "inquirer": "^9.2.14" + } +} diff --git a/docs/getting-started/verification/user-verification-using-sms-pin/src/app.js b/docs/getting-started/verification/user-verification-using-sms-pin/src/app.js new file mode 100644 index 00000000..5b2f2b22 --- /dev/null +++ b/docs/getting-started/verification/user-verification-using-sms-pin/src/app.js @@ -0,0 +1,11 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { VerificationSample } from './verification/verificationSample.js'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +const sinchClient = new SinchClient({ + applicationKey: process.env.SINCH_APPLICATION_KEY, + applicationSecret: process.env.SINCH_APPLICATION_SECRET, +}); + +new VerificationSample(sinchClient.verification).start(); diff --git a/docs/getting-started/verification/user-verification-using-sms-pin/src/verification/verificationSample.js b/docs/getting-started/verification/user-verification-using-sms-pin/src/verification/verificationSample.js new file mode 100644 index 00000000..af7e4f6b --- /dev/null +++ b/docs/getting-started/verification/user-verification-using-sms-pin/src/verification/verificationSample.js @@ -0,0 +1,118 @@ +// eslint-disable-next-line no-unused-vars +import { Verification, VerificationService, VerificationsApi } from '@sinch/sdk-core'; +import inquirer from 'inquirer'; + +/** + * Class to handle a phone number verification using SMS. + */ +export class VerificationSample { + /** + * @param { VerificationService } verificationService - the VerificationService instance from the Sinch SDK containing the API methods. + */ + constructor(verificationService) { + this.verificationService = verificationService; + } + + /** + * Starts the verification process by prompting the user for a phone number, + * sending a verification request, asking for the verification code, and reporting it. + * @return {Promise} + */ + async start() { + // Step 1: Ask the phone number to verify + const e164Number = await this.promptPhoneNumber(); + + try { + // Step 2: Start the phone number verification + const verificationId = await this.startSmsVerification(this.verificationService.verifications, e164Number); + + // Step 3: Ask the user for the received verification code + const code = await this.promptSmsCode(); + + // Step 4: Report the verification code and complete the process + await this.reportSmsVerification(this.verificationService.verifications, code, verificationId); + + console.log('Verification successfully completed.'); + } catch (error) { + console.error('An error occurred during the verification process:', error); + } + } + + /** + * Prompts the user to enter their phone number. + * @return {Promise} The phone number entered by the user. + */ + async promptPhoneNumber() { + const userInput = await inquirer.prompt([ + { + type: 'input', + name: 'phoneNumber', + message: 'Enter the phone number you want to verify (E.164 format):', + validate: (input) => input ? true : 'Phone number cannot be empty.', + }, + ]); + + return userInput.phoneNumber; + } + + /** + * Sends a request to start SMS verification for a phone number. + * @param {VerificationsApi} verificationStarter - The VerificationsApi instance. + * @param {string} phoneNumber - The phone number to verify. + * @return {Promise} The verification ID if the request is successful. + */ + async startSmsVerification(verificationStarter, phoneNumber) { + console.log(`Sending a verification request to ${phoneNumber}`); + + const requestData = Verification.startVerificationHelper.buildSmsRequest(phoneNumber); + + try { + const response = await verificationStarter.startSms(requestData); + + if (!response.id) { + throw new Error('Verification ID is undefined.'); + } + + console.log(`Verification started successfully. Verification ID: ${response.id}`); + return response.id; + } catch (error) { + console.error('Failed to start SMS verification:', error); + throw error; + } + } + + /** + * Prompts the user to enter the verification code they received. + * @return {Promise} The verification code entered by the user. + */ + async promptSmsCode() { + const answers = await inquirer.prompt([ + { + type: 'input', + name: 'code', + message: 'Enter the verification code you received:', + validate: (input) => input ? true : 'Verification code cannot be empty.', + }, + ]); + return answers.code; + } + + /** + * Sends a request to report the verification code for a specific verification ID. + * @param { VerificationsApi } verificationReporter - The VerificationsApi instance. + * @param {string} code - The verification code to report. + * @param {string} id - The verification ID corresponding to the process. + * @return {Promise} + */ + async reportSmsVerification(verificationReporter, code, id) { + const requestData = Verification.reportVerificationByIdHelper.buildSmsRequest(id, code); + + try { + const response = await verificationReporter.reportSmsById(requestData); + console.log(`Verification reported successfully. Response status: ${response.status}`); + } catch (error) { + console.error('Failed to report SMS verification:', error); + throw error; + } + } +} diff --git a/docs/getting-started/voice/make-a-call/.env.example b/docs/getting-started/voice/make-a-call/.env.example new file mode 100644 index 00000000..f49a0871 --- /dev/null +++ b/docs/getting-started/voice/make-a-call/.env.example @@ -0,0 +1,3 @@ +# The Voice Application ID and secret to authenticate your requests to the Sinch API. +SINCH_APPLICATION_KEY="Your Sinch Voice Application Key" +SINCH_APPLICATION_SECRET="Your Sinch Voice Application Secret" diff --git a/docs/getting-started/voice/make-a-call/README.md b/docs/getting-started/voice/make-a-call/README.md new file mode 100644 index 00000000..75ff9984 --- /dev/null +++ b/docs/getting-started/voice/make-a-call/README.md @@ -0,0 +1,18 @@ +# Sinch Getting started: Make a Call (Node.js) + +This code is related to [Make a call with the Node.js SDK](https://developers.sinch.com/docs/voice/getting-started/node-sdk/make-call). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Sinch Client. +- You need to fill the following variables with the values from your [Sinch account](https://dashboard.sinch.com/voice/apps): + - `SINCH_APPLICATION_KEY`= Your Sinch Voice Application Key + - `SINCH_APPLICATION_SECRET`= Your Sinch Application Secret + +## Usage + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own credentials (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/app.js` diff --git a/docs/getting-started/voice/make-a-call/package.json b/docs/getting-started/voice/make-a-call/package.json new file mode 100644 index 00000000..fc329939 --- /dev/null +++ b/docs/getting-started/voice/make-a-call/package.json @@ -0,0 +1,14 @@ +{ + "name": "@sinch/getting-started-voice_make-a-call", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/app.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5" + } +} diff --git a/docs/getting-started/voice/make-a-call/src/app.js b/docs/getting-started/voice/make-a-call/src/app.js new file mode 100644 index 00000000..5a9fadec --- /dev/null +++ b/docs/getting-started/voice/make-a-call/src/app.js @@ -0,0 +1,12 @@ +import { SinchClient } from '@sinch/sdk-core'; +import { VoiceSample } from './voice/voiceSample.js'; +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const sinchClient = new SinchClient({ + applicationKey: process.env.SINCH_APPLICATION_KEY, + applicationSecret: process.env.SINCH_APPLICATION_SECRET, +}); + +new VoiceSample(sinchClient.voice).start(); diff --git a/docs/getting-started/voice/make-a-call/src/voice/voiceSample.js b/docs/getting-started/voice/make-a-call/src/voice/voiceSample.js new file mode 100644 index 00000000..06504cf8 --- /dev/null +++ b/docs/getting-started/voice/make-a-call/src/voice/voiceSample.js @@ -0,0 +1,32 @@ +/** + * Class to place a demo Voice call using the Sinch Node.js SDK. + */ +export class VoiceSample { + /** + * @param { VoiceService } voiceService - the VoiceService instance from the Sinch SDK containing the API methods. + */ + constructor(voiceService) { + this.voiceService = voiceService; + } + + async start() { + const caller = 'CALLER_NUMBER'; + const recipient = 'RECIPIENT_PHONE_NUMBER'; + + const response = await this.voiceService.callouts.tts({ + ttsCalloutRequestBody: { + method: 'ttsCallout', + ttsCallout: { + destination: { + type: 'number', + endpoint: recipient, + }, + cli: caller, + text: 'Hello, this is a call from Sinch. Congratulations! You just made your first call.', + }, + }, + }); + + console.log('Call ID:', response.callId); + } +} diff --git a/docs/getting-started/voice/respond-to-incoming-call/.env.example b/docs/getting-started/voice/respond-to-incoming-call/.env.example new file mode 100644 index 00000000..bfc76271 --- /dev/null +++ b/docs/getting-started/voice/respond-to-incoming-call/.env.example @@ -0,0 +1,2 @@ +# Express related configuration +PORT=3001 diff --git a/docs/getting-started/voice/respond-to-incoming-call/README.md b/docs/getting-started/voice/respond-to-incoming-call/README.md new file mode 100644 index 00000000..5420c7f9 --- /dev/null +++ b/docs/getting-started/voice/respond-to-incoming-call/README.md @@ -0,0 +1,19 @@ +# Sinch Getting started: Respond to an incoming call with the Node.js SDK + +Code is related to [Handle an incoming call with the Node.js SDK](https://developers.sinch.com/docs/voice/getting-started/node-sdk/incoming-call/). + +## Configuration + +Rename the [.env.example](.env.example) file into `.env` and edit it to set the credentials that will be used to configure the Express server and the controller. + +### Server port + +- `PORT`: the port to be used to listen to incoming requests. Default is `3001` if not set. + +## Usage — Starting the server + +1. Install the dependencies by running the command `npm install`. +2. Edit the `.env` file with your own parameters (see the paragraph above for details). +3. Run the code with one of the following commands: +- `npm start` +- `node src/server.js` diff --git a/docs/getting-started/voice/respond-to-incoming-call/package.json b/docs/getting-started/voice/respond-to-incoming-call/package.json new file mode 100644 index 00000000..1b24b19d --- /dev/null +++ b/docs/getting-started/voice/respond-to-incoming-call/package.json @@ -0,0 +1,15 @@ +{ + "name": "@sinch/getting-started_respond-to-incoming-call", + "version": "0.0.0", + "author": "Sinch", + "description": "", + "type": "module", + "scripts": { + "start": "node src/server.js" + }, + "dependencies": { + "@sinch/sdk-core": "^1.3.0", + "dotenv": "^16.4.5", + "express": "^4.20.0" + } +} diff --git a/docs/getting-started/voice/respond-to-incoming-call/src/server.js b/docs/getting-started/voice/respond-to-incoming-call/src/server.js new file mode 100644 index 00000000..1dfa399c --- /dev/null +++ b/docs/getting-started/voice/respond-to-incoming-call/src/server.js @@ -0,0 +1,15 @@ +import express from 'express'; +import { voiceController } from './voice/controller.js'; +import * as dotenv from 'dotenv'; +dotenv.config(); + +const app = express(); +const port = process.env.PORT || 3001; + +app.use(express.json()); + +voiceController(app); + +app.listen(port, () => { + console.log(`Server is listening on port ${port}`); +}); diff --git a/docs/getting-started/voice/respond-to-incoming-call/src/voice/controller.js b/docs/getting-started/voice/respond-to-incoming-call/src/voice/controller.js new file mode 100644 index 00000000..952b2eaa --- /dev/null +++ b/docs/getting-started/voice/respond-to-incoming-call/src/voice/controller.js @@ -0,0 +1,29 @@ +import { VoiceCallbackWebhooks } from '@sinch/sdk-core'; +import { handleDisconnectedCallEvent, handleIncomingCallEvent } from './serverBusinessLogic.js'; + +export const voiceController = (app) => { + + app.post('/VoiceEvent', async (req, res) => { + + let response; + + try { + const event = VoiceCallbackWebhooks.parseEvent(req.body); + switch (event.event) { + case 'ice': + response = handleIncomingCallEvent(event); + break; + case 'dice': + response = handleDisconnectedCallEvent(event); + break; + default: + res.status(200).json({ message: `Unexpected event type for this tutorial: ${event.event}` }); + } + } catch (error) { + console.error('Error parsing event:', error); + return res.status(400).json({ error: 'Invalid event format' }); + } + + return res.status(200).json(response); + }); +}; diff --git a/docs/getting-started/voice/respond-to-incoming-call/src/voice/serverBusinessLogic.js b/docs/getting-started/voice/respond-to-incoming-call/src/voice/serverBusinessLogic.js new file mode 100644 index 00000000..6db6433d --- /dev/null +++ b/docs/getting-started/voice/respond-to-incoming-call/src/voice/serverBusinessLogic.js @@ -0,0 +1,29 @@ +// eslint-disable-next-line no-unused-vars +import { Voice } from '@sinch/sdk-core'; + +/** + * Handles an Incoming Call Event (ICE). + * @param {Voice.IceRequest} iceRequest - The incoming ICE request object. + * @return {Voice.IceResponse} The formatted ICE response to handle the incoming call. + */ +export const handleIncomingCallEvent = (iceRequest) => { + console.log(`Handling 'ICE' event:\n${JSON.stringify(iceRequest, null, 2)}`); + + const instruction = 'Thank you for calling your Sinch number. You have just handled an incoming call.'; + + return new Voice.IceSvamletBuilder() + .setAction(Voice.iceActionHelper.hangup()) + .addInstruction(Voice.iceInstructionHelper.say(instruction)) + .build(); +}; + +/** + * Handles a disconnected call event (DICE). + * @param {Voice.DiceRequest} diceRequest - The incoming DICE request object. + * @return {string} An empty string as a response to the disconnected call event. + */ +export const handleDisconnectedCallEvent = (diceRequest) => { + console.log(`Handling 'DICE' event:\n${JSON.stringify(diceRequest, null, 2)}`); + + return ''; +}; diff --git a/packages/conversation/src/rest/v1/callbacks/callbacks-webhook.ts b/packages/conversation/src/rest/v1/callbacks/callbacks-webhook.ts index fcc34e7c..0526d536 100644 --- a/packages/conversation/src/rest/v1/callbacks/callbacks-webhook.ts +++ b/packages/conversation/src/rest/v1/callbacks/callbacks-webhook.ts @@ -198,4 +198,15 @@ export class ConversationCallbackWebhooks implements CallbackProcessor { Then('the delete message response contains no data', () => { assert.deepEqual(deleteMessageResponse, {} ); }); + +When('I send a request to list the last messages sent to specified channel identities', async () => { + // TODO +}); + +Then('the response contains {string} last messages sent to specified channel identities', (expectedAnswer: string) => { + // TODO + assert.equal(expectedAnswer, expectedAnswer); +}); + +When('I send a request to list all the last messages sent to specified channel identities', async () => { + // TODO +}); + +When('I iterate manually over the last messages sent to specified channel identities pages', async () => { + // TODO +}); + +// eslint-disable-next-line max-len +Then('the response list contains {string} last messages sent to specified channel identities', (expectedAnswer: string) => { + // TODO + assert.equal(expectedAnswer, expectedAnswer); +}); + +// eslint-disable-next-line max-len +Then('the result contains the data from {string} pages of last messages sent to specified channel identities', (expectedAnswer: string) => { + // TODO + assert.equal(expectedAnswer, expectedAnswer); +}); diff --git a/packages/fax/src/rest/v3/callbacks/callbacks-webhook.ts b/packages/fax/src/rest/v3/callbacks/callbacks-webhook.ts index 598821ad..4dbc372a 100644 --- a/packages/fax/src/rest/v3/callbacks/callbacks-webhook.ts +++ b/packages/fax/src/rest/v3/callbacks/callbacks-webhook.ts @@ -64,4 +64,15 @@ export class FaxCallbackWebhooks implements CallbackProcessor{ throw new Error(`Unknown SMS event: ${JSON.stringify(eventBody)}`); }; + /** + * Static reviver for an SMS Event. + * This method ensures the object can be treated as an SMS Event. + * @param {any} eventBody - The event body containing the SMS event notification. + * @return {SmsCallback} - The parsed SMS event object + */ + public static parseEvent(eventBody: any): SmsCallback { + const instance = new SmsCallbackWebhooks(); + return instance.parseEvent(eventBody); + } + } diff --git a/packages/verification/src/rest/v1/callbacks/callbacks-webhook.ts b/packages/verification/src/rest/v1/callbacks/callbacks-webhook.ts index f3277562..560fc77a 100644 --- a/packages/verification/src/rest/v1/callbacks/callbacks-webhook.ts +++ b/packages/verification/src/rest/v1/callbacks/callbacks-webhook.ts @@ -67,4 +67,15 @@ export class VerificationCallbackWebhooks implements CallbackProcessor