Skip to content

Commit d467d90

Browse files
committed
DEVEXP-1218: Move Getting-Started to the SDK repo
1 parent e1689cc commit d467d90

60 files changed

Lines changed: 1671 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Express related configuration
2+
PORT=3001
3+
4+
# The project ID where are defined the resources you want to use.
5+
SINCH_PROJECT_ID="Your Sinch Project ID"
6+
7+
# The Access Key ID and secret to authenticate your requests to the Sinch API.
8+
SINCH_KEY_ID="Your Sinch Key ID"
9+
SINCH_KEY_SECRET="Your Sinch Key Secret"
10+
11+
# The region where is configured your Conversation App ID.
12+
SINCH_CONVERSATION_REGION="The conversation region (e.g.: `us` or `eu`)"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Sinch Getting Started: Receive and respond to incoming messages using Conversation API (Node.js)
2+
3+
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).
4+
5+
## Configuration
6+
7+
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.
8+
9+
### Server port
10+
11+
- `port`: the port to be used to listen to incoming requests. Default is `3001` if not set.
12+
13+
### API credentials to send a message using the Conversation API
14+
- You need to fill the following variables with the values from your Sinch account:
15+
- `SINCH_PROJECT_ID`= Your Sinch Project ID
16+
- `SINCH_KEY_ID`= Your Sinch Access Key ID
17+
- `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key
18+
- `SINCH_CONVERSATION_REGION`= The region where is configured your Conversation App ID (e.g. `us`, `eu`)
19+
20+
## Usage — Starting the server
21+
22+
1. Install the dependencies by running the command `npm install`.
23+
2. Edit the `.env` file with your own parameters (see the paragraph above for details).
24+
3. Run the code with one of the following commands:
25+
- `npm start`
26+
- `node src/server.js`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@sinch/getting-started-conversation_respond-to-incoming-message",
3+
"version": "0.0.0",
4+
"author": "Sinch",
5+
"description": "",
6+
"type": "module",
7+
"scripts": {
8+
"start": "node src/server.js"
9+
},
10+
"dependencies": {
11+
"@sinch/sdk-core": "^1.3.0",
12+
"dotenv": "^16.4.5",
13+
"express": "^4.20.0"
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ConversationCallbackWebhooks } from '@sinch/sdk-core';
2+
import { handleConversationEvent } from './serverBusinessLogic.js';
3+
import * as dotenv from 'dotenv';
4+
dotenv.config();
5+
6+
export const conversationController = (app, sinchClientParameters) => {
7+
8+
app.post('/ConversationEvent', async (req, res) => {
9+
try {
10+
const event = ConversationCallbackWebhooks.parseEvent(req.body);
11+
if (event.trigger === 'MESSAGE_INBOUND') {
12+
await handleConversationEvent(event, sinchClientParameters);
13+
} else {
14+
res.status(200).json({ message: `Unexpected event type for this tutorial: ${event.trigger}` });
15+
}
16+
} catch (error) {
17+
console.error('Error parsing event:', error);
18+
return res.status(400).json({ error: 'Invalid event format' });
19+
}
20+
res.status(200).json();
21+
});
22+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { SinchClient } from '@sinch/sdk-core';
2+
3+
/**
4+
* Handles the incoming Conversation event by echoing what has been received to the sender.
5+
* @param { MessageInboundEvent } messageInboundEvent - The incoming Conversation message event object
6+
* @param sinchClientParameters - the Conversation service instance from the Sinch SDK containing the API methods
7+
*/
8+
export const handleConversationEvent = async (messageInboundEvent, sinchClientParameters) => {
9+
console.log(`Handling event: ${JSON.stringify(messageInboundEvent, null, 2)}`);
10+
11+
/** @type {Conversation.SendTextMessageRequestData} */
12+
const sendMessageRequest = {
13+
sendMessageRequestBody: {
14+
app_id: messageInboundEvent.app_id,
15+
message: {
16+
text_message: {
17+
text: `You sent: ${messageInboundEvent.message.contact_message.text_message.text}`,
18+
},
19+
},
20+
recipient: {
21+
identified_by: {
22+
channel_identities: [
23+
{
24+
channel: 'SMS',
25+
identity: messageInboundEvent.message.channel_identity.identity,
26+
},
27+
],
28+
},
29+
},
30+
}
31+
};
32+
33+
console.log(`Replying with: ${JSON.stringify(sendMessageRequest, null, 2)}`);
34+
35+
const sinchClient = new SinchClient(sinchClientParameters);
36+
await sinchClient.conversation.messages.sendTextMessage(sendMessageRequest);
37+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import express from 'express';
2+
import { conversationController } from './conversation/controller.js';
3+
import * as dotenv from 'dotenv';
4+
dotenv.config();
5+
6+
const app = express();
7+
const port = process.env.PORT || 3001;
8+
9+
/** @type {import('@sinch/sdk-core').SinchClientParameters} */
10+
const sinchClientParameters = {
11+
projectId: process.env.SINCH_PROJECT_ID,
12+
keyId: process.env.SINCH_KEY_ID,
13+
keySecret: process.env.SINCH_KEY_SECRET,
14+
conversationRegion: process.env.SINCH_CONVERSATION_REGION,
15+
};
16+
17+
app.use(express.json());
18+
19+
conversationController(app, sinchClientParameters);
20+
21+
app.listen(port, () => {
22+
console.log(`Server is listening on port ${port}`);
23+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The project ID where are defined the resources you want to use.
2+
SINCH_PROJECT_ID="Your Sinch Project ID"
3+
4+
# The Access Key ID and secret to authenticate your requests to the Sinch API.
5+
SINCH_KEY_ID="Your Sinch Key ID"
6+
SINCH_KEY_SECRET="Your Sinch Key Secret"
7+
8+
# The region where is configured your Conversation App ID.
9+
SINCH_CONVERSATION_REGION="The conversation region (e.g.: `us` or `eu`)"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Sinch Getting started
2+
3+
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).
4+
5+
## Configuration
6+
7+
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.
8+
- You need to fill the following variables with the values from your Sinch account:
9+
- `SINCH_PROJECT_ID`= Your Sinch Project ID
10+
- `SINCH_KEY_ID`= Your Sinch Access Key ID
11+
- `SINCH_KEY_SECRET`= Your Sinch Key Secret associated to your Sinch Access Key
12+
- `SINCH_CONVERSATION_REGION`= The region where is configured your Conversation App ID (e.g. `us`, `eu`)
13+
14+
## Usage
15+
16+
1. Install the dependencies by running the command `npm install`.
17+
2. Edit the `.env` file with your own credentials (see the paragraph above for details).
18+
3. Run the code with one of the following commands:
19+
- `npm start`
20+
- `node src/app.js`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@sinch/getting-started-conversation_send-text-message",
3+
"version": "0.0.0",
4+
"author": "Sinch",
5+
"description": "",
6+
"type": "module",
7+
"scripts": {
8+
"start": "node src/app.js"
9+
},
10+
"dependencies": {
11+
"@sinch/sdk-core": "^1.3.0",
12+
"dotenv": "^16.4.5"
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { SinchClient } from '@sinch/sdk-core';
2+
import { ConversationSample } from './conversation/conversationSample.js';
3+
import * as dotenv from 'dotenv';
4+
5+
dotenv.config();
6+
7+
const sinchClient = new SinchClient({
8+
projectId: process.env.SINCH_PROJECT_ID,
9+
keyId: process.env.SINCH_KEY_ID,
10+
keySecret: process.env.SINCH_KEY_SECRET,
11+
conversationRegion: process.env.SINCH_CONVERSATION_REGION,
12+
});
13+
14+
new ConversationSample(sinchClient.conversation).start();

0 commit comments

Comments
 (0)