Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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`)"
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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();
});
};
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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}`);
});
Original file line number Diff line number Diff line change
@@ -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`)"
20 changes: 20 additions & 0 deletions docs/getting-started/conversation/send-text-message/README.md
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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"
}
}
14 changes: 14 additions & 0 deletions docs/getting-started/conversation/send-text-message/src/app.js
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions docs/getting-started/numbers/rent-and-configure/README.md
Original file line number Diff line number Diff line change
@@ -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`
14 changes: 14 additions & 0 deletions docs/getting-started/numbers/rent-and-configure/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions docs/getting-started/numbers/rent-and-configure/src/app.js
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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();
Loading
Loading