@builderbot/contexts-dialogflow 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 ADDED
@@ -0,0 +1,11 @@
1
+ # `contexts-ts`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const contextsTs = require('contexts-ts');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
@@ -0,0 +1,26 @@
1
+ import { CoreClass } from '@builderbot/bot';
2
+
3
+ import { DialogFlowContextOptions, MessageContextIncoming } from '../types';
4
+ export declare class DialogFlowContext extends CoreClass {
5
+ optionsDX: DialogFlowContextOptions;
6
+ projectId: string | null;
7
+ sessionClient: any;
8
+ googleKeyJson: string | undefined;
9
+ constructor(_database: any, _provider: any, _optionsDX?: {});
10
+ /**
11
+ * Verificar conexión con servicio de DialogFlow
12
+ */
13
+ init: () => void;
14
+ /**
15
+ * GLOSSARY.md
16
+ * @param {*} messageCtxInComming
17
+ * @returns
18
+ */
19
+ handleMsg: (messageCtxInComming: MessageContextIncoming) => Promise<any>;
20
+ private existsCredential;
21
+ private getCredential;
22
+ private initializeSessionClient;
23
+ private createSession;
24
+ private detectIntent;
25
+ }
26
+ //# sourceMappingURL=dialogflow.class.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dialogflow.class.d.ts","sourceRoot":"","sources":["../../src/dialogflow/dialogflow.class.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAK3C,OAAO,EAAc,wBAAwB,EAAW,sBAAsB,EAAE,MAAM,UAAU,CAAA;AAIhG,qBAAa,iBAAkB,SAAQ,SAAS;IAC5C,SAAS,EAAE,wBAAwB,CAElC;IACD,SAAS,EAAE,MAAM,GAAG,IAAI,CAAO;IAC/B,aAAa,MAAO;IACpB,aAAa,EAAE,MAAM,GAAG,SAAS,CAA8B;gBACnD,SAAS,KAAA,EAAE,SAAS,KAAA,EAAE,UAAU,KAAK;IAMjD;;OAEG;IACH,IAAI,aAgBH;IAED;;;;OAIG;IACH,SAAS,wBAA+B,sBAAsB,KAAG,QAAQ,GAAG,CAAC,CA0D5E;IAED,OAAO,CAAC,gBAAgB;IAIxB,OAAO,CAAC,aAAa;IAMrB,OAAO,CAAC,uBAAuB;IAI/B,OAAO,CAAC,aAAa;YAIP,YAAY;CAI7B"}
@@ -0,0 +1,10 @@
1
+ import { DialogFlowContext } from './dialogflow.class';
2
+ import { ParamsDialogFlow } from '../types';
3
+ /**
4
+ * Crear instancia de clase Bot
5
+ * @param {*} args
6
+ * @returns
7
+ */
8
+ declare const createBotDialog: ({ database, provider, options }: ParamsDialogFlow) => Promise<DialogFlowContext>;
9
+ export { createBotDialog };
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dialogflow/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAE3C;;;;GAIG;AACH,QAAA,MAAM,eAAe,oCAA2C,gBAAgB,+BAC1B,CAAA;AAEtD,OAAO,EAAE,eAAe,EAAE,CAAA"}
package/dist/index.cjs ADDED
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+
3
+ var bot = require('@builderbot/bot');
4
+ var dialogflow = require('@google-cloud/dialogflow');
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 DialogFlowContext extends bot.CoreClass {
16
+ constructor(_database, _provider, _optionsDX = {}) {
17
+ super(null, _database, _provider, null);
18
+ this.optionsDX = {
19
+ language: 'es',
20
+ };
21
+ this.projectId = null;
22
+ this.sessionClient = null;
23
+ this.googleKeyJson = process.env.GOOGLE_KEY_JSON;
24
+ /**
25
+ * Verificar conexión con servicio de DialogFlow
26
+ */
27
+ this.init = () => {
28
+ if (!this.existsCredential()) {
29
+ throw new Error(`No se encontró ${GOOGLE_ACCOUNT_PATH}`);
30
+ }
31
+ const { project_id, private_key, client_email } = this.getCredential();
32
+ this.projectId = project_id;
33
+ const configuration = {
34
+ credentials: {
35
+ private_key,
36
+ client_email,
37
+ },
38
+ };
39
+ this.initializeSessionClient(configuration);
40
+ };
41
+ /**
42
+ * GLOSSARY.md
43
+ * @param {*} messageCtxInComming
44
+ * @returns
45
+ */
46
+ this.handleMsg = async (messageCtxInComming) => {
47
+ const languageCode = this.optionsDX.language;
48
+ const { from, body } = messageCtxInComming;
49
+ let customPayload = {};
50
+ /**
51
+ * 📄 Creamos session de contexto basado en el numero de la persona
52
+ * para evitar este problema.
53
+ * https://github.com/codigoencasa/bot-whatsapp/pull/140
54
+ */
55
+ const session = this.createSession(from);
56
+ const reqDialog = {
57
+ session,
58
+ queryInput: {
59
+ text: {
60
+ text: body,
61
+ languageCode,
62
+ },
63
+ },
64
+ };
65
+ const { queryResult } = await this.detectIntent(reqDialog);
66
+ const msgPayload = queryResult?.fulfillmentMessages?.find((a) => a.message === Message.PAYLOAD);
67
+ // Revisamos si el dialogFlow tiene multimedia
68
+ if (msgPayload && msgPayload?.payload) {
69
+ const { fields } = msgPayload.payload;
70
+ const mapButtons = fields?.buttons?.listValue?.values.map((m) => {
71
+ return { body: m?.structValue?.fields?.body?.stringValue };
72
+ });
73
+ customPayload = {
74
+ options: {
75
+ media: fields?.media?.stringValue,
76
+ buttons: mapButtons,
77
+ },
78
+ };
79
+ const ctxFromDX = {
80
+ ...customPayload,
81
+ answer: fields?.answer?.stringValue,
82
+ };
83
+ this.sendFlowSimple([ctxFromDX], from);
84
+ return;
85
+ }
86
+ const messagesFromCX = queryResult['fulfillmentMessages']
87
+ .map((a) => {
88
+ if (a.message === Message.TEXT) {
89
+ return { answer: a.text.text[0] };
90
+ }
91
+ })
92
+ .filter((e) => e);
93
+ this.sendFlowSimple(messagesFromCX, from);
94
+ };
95
+ this.optionsDX = { ...this.optionsDX, ..._optionsDX };
96
+ this.init();
97
+ }
98
+ existsCredential() {
99
+ return fs.existsSync(GOOGLE_ACCOUNT_PATH);
100
+ }
101
+ getCredential() {
102
+ const rawJson = fs.readFileSync(GOOGLE_ACCOUNT_PATH, 'utf-8');
103
+ const { project_id, private_key, client_email } = JSON.parse(rawJson);
104
+ return { project_id, private_key, client_email };
105
+ }
106
+ initializeSessionClient(configuration) {
107
+ this.sessionClient = new dialogflow.SessionsClient({ ...configuration });
108
+ }
109
+ createSession(from) {
110
+ return this.sessionClient.projectAgentSessionPath(this.projectId, from);
111
+ }
112
+ async detectIntent(reqDialog) {
113
+ const [single] = (await this.sessionClient.detectIntent(reqDialog)) || [null];
114
+ return single;
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Crear instancia de clase Bot
120
+ * @param {*} args
121
+ * @returns
122
+ */
123
+ const createBotDialog = async ({ database, provider, options }) => new DialogFlowContext(database, provider, options);
124
+
125
+ exports.createBotDialog = createBotDialog;
@@ -0,0 +1,2 @@
1
+ export { createBotDialog } from './dialogflow';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA"}
@@ -0,0 +1,12 @@
1
+ import { MockContext } from './mock.class';
2
+ /**
3
+ * Crear instancia de clase Bot
4
+ * @param {*} args
5
+ * @returns
6
+ */
7
+ declare const createBotMock: ({ database, provider }: {
8
+ database: any;
9
+ provider: any;
10
+ }) => Promise<MockContext>;
11
+ export { createBotMock, MockContext };
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mock/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE1C;;;;GAIG;AACH,QAAA,MAAM,aAAa;;;0BAAwE,CAAA;AAE3F,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,CAAA"}
@@ -0,0 +1,12 @@
1
+ import { CoreClass } from '@builderbot/bot';
2
+ export declare class MockContext extends CoreClass {
3
+ constructor(_database: any, _provider: any);
4
+ init: () => void;
5
+ /**
6
+ * GLOSSARY.md
7
+ * @param {*} messageCtxInComming
8
+ * @returns
9
+ */
10
+ handleMsg: () => Promise<any>;
11
+ }
12
+ //# sourceMappingURL=mock.class.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.class.d.ts","sourceRoot":"","sources":["../../src/mock/mock.class.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,qBAAa,WAAY,SAAQ,SAAS;gBAC1B,SAAS,KAAA,EAAE,SAAS,KAAA;IAIhC,IAAI,aAAY;IAEhB;;;;OAIG;IACH,SAAS,QAAa,QAAQ,GAAG,CAAC,CAEjC;CACJ"}
@@ -0,0 +1,23 @@
1
+ export interface DialogFlowContextOptions {
2
+ language?: string;
3
+ }
4
+ export interface MessageContextIncoming {
5
+ from: string;
6
+ ref?: string;
7
+ body?: string;
8
+ }
9
+ export declare enum Message {
10
+ PAYLOAD = "payload",
11
+ TEXT = "text"
12
+ }
13
+ export interface ParamsDialogFlow {
14
+ database: any;
15
+ provider: any;
16
+ options?: DialogFlowContextOptions;
17
+ }
18
+ export interface Credential {
19
+ project_id: string;
20
+ private_key: string;
21
+ client_email: string;
22
+ }
23
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACrC,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,oBAAY,OAAO;IACf,OAAO,YAAY;IACnB,IAAI,SAAS;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC7B,QAAQ,EAAE,GAAG,CAAA;IACb,QAAQ,EAAE,GAAG,CAAA;IACb,OAAO,CAAC,EAAE,wBAAwB,CAAA;CACrC;AAED,MAAM,WAAW,UAAU;IACvB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;CACvB"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@builderbot/contexts-dialogflow",
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": "^6.2.0"
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
+ }