@vraksha/bottender-dialogflow 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +80 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +93 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Yoctol (github.com/Yoctol/bottender)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @vraksha/bottender-dialogflow
|
|
2
|
+
|
|
3
|
+
[Dialogflow](https://dialogflow.com/) integration for Bottender.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install it with npm:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @vraksha/bottender-dialogflow
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
or Yarn:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
yarn add @vraksha/bottender-dialogflow
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const { chain } = require('bottender');
|
|
23
|
+
const dialogflow = require('@vraksha/bottender-dialogflow');
|
|
24
|
+
|
|
25
|
+
async function SayHello(context) {
|
|
26
|
+
await context.sendText('Hello!');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function Unknown(context) {
|
|
30
|
+
await context.sendText('Sorry, I don’t know what you say.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Dialogflow = dialogflow({
|
|
34
|
+
projectId: process.env.GOOGLE_APPLICATION_PROJECT_ID,
|
|
35
|
+
actions: {
|
|
36
|
+
greeting: SayHello,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
module.exports = async function App() {
|
|
41
|
+
return chain([
|
|
42
|
+
Dialogflow, //
|
|
43
|
+
Unknown,
|
|
44
|
+
]);
|
|
45
|
+
};
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Reference
|
|
49
|
+
|
|
50
|
+
### projectId
|
|
51
|
+
|
|
52
|
+
The ID of your Dialogflow Project.
|
|
53
|
+
|
|
54
|
+
Type: `string`.
|
|
55
|
+
Required.
|
|
56
|
+
|
|
57
|
+
### languageCode
|
|
58
|
+
|
|
59
|
+
The language of this conversational query. See [Language Support](https://cloud.google.com/dialogflow/docs/reference/language) for a list of the currently supported language codes. Note that queries in the same session do not necessarily need to specify the same language.
|
|
60
|
+
|
|
61
|
+
Type: `string`.
|
|
62
|
+
Required.
|
|
63
|
+
|
|
64
|
+
### actions
|
|
65
|
+
|
|
66
|
+
Actions to be executed when the event matches corresponding intent.
|
|
67
|
+
|
|
68
|
+
Type: `Record<string, Action>`.
|
|
69
|
+
Required.
|
|
70
|
+
|
|
71
|
+
### timeZone
|
|
72
|
+
|
|
73
|
+
The time zone of this conversational query from the time zone database, e.g., America/New_York, Europe/Paris. If not provided, the time zone specified in agent settings is used.
|
|
74
|
+
|
|
75
|
+
Type: `string`.
|
|
76
|
+
Optional.
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT © [Yoctol](https://github.com/Yoctol/bottender)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { protos } from '@google-cloud/dialogflow';
|
|
2
|
+
import { Action, Context } from '@vraksha/bottender';
|
|
3
|
+
declare const _default: ({ projectId, languageCode, actions, timeZone, }: {
|
|
4
|
+
projectId: string;
|
|
5
|
+
languageCode: string;
|
|
6
|
+
actions: Record<string, Action<Context<any, any>, protos.google.cloud.dialogflow.v2.IQueryResult, {}>>;
|
|
7
|
+
timeZone?: string | undefined;
|
|
8
|
+
}) => Action<Context>;
|
|
9
|
+
export = _default;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAsB,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAEjE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAa,MAAM,oBAAoB,CAAC;;eA2DnD,MAAM;kBACH,MAAM;;;MAMlB,OAAO,OAAO,CAAC;AAbnB,kBA4FE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
const dialogflow_1 = __importDefault(require("@google-cloud/dialogflow"));
|
|
15
|
+
const invariant_1 = __importDefault(require("invariant"));
|
|
16
|
+
const bottender_1 = require("@vraksha/bottender");
|
|
17
|
+
function getFulfillments(messages) {
|
|
18
|
+
if (!messages) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
return messages
|
|
22
|
+
.filter((message) => message.platform === 'PLATFORM_UNSPECIFIED')
|
|
23
|
+
.map((message) => message.text)
|
|
24
|
+
.filter((text) => Boolean(text))
|
|
25
|
+
.map((text) => text.text)
|
|
26
|
+
.filter((text) => Boolean(text))
|
|
27
|
+
.map((texts) => {
|
|
28
|
+
const index = Math.floor(Math.random() * texts.length);
|
|
29
|
+
return texts[index];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function getTargetAction(actions, intent) {
|
|
33
|
+
if (intent.name && actions[intent.name]) {
|
|
34
|
+
return actions[intent.name];
|
|
35
|
+
}
|
|
36
|
+
if (intent.displayName && actions[intent.displayName]) {
|
|
37
|
+
return actions[intent.displayName];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
module.exports = function dialogflow({ projectId, languageCode = 'en', actions = {}, timeZone, }) {
|
|
41
|
+
(0, invariant_1.default)(typeof projectId === 'string' && projectId.length > 0, 'dialogflow: `projectId` is a required parameter.');
|
|
42
|
+
const credentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
|
|
43
|
+
(0, invariant_1.default)(typeof credentials === 'string' && credentials.length > 0, 'dialogflow: `GOOGLE_APPLICATION_CREDENTIALS` is required. Please make sure you have filled it correctly in `.env` file.');
|
|
44
|
+
const sessionsClient = new dialogflow_1.default.SessionsClient();
|
|
45
|
+
return function Dialogflow(context, { next }) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
if (!context.event.isText || !context.session) {
|
|
48
|
+
return next;
|
|
49
|
+
}
|
|
50
|
+
const sessionPath = sessionsClient.projectAgentSessionPath(projectId, context.session.id);
|
|
51
|
+
const request = {
|
|
52
|
+
session: sessionPath,
|
|
53
|
+
queryInput: {
|
|
54
|
+
text: {
|
|
55
|
+
text: context.event.text,
|
|
56
|
+
languageCode,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
queryParams: {
|
|
60
|
+
timeZone,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
const [response] = yield sessionsClient.detectIntent(request);
|
|
64
|
+
const queryResult = response.queryResult;
|
|
65
|
+
if (!queryResult) {
|
|
66
|
+
context.setAsNotHandled();
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
const { intent, fulfillmentMessages } = queryResult;
|
|
70
|
+
if (!intent) {
|
|
71
|
+
context.setAsNotHandled();
|
|
72
|
+
return next;
|
|
73
|
+
}
|
|
74
|
+
if (intent.displayName) {
|
|
75
|
+
context.setIntent(intent.displayName);
|
|
76
|
+
}
|
|
77
|
+
context.setAsHandled();
|
|
78
|
+
const TargetAction = getTargetAction(actions, intent);
|
|
79
|
+
if (TargetAction) {
|
|
80
|
+
return (0, bottender_1.withProps)(TargetAction, Object.assign({}, queryResult));
|
|
81
|
+
}
|
|
82
|
+
const fulfillments = getFulfillments(fulfillmentMessages);
|
|
83
|
+
if (fulfillments.length > 0) {
|
|
84
|
+
for (const fulfillment of fulfillments) {
|
|
85
|
+
yield context.sendText(fulfillment);
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
return next;
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,0EAAiE;AACjE,0DAAkC;AAClC,kDAAgE;AAEhE,SAAS,eAAe,CACtB,QAAqE;IAErE,IAAI,CAAC,QAAQ,EAAE;QACb,OAAO,EAAE,CAAC;KACX;IAED,OAAO,QAAQ;SACZ,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,KAAK,sBAAsB,CAAC;SAChE,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;SAC9B,MAAM,CACL,CAAC,IAAI,EAAkE,EAAE,CACvE,OAAO,CAAC,IAAI,CAAC,CAChB;SACA,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC;SACxB,MAAM,CAAC,CAAC,IAAI,EAAoB,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SACjD,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACb,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;QACvD,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC,CAAC,CAAC;AACP,CAAC;AAED,SAAS,eAAe,CACtB,OAGC,EACD,MAAiD;IAEjD,IAAI,MAAM,CAAC,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE;QACvC,OAAO,OAAO,CAAC,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AM,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC;YAEzC,IAAI,CAAC,WAAW,EAAE;gBAChB,OAAO,CAAC,eAAe,EAAE,CAAC;gBAC1B,OAAO,IAAI,CAAC;aACb;YAED,MAAM,EAAE,MAAM,EAAE,mBAAmB,EAAE,GAAG,WAAW,CAAC;YAEpD,IAAI,CAAC,MAAM,EAAE;gBACX,OAAO,CAAC,eAAe,EAAE,CAAC;gBAC1B,OAAO,IAAI,CAAC;aACb;YAED,IAAI,MAAM,CAAC,WAAW,EAAE;gBACtB,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;aACvC;YACD,OAAO,CAAC,YAAY,EAAE,CAAC;YAGvB,MAAM,YAAY,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACtD,IAAI,YAAY,EAAE;gBAChB,OAAO,IAAA,qBAAS,EAAC,YAAY,oBAAO,WAAW,EAAG,CAAC;aACpD;YAGD,MAAM,YAAY,GAAG,eAAe,CAAC,mBAAmB,CAAC,CAAC;YAC1D,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC3B,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;oBAEtC,MAAM,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;iBACrC;gBACD,OAAO;aACR;YAED,OAAO,IAAI,CAAC;QACd,CAAC;KAAA,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vraksha/bottender-dialogflow",
|
|
3
|
+
"version": "1.7.0",
|
|
4
|
+
"description": "Dialogflow integration for Bottender.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://bottender.js.org/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/Yoctol/bottender.git"
|
|
10
|
+
},
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"types": "dist/index.d.ts",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@google-cloud/dialogflow": "^4.4.0",
|
|
18
|
+
"invariant": "^2.2.4"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@vraksha/bottender": ">= 1.2.0-0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@vraksha/bottender": "1.7.0"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"bot",
|
|
28
|
+
"bottender",
|
|
29
|
+
"chatbot",
|
|
30
|
+
"dialogflow",
|
|
31
|
+
"language",
|
|
32
|
+
"messaging",
|
|
33
|
+
"nlu",
|
|
34
|
+
"understanding"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|