Skip to content

Commit 584166c

Browse files
authored
Merge pull request #34 from RocketChat/incoming-messages
[NEW] add a new action to send message as a bot user
2 parents b5a4ec7 + 652a83b commit 584166c

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ The app provides API to trigger specific actions. The URL for the API can be fou
7777
2. **Handover**<br/>
7878
To perform a handover
7979
- REST API Documentation for this endpoint can be found [here](./docs/api-endpoints/perform-handover.md)
80+
3. **Send-Message**<br/>
81+
To send a message as a bot to Visitor
82+
- REST API Documentation for this endpoint can be found [here](./docs/api-endpoints/send-message.md)
8083
2. Fulfillment API/Endpoint
8184

8285
The fulfillment endpoint will enable the app to handle asynchronous messages. More information on it [here](./docs/api-endpoints/fulfillment-endpoint.md)

docs/api-endpoints/send-message.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
**Send Message**
2+
----
3+
Action to send Message to Visitor. The sender for this message will be the Bot.
4+
5+
* **URL**
6+
7+
REST API URL can be found on Apps Page <br />
8+
Sample Url for eg: <br /> `http://localhost:3000/api/apps/public/783d8e4d-b06a-409a-aaf3-b37650dc0a26/incoming`
9+
10+
* **Method:**
11+
12+
`POST`
13+
14+
* **Input Data Format**
15+
16+
`JSON`
17+
18+
* **Data Params**
19+
20+
**Required:**
21+
22+
1. `action` = `send-message` <br/>
23+
24+
2. `sessionId=[string]`
25+
> Note. Session Id is the same session of Dialogflow
26+
27+
3. ```bash
28+
actionData: {
29+
`messages=[Array<string | QuickReplies>]`
30+
}
31+
```
32+
> Note: `messages` is an array of simple string messages and QuickReplies.
33+
34+
> Format for quick-replies is almost similar to [this](../QuickReplies.md). Following is the format.
35+
```bash
36+
{
37+
"text": string,
38+
"options": [
39+
string
40+
]
41+
}
42+
```
43+
44+
* **Success Response:**
45+
46+
* **Code:** 200 <br />
47+
**Content:** `{ result: "Your request was processed successfully" }`
48+
49+
* **Error Response:**
50+
51+
* **Code:** 400 BAD REQUEST <br />
52+
**Content:** <br/>
53+
`{
54+
error: "Error: Session Id not present in request"
55+
}`
56+
57+
OR
58+
59+
* **Code:** 500 Internal Server Error <br />
60+
**Content:** <br />
61+
`{ error : "Error occurred while processing perform-handover. Details:- [Error Details]" }`
62+
63+
* **Sample Call:**
64+
65+
**Curl**
66+
```bash
67+
curl "http://localhost:3000/api/apps/public/21b7d3ba-031b-41d9-8ff2-fbbfa081ae90/incoming" \
68+
-X POST \
69+
-d "{\n \"action\": \"send-message\",\n \"sessionId\": \"6uppPYfLa3rnDF6Fe\",\n \"actionData\": {\n \"messages\": [\n \"hello\",\n {\n \"text\": \"Do you want to continue?\",\n \"options\": [\n \"YES\",\n \"NO\"\n ]\n }\n ]\n }\n}" \
70+
-H "Content-Type: application/json"
71+
```
72+
**HTTP**
73+
74+
```HTTP
75+
POST /api/apps/public/21b7d3ba-031b-41d9-8ff2-fbbfa081ae90/incoming HTTP/1.1
76+
Host: localhost:3000
77+
Content-Type: application/json
78+
79+
{
80+
"action": "send-message",
81+
"sessionId": "6uppPYfLa3rnDF6Fe",
82+
"actionData": {
83+
"messages": [
84+
"hello",
85+
{
86+
"text": "Do you want to continue?",
87+
"options": [
88+
"YES",
89+
"NO"
90+
]
91+
}
92+
]
93+
}
94+
}
95+
```

endpoints/IncomingEndpoint.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EndpointActionNames, IActionsEndpointContent } from '../enum/Endpoints'
55
import { Headers, Response } from '../enum/Http';
66
import { Logs } from '../enum/Logs';
77
import { createHttpResponse } from '../lib/Http';
8+
import { createDialogflowMessage } from '../lib/Message';
89
import { closeChat, performHandover } from '../lib/Room';
910

1011
export class IncomingEndpoint extends ApiEndpoint {
@@ -42,6 +43,11 @@ export class IncomingEndpoint extends ApiEndpoint {
4243
const { visitor: { token: visitorToken } } = room;
4344
await performHandover(modify, read, sessionId, visitorToken, targetDepartment);
4445
break;
46+
case EndpointActionNames.SEND_MESSAGE:
47+
const { actionData: { messages = null } = {} } = endpointContent;
48+
if (!messages) { throw new Error(Logs.INVALID_MESSAGES); }
49+
await createDialogflowMessage(sessionId, read, modify, { messages, isFallback: false });
50+
break;
4551
default:
4652
throw new Error(Logs.INVALID_ENDPOINT_ACTION);
4753
}

enum/Endpoints.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import { IDialogflowQuickReplies } from './Dialogflow';
2+
13
export interface IActionsEndpointContent {
24
action: EndpointActionNames;
35
sessionId: string;
46
actionData?: {
57
targetDepartment?: string;
8+
messages: Array<string | IDialogflowQuickReplies>;
69
};
710
}
811

912
export enum EndpointActionNames {
1013
CLOSE_CHAT = 'close-chat',
1114
HANDOVER = 'handover',
15+
SEND_MESSAGE = 'send-message',
1216
}

enum/Logs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export enum Logs {
66
INVALID_BOT_USERNAME_SETTING = 'The Bot User does not exist.',
77
INVALID_VISITOR_TOKEN = 'Error: Visitor Token not valid',
88
INVALID_DEPARTMENT_NAME = 'Error: Department Name is not valid',
9+
INVALID_MESSAGES = 'Error! Messages data not present or in wrong format. Kindly refer the api documentation.',
910
ENDPOINT_REQUEST_PROCESSING_ERROR = 'Error occurred while processing the request. Details:- ',
1011
INVALID_ENDPOINT_ACTION = 'Error!! Invalid Action type',
1112
EMPTY_CLIENT_EMAIL_OR_PRIVATE_KEY_SETTING = 'Client Email or Private Key Field cannot be empty',

0 commit comments

Comments
 (0)