Skip to content

Commit c555a9d

Browse files
authored
Merge branch 'master' into incoming-event
2 parents fd09801 + 584166c commit c555a9d

5 files changed

Lines changed: 109 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ The app provides API to trigger specific actions. The URL for the API can be fou
8080
3. **Trigger Event**<br/>
8181
To trigger an event on Dialogflow
8282
- REST API Documentation for this endpoint can be found [here](./docs/api-endpoints/trigger-event.md)
83+
4. **Send-Message**<br/>
84+
To send a message as a bot to Visitor
85+
- REST API Documentation for this endpoint can be found [here](./docs/api-endpoints/send-message.md)
8386
2. Fulfillment API/Endpoint
8487

8588
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ export class IncomingEndpoint extends ApiEndpoint {
4949
const { actionData: { event = null } = {} } = endpointContent;
5050
if (!event) { throw new Error(Logs.INVALID_EVENT_DATA); }
5151

52-
let response: IDialogflowMessage;
5352
try {
54-
response = await Dialogflow.sendEvent(http, read, modify, sessionId, event);
53+
const response: IDialogflowMessage = await Dialogflow.sendEvent(http, read, modify, sessionId, event);
54+
await createDialogflowMessage(sessionId, read, modify, response);
5555
} catch (error) {
5656
this.app.getLogger().error(`${Logs.DIALOGFLOW_REST_API_ERROR} ${error.message}`);
5757
throw new Error(`${Logs.DIALOGFLOW_REST_API_ERROR} ${error.message}`);
5858
}
59-
60-
await createDialogflowMessage(sessionId, read, modify, response);
59+
break;
60+
case EndpointActionNames.SEND_MESSAGE:
61+
const { actionData: { messages = null } = {} } = endpointContent;
62+
if (!messages) { throw new Error(Logs.INVALID_MESSAGES); }
63+
await createDialogflowMessage(sessionId, read, modify, { messages, isFallback: false });
6164
break;
6265
default:
6366
throw new Error(Logs.INVALID_ENDPOINT_ACTION);

enum/Endpoints.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { IDialogflowEvent } from './Dialogflow';
1+
import { IDialogflowEvent, IDialogflowQuickReplies } from './Dialogflow';
22

33
export interface IActionsEndpointContent {
44
action: EndpointActionNames;
55
sessionId: string;
66
actionData?: {
77
targetDepartment?: string;
88
event?: IDialogflowEvent;
9+
messages: Array<string | IDialogflowQuickReplies>;
910
};
1011
}
1112

1213
export enum EndpointActionNames {
1314
CLOSE_CHAT = 'close-chat',
1415
HANDOVER = 'handover',
1516
TRIGGER_EVENT = 'trigger-event',
17+
SEND_MESSAGE = 'send-message',
1618
}

enum/Logs.ts

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

0 commit comments

Comments
 (0)