@builderbot/contexts-dialogflow-cx 0.1.3-alpha.22
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/README.md +11 -0
- package/dist/dialogflow-cx/dialogflow-cx.class.d.ts +26 -0
- package/dist/dialogflow-cx/dialogflow-cx.class.d.ts.map +1 -0
- package/dist/dialogflow-cx/index.d.ts +10 -0
- package/dist/dialogflow-cx/index.d.ts.map +1 -0
- package/dist/index.cjs +121 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CoreClass } from '@builderbot/bot';
|
|
2
|
+
|
|
3
|
+
import { DialogFlowContextOptions, DialogFlowCredentials, MessageContextIncoming } from '../types';
|
|
4
|
+
export declare class DialogFlowContextCX extends CoreClass {
|
|
5
|
+
projectId: string | null;
|
|
6
|
+
configuration: any;
|
|
7
|
+
sessionClient: any;
|
|
8
|
+
optionsDX: DialogFlowContextOptions;
|
|
9
|
+
constructor(_database: any, _provider: any, _optionsDX?: {});
|
|
10
|
+
loadCredentials: () => DialogFlowCredentials | null;
|
|
11
|
+
private initializeDialogFlowClient;
|
|
12
|
+
/**
|
|
13
|
+
* Verificar conexión con servicio de DialogFlow
|
|
14
|
+
*/
|
|
15
|
+
init: () => void;
|
|
16
|
+
/**
|
|
17
|
+
* GLOSSARY.md
|
|
18
|
+
* @param {*} messageCtxInComming
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
handleMsg: (messageCtxInComming: MessageContextIncoming) => Promise<any>;
|
|
22
|
+
private existsCredential;
|
|
23
|
+
private createSession;
|
|
24
|
+
private detectIntent;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=dialogflow-cx.class.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dialogflow-cx.class.d.ts","sourceRoot":"","sources":["../../src/dialogflow-cx/dialogflow-cx.class.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAK3C,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAW,sBAAsB,EAAE,MAAM,UAAU,CAAA;AAI3G,qBAAa,mBAAoB,SAAQ,SAAS;IAC9C,SAAS,EAAE,MAAM,GAAG,IAAI,CAAO;IAC/B,aAAa,MAAO;IACpB,aAAa,MAAO;IACpB,SAAS,EAAE,wBAAwB,CAIlC;gBAEW,SAAS,KAAA,EAAE,SAAS,KAAA,EAAE,UAAU,KAAK;IAMjD,eAAe,QAAO,qBAAqB,GAAG,IAAI,CAGjD;IAED,OAAO,CAAC,0BAA0B,CAYjC;IAED;;OAEG;IACH,IAAI,aAMH;IAED;;;;OAIG;IACH,SAAS,wBAA+B,sBAAsB,KAAG,QAAQ,GAAG,CAAC,CAgD5E;IAED,OAAO,CAAC,gBAAgB;IAIxB,OAAO,CAAC,aAAa;YAKP,YAAY;CAI7B"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DialogFlowContextCX } from './dialogflow-cx.class';
|
|
2
|
+
import { ParamsDialogFlowCX } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Crear instancia de clase Bot
|
|
5
|
+
* @param {*} args
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
declare const createBotDialog: ({ database, provider, options }: ParamsDialogFlowCX) => Promise<DialogFlowContextCX>;
|
|
9
|
+
export { createBotDialog };
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dialogflow-cx/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAE7C;;;;GAIG;AACH,QAAA,MAAM,eAAe,oCAA2C,kBAAkB,iCAC1B,CAAA;AAExD,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var bot = require('@builderbot/bot');
|
|
4
|
+
var dialogflowCx = require('@google-cloud/dialogflow-cx');
|
|
5
|
+
var fs = require('fs');
|
|
6
|
+
var path = require('path');
|
|
7
|
+
|
|
8
|
+
var Message;
|
|
9
|
+
(function (Message) {
|
|
10
|
+
Message["PAYLOAD"] = "payload";
|
|
11
|
+
Message["TEXT"] = "text";
|
|
12
|
+
})(Message || (Message = {}));
|
|
13
|
+
|
|
14
|
+
const GOOGLE_ACCOUNT_PATH = path.join(process.cwd(), 'google-key.json');
|
|
15
|
+
class DialogFlowContextCX extends bot.CoreClass {
|
|
16
|
+
constructor(_database, _provider, _optionsDX = {}) {
|
|
17
|
+
super(null, _database, _provider, null);
|
|
18
|
+
this.projectId = null;
|
|
19
|
+
this.configuration = null;
|
|
20
|
+
this.sessionClient = null;
|
|
21
|
+
this.optionsDX = {
|
|
22
|
+
language: 'es',
|
|
23
|
+
location: '',
|
|
24
|
+
agentId: '',
|
|
25
|
+
};
|
|
26
|
+
this.loadCredentials = () => {
|
|
27
|
+
const rawJson = fs.readFileSync(GOOGLE_ACCOUNT_PATH, 'utf-8');
|
|
28
|
+
return JSON.parse(rawJson);
|
|
29
|
+
};
|
|
30
|
+
this.initializeDialogFlowClient = (credentials) => {
|
|
31
|
+
const { project_id, private_key, client_email } = credentials;
|
|
32
|
+
this.projectId = project_id;
|
|
33
|
+
const configuration = {
|
|
34
|
+
credentials: {
|
|
35
|
+
private_key,
|
|
36
|
+
client_email,
|
|
37
|
+
},
|
|
38
|
+
apiEndpoint: `${this.optionsDX.location}-dialogflow.googleapis.com`,
|
|
39
|
+
};
|
|
40
|
+
this.sessionClient = new dialogflowCx.SessionsClient({ ...configuration });
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Verificar conexión con servicio de DialogFlow
|
|
44
|
+
*/
|
|
45
|
+
this.init = () => {
|
|
46
|
+
if (!this.existsCredential()) {
|
|
47
|
+
throw new Error(`No se encontró ${GOOGLE_ACCOUNT_PATH}`);
|
|
48
|
+
}
|
|
49
|
+
const credentials = this.loadCredentials();
|
|
50
|
+
this.initializeDialogFlowClient(credentials);
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* GLOSSARY.md
|
|
54
|
+
* @param {*} messageCtxInComming
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
this.handleMsg = async (messageCtxInComming) => {
|
|
58
|
+
const languageCode = this.optionsDX.language;
|
|
59
|
+
const { from, body } = messageCtxInComming;
|
|
60
|
+
/**
|
|
61
|
+
* 📄 Creamos session de contexto basado en el numero de la persona
|
|
62
|
+
* para evitar este problema.
|
|
63
|
+
* https://github.com/codigoencasa/bot-whatsapp/pull/140
|
|
64
|
+
*/
|
|
65
|
+
const session = this.createSession(from);
|
|
66
|
+
const reqDialog = {
|
|
67
|
+
session,
|
|
68
|
+
queryInput: {
|
|
69
|
+
text: {
|
|
70
|
+
text: body,
|
|
71
|
+
},
|
|
72
|
+
languageCode,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
const { queryResult } = await this.detectIntent(reqDialog);
|
|
76
|
+
const listMessages = queryResult?.responseMessages?.map((res) => {
|
|
77
|
+
if (res.message === Message.TEXT) {
|
|
78
|
+
return { answer: res.text.text[0] };
|
|
79
|
+
}
|
|
80
|
+
if (res.message === Message.PAYLOAD) {
|
|
81
|
+
const { media = null, buttons = [], answer = '' } = res.payload.fields;
|
|
82
|
+
const buttonsArray = buttons?.listValue?.values?.map((btnValue) => {
|
|
83
|
+
const { stringValue } = btnValue.structValue.fields.body;
|
|
84
|
+
return { body: stringValue };
|
|
85
|
+
}) || [];
|
|
86
|
+
return {
|
|
87
|
+
answer: answer?.stringValue || '',
|
|
88
|
+
options: {
|
|
89
|
+
media: media?.stringValue,
|
|
90
|
+
buttons: buttonsArray,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return { answer: '' };
|
|
95
|
+
});
|
|
96
|
+
this.sendFlowSimple(listMessages, from);
|
|
97
|
+
};
|
|
98
|
+
this.optionsDX = { ...this.optionsDX, ..._optionsDX };
|
|
99
|
+
this.init();
|
|
100
|
+
}
|
|
101
|
+
existsCredential() {
|
|
102
|
+
return fs.existsSync(GOOGLE_ACCOUNT_PATH);
|
|
103
|
+
}
|
|
104
|
+
createSession(from) {
|
|
105
|
+
const { location, agentId } = this.optionsDX;
|
|
106
|
+
return this.sessionClient.projectLocationAgentSessionPath(this.projectId, location, agentId, from);
|
|
107
|
+
}
|
|
108
|
+
async detectIntent(reqDialog) {
|
|
109
|
+
const [single] = (await this.sessionClient.detectIntent(reqDialog)) || [null];
|
|
110
|
+
return single;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Crear instancia de clase Bot
|
|
116
|
+
* @param {*} args
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
const createBotDialog = async ({ database, provider, options }) => new DialogFlowContextCX(database, provider, options);
|
|
120
|
+
|
|
121
|
+
exports.createBotDialog = createBotDialog;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Button } from '@builderbot/bot/dist/types';
|
|
2
|
+
export interface DialogFlowContextOptions {
|
|
3
|
+
language: string;
|
|
4
|
+
location: string;
|
|
5
|
+
agentId: string;
|
|
6
|
+
}
|
|
7
|
+
export interface DialogFlowCredentials {
|
|
8
|
+
project_id: string;
|
|
9
|
+
private_key: string;
|
|
10
|
+
client_email: string;
|
|
11
|
+
}
|
|
12
|
+
export interface DialogFlowCXContextOptions {
|
|
13
|
+
location: string;
|
|
14
|
+
agentId: string;
|
|
15
|
+
language?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface MessageContextIncoming {
|
|
18
|
+
from: string;
|
|
19
|
+
ref?: string;
|
|
20
|
+
body?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface DialogResponseMessage {
|
|
23
|
+
answer: string;
|
|
24
|
+
options?: {
|
|
25
|
+
media?: string;
|
|
26
|
+
buttons?: Button[];
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export declare enum Message {
|
|
30
|
+
PAYLOAD = "payload",
|
|
31
|
+
TEXT = "text"
|
|
32
|
+
}
|
|
33
|
+
export interface ParamsDialogFlowCX {
|
|
34
|
+
database: any;
|
|
35
|
+
provider: any;
|
|
36
|
+
options: DialogFlowCXContextOptions;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAEnD,MAAM,WAAW,wBAAwB;IACrC,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,qBAAqB;IAClC,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;CACvB;AAED,MAAM,WAAW,0BAA0B;IACvC,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,sBAAsB;IACnC,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,qBAAqB;IAClC,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE;QACN,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;KACrB,CAAA;CACJ;AAED,oBAAY,OAAO;IACf,OAAO,YAAY;IACnB,IAAI,SAAS;CAChB;AAED,MAAM,WAAW,kBAAkB;IAC/B,QAAQ,EAAE,GAAG,CAAA;IACb,QAAQ,EAAE,GAAG,CAAA;IACb,OAAO,EAAE,0BAA0B,CAAA;CACtC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@builderbot/contexts-dialogflow-cx",
|
|
3
|
+
"version": "0.1.3-alpha.22",
|
|
4
|
+
"description": "contexts typescript",
|
|
5
|
+
"author": "Leifer Mendez <leifer33@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/codigoencasa/bot-whatsapp#readme",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "dist/index.cjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rimraf dist && rollup --config",
|
|
13
|
+
"test": " npx uvu -r tsm ./__tests__ .test.ts",
|
|
14
|
+
"test:coverage": "npx c8 npm run test",
|
|
15
|
+
"test:debug": " npx tsm --inspect-brk ../../node_modules/uvu/bin.js ./__tests__ .test.ts"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"./dist/"
|
|
19
|
+
],
|
|
20
|
+
"directories": {
|
|
21
|
+
"src": "src",
|
|
22
|
+
"test": "__tests__"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/codigoencasa/bot-whatsapp.git"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/codigoencasa/bot-whatsapp/issues"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@builderbot/bot": "^0.1.3-alpha.22",
|
|
33
|
+
"@google-cloud/dialogflow-cx": "^4.3.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
37
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
38
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
39
|
+
"@types/node": "^20.11.0",
|
|
40
|
+
"@types/proxyquire": "^1.3.31",
|
|
41
|
+
"@types/sinon": "^17.0.3",
|
|
42
|
+
"proxyquire": "^2.1.3",
|
|
43
|
+
"rimraf": "^3.0.2",
|
|
44
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
45
|
+
"sinon": "^17.0.1"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "e52a9fe13d30d65b650a54b6e116953c949b1158"
|
|
48
|
+
}
|