@dawntech/dwntbots-services 0.0.90 → 0.0.92
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/package.json +14 -9
- package/src/lib/blip/BlipBuilder/BlipBuilderTranspilation.js +4 -2
- package/src/lib/blip/BlipBuilder/BlipBuilderTranspilation.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/Feature.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/InactivityFeature.js +1 -1
- package/src/lib/blip/BlipBuilder/features/InactivityFeature.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/PositionFeature.js +2 -2
- package/src/lib/blip/BlipBuilder/features/PositionFeature.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/SDKFeature.js +12 -9
- package/src/lib/blip/BlipBuilder/features/SDKFeature.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/entrypoint.js +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/entrypoint.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/fallback.d.ts +2 -2
- package/src/lib/blip/BlipBuilder/features/actions/fallback.js +12 -25
- package/src/lib/blip/BlipBuilder/features/actions/fallback.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/global.d.ts +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/global.js +9 -17
- package/src/lib/blip/BlipBuilder/features/actions/global.js.map +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/onboarding.js +1 -1
- package/src/lib/blip/BlipBuilder/features/actions/onboarding.js.map +1 -1
- package/src/lib/blip/BlipBuilder/index.d.ts +3 -3
- package/src/lib/blip/BlipBuilder/index.js +2 -3
- package/src/lib/blip/BlipBuilder/index.js.map +1 -1
- package/src/lib/blip/BlipBuilder/models/_base.js +3 -3
- package/src/lib/blip/BlipBuilder/models/_base.js.map +1 -1
- package/src/lib/blip/BlipBuilder/models/base.js +3 -3
- package/src/lib/blip/BlipBuilder/models/base.js.map +1 -1
- package/src/lib/blip/BlipBuilder/models/navigation.js.map +1 -1
- package/src/lib/blip/BlipBuilder/models/tags.js +1 -1
- package/src/lib/blip/BlipBuilder/models/tags.js.map +1 -1
- package/src/lib/blip/BlipBuilder/utils.d.ts +1 -1
- package/src/lib/blip/BlipBuilder/utils.js.map +1 -1
- package/src/lib/blip/BlipService.d.ts +21 -4
- package/src/lib/blip/BlipService.js +161 -22
- package/src/lib/blip/BlipService.js.map +1 -1
- package/src/lib/blip/BlipTransport.d.ts +1 -0
- package/src/lib/blip/BlipTransport.js +16 -4
- package/src/lib/blip/BlipTransport.js.map +1 -1
- package/src/lib/blip/{BlipBuilder/BlipBotDiff.d.ts → BotDiff/BotDiff.d.ts} +24 -11
- package/src/lib/blip/{BlipBuilder/BlipBotDiff.js → BotDiff/BotDiff.js} +57 -23
- package/src/lib/blip/BotDiff/BotDiff.js.map +1 -0
- package/src/lib/blip/BotDiff/index.d.ts +2 -0
- package/src/lib/blip/BotDiff/index.js +8 -0
- package/src/lib/blip/BotDiff/index.js.map +1 -0
- package/src/lib/blip/{BlipBuilder → BuilderSanitize}/BlipBuilderSanitize.d.ts +1 -0
- package/src/lib/blip/{BlipBuilder → BuilderSanitize}/BlipBuilderSanitize.js +16 -10
- package/src/lib/blip/BuilderSanitize/BlipBuilderSanitize.js.map +1 -0
- package/src/lib/blip/BuilderSanitize/index.d.ts +1 -0
- package/src/lib/blip/BuilderSanitize/index.js +18 -0
- package/src/lib/blip/BuilderSanitize/index.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/BlipBuilder.d.ts +96 -0
- package/src/lib/blip/DwnGridCompiler/BlipBuilder.js +443 -0
- package/src/lib/blip/DwnGridCompiler/BlipBuilder.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/DwnGridCompiler.d.ts +61 -0
- package/src/lib/blip/DwnGridCompiler/DwnGridCompiler.js +255 -0
- package/src/lib/blip/DwnGridCompiler/DwnGridCompiler.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/ExecutionLogger.d.ts +24 -0
- package/src/lib/blip/DwnGridCompiler/ExecutionLogger.js +48 -0
- package/src/lib/blip/DwnGridCompiler/ExecutionLogger.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/CoreFeature.d.ts +65 -0
- package/src/lib/blip/DwnGridCompiler/features/CoreFeature.js +408 -0
- package/src/lib/blip/DwnGridCompiler/features/CoreFeature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/Feature.d.ts +149 -0
- package/src/lib/blip/DwnGridCompiler/features/Feature.js +160 -0
- package/src/lib/blip/DwnGridCompiler/features/Feature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/InactivityFeature.d.ts +13 -0
- package/src/lib/blip/DwnGridCompiler/features/InactivityFeature.js +56 -0
- package/src/lib/blip/DwnGridCompiler/features/InactivityFeature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/LinterFeature.d.ts +38 -0
- package/src/lib/blip/DwnGridCompiler/features/LinterFeature.js +77 -0
- package/src/lib/blip/DwnGridCompiler/features/LinterFeature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/ScriptsFeature.d.ts +14 -0
- package/src/lib/blip/DwnGridCompiler/features/ScriptsFeature.js +46 -0
- package/src/lib/blip/DwnGridCompiler/features/ScriptsFeature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/TagsFeature.d.ts +30 -0
- package/src/lib/blip/DwnGridCompiler/features/TagsFeature.js +221 -0
- package/src/lib/blip/DwnGridCompiler/features/TagsFeature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/TemplatesFeature.d.ts +14 -0
- package/src/lib/blip/DwnGridCompiler/features/TemplatesFeature.js +31 -0
- package/src/lib/blip/DwnGridCompiler/features/TemplatesFeature.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/conditions.d.ts +2 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/conditions.js +45 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/conditions.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/contact.d.ts +5 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/contact.js +112 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/contact.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/content.d.ts +7 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/content.js +183 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/content.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/events.d.ts +21 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/events.js +827 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/events.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/redirect.d.ts +3 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/redirect.js +77 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/redirect.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/scripts.d.ts +14 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/scripts.js +292 -0
- package/src/lib/blip/DwnGridCompiler/features/entities/scripts.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/index.d.ts +8 -0
- package/src/lib/blip/DwnGridCompiler/features/index.js +67 -0
- package/src/lib/blip/DwnGridCompiler/features/index.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/__definitions.d.ts +830 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/__definitions.js +364 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/__definitions.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/__scripts.template.d.ts +14 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/__scripts.template.js +292 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/__scripts.template.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_bindings.d.ts +24 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_bindings.js +25 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_bindings.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_build.d.ts +2 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_build.js +8 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_build.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_types.d.ts +14 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_types.js +3 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/_types.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_error_message.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_error_message.js +46 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_error_message.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_fallback_create.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_fallback_create.js +178 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_fallback_create.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_fallback_process.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_fallback_process.js +225 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_fallback_process.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_config_pt_br.d.ts +19 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_config_pt_br.js +165 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_config_pt_br.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_core.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_core.js +411 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_core.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_dwn.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_dwn.js +1195 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_gactions_dwn.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_onboarding_reset.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_onboarding_reset.js +109 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/grid_onboarding_reset.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_console.d.ts +29 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_console.js +61 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_console.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_datetime.d.ts +5 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_datetime.js +24 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_datetime.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_enforcesate.d.ts +2 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_enforcesate.js +28 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_enforcesate.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_http.d.ts +139 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_http.js +270 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_http.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_kebabcase.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_kebabcase.js +16 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_kebabcase.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_merge.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_merge.js +64 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_merge.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_script.d.ts +2 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_script.js +148 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/libs_script.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_intent.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_intent.js +148 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_intent.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_prepare.d.ts +33 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_prepare.js +165 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_prepare.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_script_basic.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_script_basic.js +31 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_script_basic.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_script_complete.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_script_complete.js +31 -0
- package/src/lib/blip/DwnGridCompiler/features/scripts/template_script_complete.js.map +1 -0
- package/src/lib/blip/DwnGridCompiler/index.d.ts +1 -0
- package/src/lib/blip/DwnGridCompiler/index.js +18 -0
- package/src/lib/blip/DwnGridCompiler/index.js.map +1 -0
- package/src/lib/blip/WhatsAppBlipService.js +0 -2
- package/src/lib/blip/WhatsAppBlipService.js.map +1 -1
- package/src/lib/new-relic/NewRelicIngest.d.ts +17 -4
- package/src/lib/new-relic/NewRelicIngest.js +2 -0
- package/src/lib/new-relic/NewRelicIngest.js.map +1 -1
- package/src/lib/storage/Driver.js +1 -1
- package/src/lib/storage/Driver.js.map +1 -1
- package/src/lib/storage/ScopedStorage.js.map +1 -1
- package/src/lib/whatsapp-flow/index.js.map +1 -1
- package/src/lib/blip/BlipBuilder/BlipBotDiff.js.map +0 -1
- package/src/lib/blip/BlipBuilder/BlipBuilderSanitize.js.map +0 -1
- package/src/lib/blip/BlipBuilder/BlipBuilderTranspile.d.ts +0 -49
- package/src/lib/blip/BlipBuilder/BlipBuilderTranspile.js +0 -317
- package/src/lib/blip/BlipBuilder/BlipBuilderTranspile.js.map +0 -1
|
@@ -0,0 +1,1195 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.dwnRun = void 0;
|
|
13
|
+
const Defs = require("./__definitions");
|
|
14
|
+
const __definitions_1 = require("./__definitions");
|
|
15
|
+
const _bindings_1 = require("./_bindings");
|
|
16
|
+
const libs_enforcesate_1 = require("./libs_enforcesate");
|
|
17
|
+
const _build_1 = require("./_build");
|
|
18
|
+
(0, _build_1._compiler)("MAIN-START");
|
|
19
|
+
function dwnRun(dwnStr, coreStr, configStr, statesStr, errorStr, listsConfigStr, tunnelOriginator, contactSerialized) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
dwnStr = dwnStr || "";
|
|
22
|
+
coreStr = coreStr || "";
|
|
23
|
+
configStr = configStr || "";
|
|
24
|
+
statesStr = statesStr || "";
|
|
25
|
+
errorStr = errorStr || "";
|
|
26
|
+
listsConfigStr = listsConfigStr || "";
|
|
27
|
+
let warnings = [];
|
|
28
|
+
let error = __definitions_1.DwnError.fromDwnError(errorStr);
|
|
29
|
+
let dwn = null;
|
|
30
|
+
let core = null;
|
|
31
|
+
let config = null;
|
|
32
|
+
let contact;
|
|
33
|
+
let states = {};
|
|
34
|
+
let intent = null;
|
|
35
|
+
let goto = undefined;
|
|
36
|
+
let lists = { allow: [], block: [] };
|
|
37
|
+
try {
|
|
38
|
+
try {
|
|
39
|
+
core = JSON.parse(coreStr);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
warnings.push(`[update-dwn] Unexpected core JSON ${String(err)}`);
|
|
43
|
+
throw new Error(`core parsing ${String(err)}`);
|
|
44
|
+
}
|
|
45
|
+
warnings = core.warnings;
|
|
46
|
+
const EXPECTED_ERRORS = [
|
|
47
|
+
__definitions_1.PREDEFINED_ERRORS.RUNTIME_ERROR,
|
|
48
|
+
__definitions_1.PREDEFINED_ERRORS.MISCONFIGURATION_ERROR,
|
|
49
|
+
__definitions_1.PREDEFINED_ERRORS.PROPAGATE,
|
|
50
|
+
];
|
|
51
|
+
if (error && EXPECTED_ERRORS.includes(error.type)) {
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
dwn = JSON.parse(dwnStr || "{}");
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
warnings.push(`[update-dwn] Unexpected dwn JSON ${String(err)}`);
|
|
59
|
+
dwn = {};
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
states = JSON.parse(statesStr);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
warnings.push(`[update-dwn] Unexpected states JSON ${String(err)}`);
|
|
66
|
+
states = {};
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
config = JSON.parse(configStr);
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
warnings.push(`[update-dwn] Unexpected config JSON ${String(err)}`);
|
|
73
|
+
throw new Error(`config parsing ${String(err)}`);
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
contact = JSON.parse(contactSerialized);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
warnings.push(`[update-dwn] Unexpected contact JSON ${String(err)}`);
|
|
80
|
+
throw new Error(`contact parsing ${String(err)}`);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
lists = JSON.parse(listsConfigStr || "{}");
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
warnings.push(`[update-dwn] Unexpected listsConfig JSON ${String(err)}`);
|
|
87
|
+
throw new Error(`listsConfig parsing ${String(err)}`);
|
|
88
|
+
}
|
|
89
|
+
dwn.error = undefined;
|
|
90
|
+
dwn.intent = undefined;
|
|
91
|
+
const prepare = core.prepare;
|
|
92
|
+
let allowList = [];
|
|
93
|
+
try {
|
|
94
|
+
if (!config.list.allow) {
|
|
95
|
+
throw new Error("DISABLED_LIST");
|
|
96
|
+
}
|
|
97
|
+
if (!Array.isArray(lists.allow) || lists.allow.length === 0) {
|
|
98
|
+
throw new Error("allow list is not filled or invalid");
|
|
99
|
+
}
|
|
100
|
+
allowList = lists.allow.map((id) => user__id(id, contact.identity));
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
allowList = [];
|
|
104
|
+
const _err = err instanceof Error ? err.message : String(err);
|
|
105
|
+
if (_err !== "DISABLED_LIST") {
|
|
106
|
+
warnings.push(`[update-dwn] Unexpected allowList JSON ${String(err)}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
let blockList = [];
|
|
110
|
+
try {
|
|
111
|
+
if (!config.list.block) {
|
|
112
|
+
throw new Error("DISABLED_LIST");
|
|
113
|
+
}
|
|
114
|
+
if (!Array.isArray(lists.block) || lists.block.length === 0) {
|
|
115
|
+
throw new Error("blockList is not filled or invalid");
|
|
116
|
+
}
|
|
117
|
+
blockList = lists.block.map((id) => user__id(id, contact.identity));
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
blockList = [];
|
|
121
|
+
const _err = err instanceof Error ? err.message : String(err);
|
|
122
|
+
if (_err !== "DISABLED_LIST") {
|
|
123
|
+
warnings.push(`[update-dwn] Unexpected blockList JSON ${err}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const calendar = datetime__process(new Date(), config.gmt);
|
|
127
|
+
const daypart = daypart__find(calendar.hour, config.daypart);
|
|
128
|
+
const user = user__process(contact, tunnelOriginator, !dwn.user, config.debug, core.group, core.sessions, allowList, blockList);
|
|
129
|
+
if (user.blocked) {
|
|
130
|
+
const _error = new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.BLOCKED_CONTACT, "Blocked contact", {
|
|
131
|
+
recurrence: { type: -1, state: -1, combined: -1 },
|
|
132
|
+
goto: null,
|
|
133
|
+
});
|
|
134
|
+
error = error || _error;
|
|
135
|
+
}
|
|
136
|
+
const input = input__process(core.reply, prepare.models);
|
|
137
|
+
if (input.model == Defs.INPUT_MODEL.DEBUG &&
|
|
138
|
+
input.payload["password"] === config.password) {
|
|
139
|
+
user.debug = !user.debug || undefined;
|
|
140
|
+
const _error = new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.DEBUG, `Debug mode toggled ${user.debug ? "on" : "off"}`);
|
|
141
|
+
error = error || _error;
|
|
142
|
+
}
|
|
143
|
+
if (input.model == Defs.INPUT_MODEL.GOTO &&
|
|
144
|
+
input.payload["password"] === config.password &&
|
|
145
|
+
user.debug) {
|
|
146
|
+
goto = {
|
|
147
|
+
id: input.payload.state,
|
|
148
|
+
service: input.payload.service,
|
|
149
|
+
};
|
|
150
|
+
const _error = new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.GOTO, `Force state change`, { goto });
|
|
151
|
+
error = error || _error;
|
|
152
|
+
}
|
|
153
|
+
if (input.type === Defs.INPUT_TYPE.INACTIVITY &&
|
|
154
|
+
!config.disabled.inactivity) {
|
|
155
|
+
const _error = new __definitions_1.DwnError(config.inactivity, "Inactivity detected", {
|
|
156
|
+
goto: null,
|
|
157
|
+
});
|
|
158
|
+
error = error || _error;
|
|
159
|
+
}
|
|
160
|
+
if (!intent) {
|
|
161
|
+
const [_intent, _goto] = redirect__intent(core.intent, core.intents.groups);
|
|
162
|
+
if (_intent) {
|
|
163
|
+
intent = _intent;
|
|
164
|
+
goto = _goto;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!intent) {
|
|
168
|
+
const [_intent, _goto, _payload] = user__intent(contact, core.intents.groups);
|
|
169
|
+
if (_intent) {
|
|
170
|
+
intent = _intent;
|
|
171
|
+
goto = _goto;
|
|
172
|
+
input.payload = input.payload || _payload;
|
|
173
|
+
const _error = new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.INTENT, `Contact intent ${intent.id} matched.`, { goto });
|
|
174
|
+
error = error || _error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!intent) {
|
|
178
|
+
for (const intentSpec of core.intents.groups) {
|
|
179
|
+
const [_intent, _goto, _payload] = reply__intent(core.reply, intentSpec, config);
|
|
180
|
+
if (_intent) {
|
|
181
|
+
intent = _intent;
|
|
182
|
+
goto = _goto;
|
|
183
|
+
input.payload = input.payload || _payload;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (intent && intent.id === __definitions_1.PREDEFINED_INTENTS.CHECKPOINT_BACK) {
|
|
189
|
+
const _goto = core.checkpoints.shift();
|
|
190
|
+
if (_goto) {
|
|
191
|
+
goto = goto || _goto;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
intent = null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (intent && (intent.scope !== Defs.INTENT_SCOPE.PREPARE || goto)) {
|
|
198
|
+
const _error = new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.INTENT, `Intent ${intent.id} in scope ${intent.scope} matched.`, { goto });
|
|
199
|
+
error = error || _error;
|
|
200
|
+
}
|
|
201
|
+
if (!intent && prepare) {
|
|
202
|
+
const messages = config.error.messages || [];
|
|
203
|
+
const goto = config.error.goto || undefined;
|
|
204
|
+
if (prepare.models && prepare.models.length > 0 && !input.model) {
|
|
205
|
+
const _error = new __definitions_1.DwnError(config.error.type, `Unexpected input model. Expected one of: ${prepare.models.join(", ")}`, { messages, goto });
|
|
206
|
+
error = error || _error;
|
|
207
|
+
}
|
|
208
|
+
else if (prepare.types &&
|
|
209
|
+
prepare.types.length > 0 &&
|
|
210
|
+
input.type &&
|
|
211
|
+
!prepare.types.includes(input.type)) {
|
|
212
|
+
const _error = new __definitions_1.DwnError(config.error.type, `Unexpected input type: ${input.type}. Expected one of: ${prepare.types.join(", ")}`, { messages, goto });
|
|
213
|
+
error = error || _error;
|
|
214
|
+
}
|
|
215
|
+
else if (prepare.intents && prepare.intents.length > 0) {
|
|
216
|
+
intent = {
|
|
217
|
+
id: undefined,
|
|
218
|
+
type: Defs.INTENT_TYPE.STATIC,
|
|
219
|
+
label: config.label[__definitions_1.PREDEFINED_LABELS.UNEXPECTED],
|
|
220
|
+
scope: Defs.INTENT_SCOPE.PREPARE,
|
|
221
|
+
event: prepare.id,
|
|
222
|
+
pattern: "",
|
|
223
|
+
score: 0,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (core.checkpoint) {
|
|
228
|
+
const checkpointIndex = core.checkpoints.findIndex((checkpoint) => checkpoint.id === core.state.id && checkpoint.service === core.service);
|
|
229
|
+
if (checkpointIndex >= 0) {
|
|
230
|
+
core.checkpoints.splice(0, checkpointIndex + 1);
|
|
231
|
+
}
|
|
232
|
+
core.checkpoints.unshift(core.state);
|
|
233
|
+
core.checkpoints = core.checkpoints.slice(0, config.stackSize.checkpoints);
|
|
234
|
+
}
|
|
235
|
+
if (prepare.intents && prepare.stack > 0) {
|
|
236
|
+
const intentsGroup = {
|
|
237
|
+
id: prepare.id,
|
|
238
|
+
intents: prepare.intents,
|
|
239
|
+
scope: Defs.INTENT_SCOPE.STACK,
|
|
240
|
+
event: prepare.id,
|
|
241
|
+
};
|
|
242
|
+
const stackLastIndex = core.intents.stack.reduce((lastIndex, intent, index) => intent.id === prepare.id ? index : lastIndex, -1);
|
|
243
|
+
if (stackLastIndex >= 0) {
|
|
244
|
+
core.intents.stack.splice(0, stackLastIndex + 1);
|
|
245
|
+
}
|
|
246
|
+
const groups = new Array(prepare.stack).fill(intentsGroup);
|
|
247
|
+
core.intents.stack.unshift(...groups);
|
|
248
|
+
core.intents.stack = core.intents.stack.slice(0, config.stackSize.intents);
|
|
249
|
+
}
|
|
250
|
+
dwn.journey = core.journey;
|
|
251
|
+
dwn.daypart = daypart;
|
|
252
|
+
dwn.greeting = config.messages.greeting[daypart];
|
|
253
|
+
dwn.calendar = calendar;
|
|
254
|
+
dwn.input = input;
|
|
255
|
+
dwn.user = user;
|
|
256
|
+
dwn.service = config.service.current;
|
|
257
|
+
dwn.main = core.main || false;
|
|
258
|
+
dwn.error = undefined;
|
|
259
|
+
if (error) {
|
|
260
|
+
error.goto = (0, libs_enforcesate_1.enforceState)(error.goto, config.service.current, states);
|
|
261
|
+
dwn.error = error.toJson();
|
|
262
|
+
}
|
|
263
|
+
dwn.intent = intent || undefined;
|
|
264
|
+
core.intent = intent || null;
|
|
265
|
+
}
|
|
266
|
+
catch (caught) {
|
|
267
|
+
let err = caught;
|
|
268
|
+
if (!(err instanceof __definitions_1.DwnError)) {
|
|
269
|
+
const description = `[update-dwn] Unexpected error ${String(err)}`;
|
|
270
|
+
const stack = err.stack || "";
|
|
271
|
+
err = new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.RUNTIME_ERROR, description, {
|
|
272
|
+
stack,
|
|
273
|
+
goto: null,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
error = err;
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
if (!core || typeof core !== "object") {
|
|
280
|
+
core = {};
|
|
281
|
+
}
|
|
282
|
+
if (!dwn || typeof dwn !== "object") {
|
|
283
|
+
dwn = {};
|
|
284
|
+
}
|
|
285
|
+
if (error) {
|
|
286
|
+
dwn.error = error.toJson();
|
|
287
|
+
}
|
|
288
|
+
core.warnings = warnings;
|
|
289
|
+
dwn.warnings = core.warnings.join("\n");
|
|
290
|
+
dwnStr = JSON.stringify(dwn);
|
|
291
|
+
}
|
|
292
|
+
if (core && typeof core === "object") {
|
|
293
|
+
yield _bindings_1.context.setVariableAsync(__definitions_1.VARIABLES.CORE, JSON.stringify(core));
|
|
294
|
+
}
|
|
295
|
+
if (error && error.toString() !== errorStr) {
|
|
296
|
+
yield _bindings_1.context.setVariableAsync(__definitions_1.VARIABLES.ERROR, error.toString());
|
|
297
|
+
}
|
|
298
|
+
return dwnStr;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
exports.dwnRun = dwnRun;
|
|
302
|
+
function user__source(contactIdentity) {
|
|
303
|
+
contactIdentity = String(contactIdentity);
|
|
304
|
+
if (contactIdentity.includes("@0mn.io"))
|
|
305
|
+
return Defs.USER_SOURCE.BLIP_CHAT;
|
|
306
|
+
if (contactIdentity.includes("@mailgun.gw.msging.net"))
|
|
307
|
+
return Defs.USER_SOURCE.E_MAIL;
|
|
308
|
+
if (contactIdentity.includes("@messenger.gw.msging.net"))
|
|
309
|
+
return Defs.USER_SOURCE.MESSENGER;
|
|
310
|
+
if (contactIdentity.includes("@skype.gw.msging.net"))
|
|
311
|
+
return Defs.USER_SOURCE.SKYPE;
|
|
312
|
+
if (contactIdentity.includes("@take.io"))
|
|
313
|
+
return Defs.USER_SOURCE.SMS_TAKE;
|
|
314
|
+
if (contactIdentity.includes("@tangram.com.br"))
|
|
315
|
+
return Defs.USER_SOURCE.SMS_TANGRAM;
|
|
316
|
+
if (contactIdentity.includes("@telegram.gw.msging.net"))
|
|
317
|
+
return Defs.USER_SOURCE.TELEGRAM;
|
|
318
|
+
if (contactIdentity.includes("@wa.gw.msging.net"))
|
|
319
|
+
return Defs.USER_SOURCE.WHATSAPP;
|
|
320
|
+
if (contactIdentity.includes("@instagram.gw.msging.net"))
|
|
321
|
+
return Defs.USER_SOURCE.INSTAGRAM;
|
|
322
|
+
if (contactIdentity.includes("@googlercs.gw.msging.net"))
|
|
323
|
+
return Defs.USER_SOURCE.GOOGLE_RCS;
|
|
324
|
+
if (contactIdentity.includes("@abs.gw.msging.net"))
|
|
325
|
+
return Defs.USER_SOURCE.MICROSOFT_TEAMS;
|
|
326
|
+
if (contactIdentity.includes("@businesschat.gw.msging.net"))
|
|
327
|
+
return Defs.USER_SOURCE.APPLE_BUSINESS_CHAT;
|
|
328
|
+
if (contactIdentity.includes("@workplace.gw.msging.net"))
|
|
329
|
+
return Defs.USER_SOURCE.WORKPLACE;
|
|
330
|
+
return Defs.USER_SOURCE.OUTRO;
|
|
331
|
+
}
|
|
332
|
+
function user__id(contactPrefix, contactId) {
|
|
333
|
+
if (contactPrefix.includes("@"))
|
|
334
|
+
return contactPrefix;
|
|
335
|
+
const source = user__source(contactId);
|
|
336
|
+
const domains = {
|
|
337
|
+
[Defs.USER_SOURCE.BLIP_CHAT]: "@0mn.io",
|
|
338
|
+
[Defs.USER_SOURCE.E_MAIL]: "@mailgun.gw.msging.net",
|
|
339
|
+
[Defs.USER_SOURCE.MESSENGER]: "@messenger.gw.msging.net",
|
|
340
|
+
[Defs.USER_SOURCE.SKYPE]: "@skype.gw.msging.net",
|
|
341
|
+
[Defs.USER_SOURCE.SMS_TAKE]: "@take.io",
|
|
342
|
+
[Defs.USER_SOURCE.SMS_TANGRAM]: "@tangram.com.br",
|
|
343
|
+
[Defs.USER_SOURCE.TELEGRAM]: "@telegram.gw.msging.net",
|
|
344
|
+
[Defs.USER_SOURCE.WHATSAPP]: "@wa.gw.msging.net",
|
|
345
|
+
[Defs.USER_SOURCE.INSTAGRAM]: "@instagram.gw.msging.net",
|
|
346
|
+
[Defs.USER_SOURCE.GOOGLE_RCS]: "@googlercs.gw.msging.net",
|
|
347
|
+
[Defs.USER_SOURCE.MICROSOFT_TEAMS]: "@abs.gw.msging.net",
|
|
348
|
+
[Defs.USER_SOURCE.APPLE_BUSINESS_CHAT]: "@businesschat.gw.msging.net",
|
|
349
|
+
[Defs.USER_SOURCE.WORKPLACE]: "@workplace.gw.msging.net",
|
|
350
|
+
[Defs.USER_SOURCE.OUTRO]: "",
|
|
351
|
+
};
|
|
352
|
+
return `${contactPrefix}${domains[source] || ""}`;
|
|
353
|
+
}
|
|
354
|
+
function redirect__intent(intent, groups) {
|
|
355
|
+
const { id, scope } = intent || {};
|
|
356
|
+
let goto = undefined;
|
|
357
|
+
const group = groups.find((group) => group.id === "config");
|
|
358
|
+
intent = null;
|
|
359
|
+
if (id && group && scope === Defs.INTENT_SCOPE.REDIRECT) {
|
|
360
|
+
const _intent = group.intents.find((_intent) => _intent.id === id);
|
|
361
|
+
if (_intent) {
|
|
362
|
+
intent = {
|
|
363
|
+
id: _intent.id,
|
|
364
|
+
type: Defs.INTENT_TYPE.STATIC,
|
|
365
|
+
label: `Intent redirect ${_intent.id}`,
|
|
366
|
+
scope: Defs.INTENT_SCOPE.REDIRECT,
|
|
367
|
+
event: "dwn-redirect-intents",
|
|
368
|
+
pattern: "",
|
|
369
|
+
score: 0,
|
|
370
|
+
};
|
|
371
|
+
goto = _intent.goto || undefined;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return [intent, goto];
|
|
375
|
+
}
|
|
376
|
+
function user__intent(contact, groups) {
|
|
377
|
+
let intent = null;
|
|
378
|
+
let goto = undefined;
|
|
379
|
+
let payload = null;
|
|
380
|
+
let id = undefined;
|
|
381
|
+
let event = "dwn-contact-intents";
|
|
382
|
+
const group = groups.find((group) => group.id === "config");
|
|
383
|
+
try {
|
|
384
|
+
if (!contact.extras["intent"])
|
|
385
|
+
throw new Error("Intent not found");
|
|
386
|
+
let extras = {};
|
|
387
|
+
const [intent, extrasStr] = contact.extras["intent"].split("-|-");
|
|
388
|
+
id = intent || undefined;
|
|
389
|
+
try {
|
|
390
|
+
extras = JSON.parse(extrasStr || "{}");
|
|
391
|
+
}
|
|
392
|
+
catch (e) {
|
|
393
|
+
// Keep the default extras when the stored value is invalid.
|
|
394
|
+
}
|
|
395
|
+
if (extras.event) {
|
|
396
|
+
event = extras.event;
|
|
397
|
+
}
|
|
398
|
+
if (extras.payload) {
|
|
399
|
+
payload = extras.payload;
|
|
400
|
+
}
|
|
401
|
+
if (extras.dueDate && extras.expired && extras.expired.intent) {
|
|
402
|
+
const date = new Date(extras.dueDate || 0);
|
|
403
|
+
if (new Date().getTime() > date.getTime()) {
|
|
404
|
+
id = extras.expired.intent;
|
|
405
|
+
}
|
|
406
|
+
if (extras.expired.event) {
|
|
407
|
+
event = extras.expired.event;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
catch (e) {
|
|
412
|
+
// Leave the intent unset when the contact extra is unavailable.
|
|
413
|
+
}
|
|
414
|
+
if (id && group) {
|
|
415
|
+
const intentSpec = group.intents.find((intent) => intent.id === id);
|
|
416
|
+
if (intentSpec) {
|
|
417
|
+
goto = intentSpec.goto;
|
|
418
|
+
intent = {
|
|
419
|
+
id: id ? id.trim() : undefined,
|
|
420
|
+
type: Defs.INTENT_TYPE.STATIC,
|
|
421
|
+
label: `Intent contact extra ${id}`,
|
|
422
|
+
scope: Defs.INTENT_SCOPE.CONTACT,
|
|
423
|
+
event,
|
|
424
|
+
pattern: "",
|
|
425
|
+
score: 0,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return [intent, goto, payload];
|
|
430
|
+
}
|
|
431
|
+
function is_phone(text) {
|
|
432
|
+
return Boolean(text && /^\d+$/.test(text) && text.length <= 15);
|
|
433
|
+
}
|
|
434
|
+
function user__phone(contact, source) {
|
|
435
|
+
if (source !== Defs.USER_SOURCE.WHATSAPP) {
|
|
436
|
+
return undefined;
|
|
437
|
+
}
|
|
438
|
+
if (contact.whatsAppWaId && is_phone(contact.whatsAppWaId)) {
|
|
439
|
+
return contact.whatsAppWaId;
|
|
440
|
+
}
|
|
441
|
+
const identityPrefix = contact.identity.split("@")[0];
|
|
442
|
+
if (is_phone(identityPrefix)) {
|
|
443
|
+
return identityPrefix;
|
|
444
|
+
}
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
function user__process(contact, tunnelOriginator, newUser, configDebug, group, sessions, allowList, blockList) {
|
|
448
|
+
const id = tunnelOriginator ? tunnelOriginator : contact.identity;
|
|
449
|
+
const source = user__source(contact.identity);
|
|
450
|
+
const debug = Boolean(contact.extras && contact.extras["debug"]) ||
|
|
451
|
+
configDebug ||
|
|
452
|
+
undefined;
|
|
453
|
+
const blocked = blockList.some((id) => contact.identity === user__id(id, contact.identity));
|
|
454
|
+
const allowed = allowList.some((id) => contact.identity === user__id(id, contact.identity));
|
|
455
|
+
const phone = user__phone(contact, source);
|
|
456
|
+
const cellphone = phone ? phone.substring(2) : undefined;
|
|
457
|
+
const DDD = phone ? phone.substring(2, 4) : undefined;
|
|
458
|
+
return {
|
|
459
|
+
id,
|
|
460
|
+
source,
|
|
461
|
+
new: newUser || undefined,
|
|
462
|
+
blocked,
|
|
463
|
+
allowed,
|
|
464
|
+
group,
|
|
465
|
+
sessions,
|
|
466
|
+
cellphone,
|
|
467
|
+
phone,
|
|
468
|
+
DDD,
|
|
469
|
+
debug,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
function datetime__to_iso_with_offset(date, tzOffset) {
|
|
473
|
+
const dateTz = new Date(date.getTime() + tzOffset * 3600000);
|
|
474
|
+
const dateFormatted = dateTz.toISOString().split(".")[0];
|
|
475
|
+
const offsetSign = tzOffset >= 0 ? "+" : "-";
|
|
476
|
+
const offsetHours = Math.abs(Math.floor(tzOffset))
|
|
477
|
+
.toString()
|
|
478
|
+
.padStart(2, "0");
|
|
479
|
+
const offsetMinutes = Math.abs((tzOffset % 1) * 60)
|
|
480
|
+
.toString()
|
|
481
|
+
.padStart(2, "0");
|
|
482
|
+
return `${dateFormatted}${offsetSign}${offsetHours}:${offsetMinutes}`;
|
|
483
|
+
}
|
|
484
|
+
function datetime__process(date, tzOffset) {
|
|
485
|
+
const datetime = datetime__to_iso_with_offset(date, tzOffset);
|
|
486
|
+
const calendar = {
|
|
487
|
+
datetime: datetime,
|
|
488
|
+
date: datetime.split("T")[0],
|
|
489
|
+
time: datetime.split("T")[1].split(".")[0].substring(0, 8),
|
|
490
|
+
hour: parseInt(datetime.split("T")[1].substring(0, 2)),
|
|
491
|
+
minute: parseInt(datetime.split("T")[1].substring(3, 5)),
|
|
492
|
+
GMT: tzOffset,
|
|
493
|
+
};
|
|
494
|
+
return calendar;
|
|
495
|
+
}
|
|
496
|
+
function daypart__find(hour, daypart) {
|
|
497
|
+
if (isNaN(hour) || hour < 0 || hour > 23) {
|
|
498
|
+
throw new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.RUNTIME_ERROR, `Invalid hour: ${hour}`, { goto: null });
|
|
499
|
+
}
|
|
500
|
+
if (!daypart ||
|
|
501
|
+
!daypart.MORNING ||
|
|
502
|
+
!daypart.AFTERNOON ||
|
|
503
|
+
!daypart.EVENING ||
|
|
504
|
+
!daypart.NIGHT) {
|
|
505
|
+
throw new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.MISCONFIGURATION_ERROR, "Daypart configuration is incomplete", { goto: null });
|
|
506
|
+
}
|
|
507
|
+
if (daypart.MORNING.includes(hour))
|
|
508
|
+
return Defs.DAYPART.MORNING;
|
|
509
|
+
if (daypart.AFTERNOON.includes(hour))
|
|
510
|
+
return Defs.DAYPART.AFTERNOON;
|
|
511
|
+
if (daypart.EVENING.includes(hour))
|
|
512
|
+
return Defs.DAYPART.EVENING;
|
|
513
|
+
if (daypart.NIGHT.includes(hour))
|
|
514
|
+
return Defs.DAYPART.NIGHT;
|
|
515
|
+
throw new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.MISCONFIGURATION_ERROR, `Hour ${hour} not found in any daypart`, { goto: null });
|
|
516
|
+
}
|
|
517
|
+
function type__inactivity(reply) {
|
|
518
|
+
return reply.content === "" ? null : undefined;
|
|
519
|
+
}
|
|
520
|
+
function type__flows(reply) {
|
|
521
|
+
try {
|
|
522
|
+
return JSON.parse(JSON.parse(reply.message).metadata["#wa.interactive.nfm_reply.json_response"]);
|
|
523
|
+
}
|
|
524
|
+
catch (err) {
|
|
525
|
+
// An invalid flow response does not match this reply type.
|
|
526
|
+
}
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
function type__ctwa(reply) {
|
|
530
|
+
try {
|
|
531
|
+
const { metadata } = JSON.parse(reply.message || "{}");
|
|
532
|
+
if (metadata && metadata["#wa.referral.source_id"]) {
|
|
533
|
+
return {
|
|
534
|
+
headline: metadata["#wa.referral.headline"],
|
|
535
|
+
body: metadata["#wa.referral.body"],
|
|
536
|
+
source_type: metadata["#wa.referral.source_type"],
|
|
537
|
+
source_id: metadata["#wa.referral.source_id"],
|
|
538
|
+
source_url: metadata["#wa.referral.source_url"],
|
|
539
|
+
media_id: metadata["#wa.referral.image.id"],
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
catch (err) {
|
|
544
|
+
// Missing or invalid referral data does not match this reply type.
|
|
545
|
+
}
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
function type__cart(reply) {
|
|
549
|
+
if (reply.type.includes("x-whatsapp-order+json")) {
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
return undefined;
|
|
553
|
+
}
|
|
554
|
+
function type__text(reply) {
|
|
555
|
+
return reply.type === "text/plain" ? null : undefined;
|
|
556
|
+
}
|
|
557
|
+
function type__image(reply) {
|
|
558
|
+
try {
|
|
559
|
+
const _content = JSON.parse(reply.content);
|
|
560
|
+
const _type = (_content && _content.type && _content.type.toLowerCase()) || "";
|
|
561
|
+
if (_type.includes("image")) {
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
catch (err) {
|
|
566
|
+
// Invalid content does not match this reply type.
|
|
567
|
+
}
|
|
568
|
+
return undefined;
|
|
569
|
+
}
|
|
570
|
+
function type__sticker(reply) {
|
|
571
|
+
try {
|
|
572
|
+
const _content = JSON.parse(reply.content);
|
|
573
|
+
const _type = (_content && _content.type && _content.type.toLowerCase()) || "";
|
|
574
|
+
if (_type.includes("sticker")) {
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
catch (err) {
|
|
579
|
+
// Invalid content does not match this reply type.
|
|
580
|
+
}
|
|
581
|
+
return undefined;
|
|
582
|
+
}
|
|
583
|
+
function type__video(reply) {
|
|
584
|
+
try {
|
|
585
|
+
const _content = JSON.parse(reply.content);
|
|
586
|
+
const _type = (_content && _content.type && _content.type.toLowerCase()) || "";
|
|
587
|
+
if (_type.includes("video")) {
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
catch (err) {
|
|
592
|
+
// Invalid content does not match this reply type.
|
|
593
|
+
}
|
|
594
|
+
return undefined;
|
|
595
|
+
}
|
|
596
|
+
function type__audio(reply) {
|
|
597
|
+
try {
|
|
598
|
+
const _content = JSON.parse(reply.content);
|
|
599
|
+
const _type = (_content && _content.type && _content.type.toLowerCase()) || "";
|
|
600
|
+
if (_type.includes("audio")) {
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
catch (err) {
|
|
605
|
+
// Invalid content does not match this reply type.
|
|
606
|
+
}
|
|
607
|
+
return undefined;
|
|
608
|
+
}
|
|
609
|
+
function type__document(reply) {
|
|
610
|
+
try {
|
|
611
|
+
const _content = JSON.parse(reply.content);
|
|
612
|
+
const _type = (_content && _content.type && _content.type.toLowerCase()) || "";
|
|
613
|
+
if (_type.includes("application")) {
|
|
614
|
+
return null;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
catch (err) {
|
|
618
|
+
// Invalid content does not match this reply type.
|
|
619
|
+
}
|
|
620
|
+
return undefined;
|
|
621
|
+
}
|
|
622
|
+
function type__location(reply) {
|
|
623
|
+
try {
|
|
624
|
+
const _content = JSON.parse(reply.message).content;
|
|
625
|
+
if (typeof _content === "string") {
|
|
626
|
+
return undefined;
|
|
627
|
+
}
|
|
628
|
+
const _type = reply.type;
|
|
629
|
+
if (_type.includes("location")) {
|
|
630
|
+
return {
|
|
631
|
+
latitude: _content.latitude,
|
|
632
|
+
longitude: _content.longitude,
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
catch (err) {
|
|
637
|
+
// Invalid content does not match this reply type.
|
|
638
|
+
}
|
|
639
|
+
return undefined;
|
|
640
|
+
}
|
|
641
|
+
function model__cep(reply) {
|
|
642
|
+
const text = reply.content;
|
|
643
|
+
if (typeof text !== "string") {
|
|
644
|
+
return undefined;
|
|
645
|
+
}
|
|
646
|
+
const cleaned = text.replace(/\D/g, "");
|
|
647
|
+
if (cleaned.length !== 8) {
|
|
648
|
+
return undefined;
|
|
649
|
+
}
|
|
650
|
+
return {
|
|
651
|
+
value: cleaned,
|
|
652
|
+
pretty: `${cleaned.slice(0, 5)}-${cleaned.slice(5)}`,
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
function model__cpf(reply) {
|
|
656
|
+
const text = reply.content;
|
|
657
|
+
if (typeof text !== "string") {
|
|
658
|
+
return undefined;
|
|
659
|
+
}
|
|
660
|
+
const cpf = text.replace(/\D/g, "");
|
|
661
|
+
if (cpf.length !== 11 || /^(\d)\1{10}$/.test(cpf)) {
|
|
662
|
+
return undefined;
|
|
663
|
+
}
|
|
664
|
+
let sum = 0;
|
|
665
|
+
let remainder;
|
|
666
|
+
for (let i = 1; i <= 9; i++) {
|
|
667
|
+
sum = sum + parseInt(cpf.substring(i - 1, i)) * (11 - i);
|
|
668
|
+
}
|
|
669
|
+
remainder = (sum * 10) % 11;
|
|
670
|
+
if (remainder === 10 || remainder === 11) {
|
|
671
|
+
remainder = 0;
|
|
672
|
+
}
|
|
673
|
+
if (remainder !== parseInt(cpf.substring(9, 10))) {
|
|
674
|
+
return undefined;
|
|
675
|
+
}
|
|
676
|
+
sum = 0;
|
|
677
|
+
for (let i = 1; i <= 10; i++) {
|
|
678
|
+
sum = sum + parseInt(cpf.substring(i - 1, i)) * (12 - i);
|
|
679
|
+
}
|
|
680
|
+
remainder = (sum * 10) % 11;
|
|
681
|
+
if (remainder === 10 || remainder === 11) {
|
|
682
|
+
remainder = 0;
|
|
683
|
+
}
|
|
684
|
+
if (remainder !== parseInt(cpf.substring(10, 11))) {
|
|
685
|
+
return undefined;
|
|
686
|
+
}
|
|
687
|
+
return {
|
|
688
|
+
value: cpf,
|
|
689
|
+
pretty: cpf.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1.$2.$3-$4"),
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
function model__cnpj(reply) {
|
|
693
|
+
const text = reply.content;
|
|
694
|
+
if (typeof text !== "string") {
|
|
695
|
+
return undefined;
|
|
696
|
+
}
|
|
697
|
+
const cnpj = text.replace(/[^a-zA-Z0-9]/g, "").toUpperCase();
|
|
698
|
+
if (cnpj.length !== 14 || /^([A-Z0-9])\1{13}$/.test(cnpj)) {
|
|
699
|
+
return undefined;
|
|
700
|
+
}
|
|
701
|
+
const getVal = (c) => c.charCodeAt(0) - 48;
|
|
702
|
+
const validateDigit = (base, weights) => {
|
|
703
|
+
let sum = 0;
|
|
704
|
+
for (let i = 0; i < base.length; i++) {
|
|
705
|
+
sum += getVal(base[i]) * weights[i];
|
|
706
|
+
}
|
|
707
|
+
const remainder = sum % 11;
|
|
708
|
+
return remainder < 2 ? 0 : 11 - remainder;
|
|
709
|
+
};
|
|
710
|
+
const w1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
711
|
+
const w2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
712
|
+
const base = cnpj.substring(0, 12);
|
|
713
|
+
const dv1 = validateDigit(base, w1);
|
|
714
|
+
const dv2 = validateDigit(base + dv1, w2);
|
|
715
|
+
if (dv1 !== getVal(cnpj[12]) || dv2 !== getVal(cnpj[13])) {
|
|
716
|
+
return undefined;
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
value: cnpj,
|
|
720
|
+
pretty: cnpj.replace(/^([A-Z0-9]{2})([A-Z0-9]{3})([A-Z0-9]{3})([A-Z0-9]{4})([A-Z0-9]{2})$/, "$1.$2.$3/$4-$5"),
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
function model__birth_day(reply) {
|
|
724
|
+
const text = reply.content;
|
|
725
|
+
if (typeof text !== "string") {
|
|
726
|
+
return undefined;
|
|
727
|
+
}
|
|
728
|
+
const match = text
|
|
729
|
+
.trim()
|
|
730
|
+
.match(/^(\d{1,2})[\x2f.-](\d{1,2})[\x2f.-](\d{4})$/);
|
|
731
|
+
if (!match)
|
|
732
|
+
return undefined;
|
|
733
|
+
const day = parseInt(match[1], 10);
|
|
734
|
+
const month = parseInt(match[2], 10);
|
|
735
|
+
const year = parseInt(match[3], 10);
|
|
736
|
+
const date = new Date(year, month - 1, day);
|
|
737
|
+
if (date.getFullYear() !== year ||
|
|
738
|
+
date.getMonth() + 1 !== month ||
|
|
739
|
+
date.getDate() !== day) {
|
|
740
|
+
return undefined;
|
|
741
|
+
}
|
|
742
|
+
const dayStr = String(day).padStart(2, "0");
|
|
743
|
+
const monthStr = String(month).padStart(2, "0");
|
|
744
|
+
const yearStr = String(year);
|
|
745
|
+
return {
|
|
746
|
+
year: yearStr,
|
|
747
|
+
month: monthStr,
|
|
748
|
+
day: dayStr,
|
|
749
|
+
pretty: `${dayStr}/${monthStr}/${yearStr}`,
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
function model__name(reply) {
|
|
753
|
+
const text = reply.content;
|
|
754
|
+
if (typeof text !== "string") {
|
|
755
|
+
return undefined;
|
|
756
|
+
}
|
|
757
|
+
const cleaned = text.trim();
|
|
758
|
+
const nameRegex = /^[a-zA-Z\u00C0-\u017F]+$/;
|
|
759
|
+
if (nameRegex.test(cleaned) && cleaned.split(/\s+/).length === 1) {
|
|
760
|
+
const lowercase = cleaned.toLowerCase();
|
|
761
|
+
const pretty = lowercase.charAt(0).toUpperCase() + lowercase.slice(1);
|
|
762
|
+
return {
|
|
763
|
+
value: lowercase,
|
|
764
|
+
pretty: pretty,
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
return undefined;
|
|
768
|
+
}
|
|
769
|
+
function model__full_name(reply) {
|
|
770
|
+
const text = reply.content;
|
|
771
|
+
if (typeof text !== "string") {
|
|
772
|
+
return undefined;
|
|
773
|
+
}
|
|
774
|
+
const cleaned = text.trim();
|
|
775
|
+
const fullNameRegex = /^[a-zA-Z\u00C0-\u017F]+(?:\s[a-zA-Z\u00C0-\u017F]+)+$/;
|
|
776
|
+
if (fullNameRegex.test(cleaned)) {
|
|
777
|
+
const names = cleaned.split(/\s+/).map((n) => n.toLowerCase());
|
|
778
|
+
if (names.length < 2)
|
|
779
|
+
return undefined;
|
|
780
|
+
const prettyNames = names.map((name) => name.charAt(0).toUpperCase() + name.slice(1));
|
|
781
|
+
const first = prettyNames[0];
|
|
782
|
+
const last = prettyNames.slice(1).join(" ");
|
|
783
|
+
const pretty = prettyNames.join(" ");
|
|
784
|
+
return {
|
|
785
|
+
first: first,
|
|
786
|
+
last: last,
|
|
787
|
+
names: names,
|
|
788
|
+
lowercase: cleaned.toLowerCase(),
|
|
789
|
+
pretty: pretty,
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
return undefined;
|
|
793
|
+
}
|
|
794
|
+
function model__email(reply) {
|
|
795
|
+
const text = reply.content;
|
|
796
|
+
if (typeof text !== "string") {
|
|
797
|
+
return undefined;
|
|
798
|
+
}
|
|
799
|
+
const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
800
|
+
const lowercasedText = text.toLowerCase().trim();
|
|
801
|
+
if (emailRegex.test(lowercasedText)) {
|
|
802
|
+
const parts = lowercasedText.split("@");
|
|
803
|
+
return {
|
|
804
|
+
host: parts[0],
|
|
805
|
+
domain: parts[1],
|
|
806
|
+
pretty: lowercasedText,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
return undefined;
|
|
810
|
+
}
|
|
811
|
+
function model__phone(reply) {
|
|
812
|
+
const text = reply.content;
|
|
813
|
+
if (typeof text !== "string") {
|
|
814
|
+
return undefined;
|
|
815
|
+
}
|
|
816
|
+
const cleaned = text.replace(/\D/g, "");
|
|
817
|
+
if (cleaned.length < 10 || cleaned.length > 15) {
|
|
818
|
+
return undefined;
|
|
819
|
+
}
|
|
820
|
+
let country = "55";
|
|
821
|
+
let remainingDigits = cleaned;
|
|
822
|
+
if (cleaned.length >= 12) {
|
|
823
|
+
const possibleCountry = cleaned.substring(0, cleaned.length - 10);
|
|
824
|
+
if (possibleCountry.length >= 1 && possibleCountry.length <= 3) {
|
|
825
|
+
country = possibleCountry;
|
|
826
|
+
remainingDigits = cleaned.substring(possibleCountry.length);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
if (country === "55") {
|
|
830
|
+
if (remainingDigits.length < 10 || remainingDigits.length > 11) {
|
|
831
|
+
return undefined;
|
|
832
|
+
}
|
|
833
|
+
const ddd = remainingDigits.substring(0, 2);
|
|
834
|
+
const phone = remainingDigits.substring(2);
|
|
835
|
+
const dddNum = parseInt(ddd, 10);
|
|
836
|
+
if (dddNum < 11 || dddNum > 99) {
|
|
837
|
+
return undefined;
|
|
838
|
+
}
|
|
839
|
+
if (phone.length !== 8 && phone.length !== 9) {
|
|
840
|
+
return undefined;
|
|
841
|
+
}
|
|
842
|
+
if (phone.length === 9 && phone[0] !== "9") {
|
|
843
|
+
return undefined;
|
|
844
|
+
}
|
|
845
|
+
const phonePart1 = phone.length === 9 ? phone.substring(0, 5) : phone.substring(0, 4);
|
|
846
|
+
const phonePart2 = phone.length === 9 ? phone.substring(5) : phone.substring(4);
|
|
847
|
+
const local = `(${ddd}) ${phonePart1}-${phonePart2}`;
|
|
848
|
+
const global = `+${country} ${local}`;
|
|
849
|
+
return {
|
|
850
|
+
value: country + ddd + phone,
|
|
851
|
+
country,
|
|
852
|
+
ddd,
|
|
853
|
+
phone,
|
|
854
|
+
pretty: { global, local },
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
if (remainingDigits.length >= 10) {
|
|
858
|
+
const ddd = remainingDigits.substring(0, 2);
|
|
859
|
+
const phone = remainingDigits.substring(2);
|
|
860
|
+
const phonePart1 = phone.substring(0, Math.ceil(phone.length / 2));
|
|
861
|
+
const phonePart2 = phone.substring(Math.ceil(phone.length / 2));
|
|
862
|
+
const local = `${ddd} ${phonePart1}-${phonePart2}`;
|
|
863
|
+
const global = `+${country} ${local}`;
|
|
864
|
+
return {
|
|
865
|
+
value: country + ddd + phone,
|
|
866
|
+
country,
|
|
867
|
+
ddd,
|
|
868
|
+
phone,
|
|
869
|
+
pretty: { global, local },
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
return undefined;
|
|
873
|
+
}
|
|
874
|
+
function model__debug(reply) {
|
|
875
|
+
const text = reply.content;
|
|
876
|
+
if (typeof text !== "string") {
|
|
877
|
+
return undefined;
|
|
878
|
+
}
|
|
879
|
+
const match = text.trim().match(/^#debug\s+(\S+)$/i);
|
|
880
|
+
if (match) {
|
|
881
|
+
const [_, password] = match;
|
|
882
|
+
return { password };
|
|
883
|
+
}
|
|
884
|
+
return undefined;
|
|
885
|
+
}
|
|
886
|
+
function model__goto(reply) {
|
|
887
|
+
const text = reply.content;
|
|
888
|
+
if (typeof text !== "string") {
|
|
889
|
+
return undefined;
|
|
890
|
+
}
|
|
891
|
+
const match = text.trim().match(/^#goto\s+(\S+)\s+(\S+)(?:\s+(\S+))?$/i);
|
|
892
|
+
if (match) {
|
|
893
|
+
const [_, password, state, service] = match;
|
|
894
|
+
return { password, state, service };
|
|
895
|
+
}
|
|
896
|
+
return undefined;
|
|
897
|
+
}
|
|
898
|
+
function model__ctwa(reply) {
|
|
899
|
+
const message = reply.message;
|
|
900
|
+
try {
|
|
901
|
+
const { metadata } = JSON.parse(message || "{}");
|
|
902
|
+
if (metadata && metadata["#wa.referral.source_id"]) {
|
|
903
|
+
return {
|
|
904
|
+
headline: metadata["#wa.referral.headline"],
|
|
905
|
+
body: metadata["#wa.referral.body"],
|
|
906
|
+
source_type: metadata["#wa.referral.source_type"],
|
|
907
|
+
source_id: metadata["#wa.referral.source_id"],
|
|
908
|
+
source_url: metadata["#wa.referral.source_url"],
|
|
909
|
+
media_id: metadata["#wa.referral.image.id"],
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
catch (err) {
|
|
914
|
+
// Missing or invalid referral data does not match this model.
|
|
915
|
+
}
|
|
916
|
+
return undefined;
|
|
917
|
+
}
|
|
918
|
+
function model__flows(reply) {
|
|
919
|
+
const message = reply.message;
|
|
920
|
+
try {
|
|
921
|
+
return JSON.parse(JSON.parse(message).metadata["#wa.interactive.nfm_reply.json_response"]);
|
|
922
|
+
}
|
|
923
|
+
catch (err) {
|
|
924
|
+
// An invalid flow response does not match this model.
|
|
925
|
+
}
|
|
926
|
+
return undefined;
|
|
927
|
+
}
|
|
928
|
+
function model__cart(reply) {
|
|
929
|
+
if (reply.type.includes("x-whatsapp-order+json")) {
|
|
930
|
+
return null;
|
|
931
|
+
}
|
|
932
|
+
return undefined;
|
|
933
|
+
}
|
|
934
|
+
function model__car_plate(reply) {
|
|
935
|
+
const text = reply.content;
|
|
936
|
+
const cleaned = text.replace(/-/g, "").trim().toUpperCase();
|
|
937
|
+
const carPlateRegex = /^[A-Z]{3}[\d][A-Z\d][\d]{2}$/;
|
|
938
|
+
if (carPlateRegex.test(cleaned)) {
|
|
939
|
+
const pretty = `${cleaned.substring(0, 3)}-${cleaned.substring(3)}`;
|
|
940
|
+
return {
|
|
941
|
+
value: cleaned,
|
|
942
|
+
pretty: pretty,
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
return undefined;
|
|
946
|
+
}
|
|
947
|
+
function input__process(reply, models) {
|
|
948
|
+
const content = reply.content;
|
|
949
|
+
let model = undefined;
|
|
950
|
+
let payload = null;
|
|
951
|
+
let type = Defs.INPUT_TYPE.TEXT;
|
|
952
|
+
// null means a match but a null payload, undefined means unmatched
|
|
953
|
+
const _types = [
|
|
954
|
+
Defs.INPUT_TYPE.INACTIVITY,
|
|
955
|
+
Defs.INPUT_TYPE.FLOWS,
|
|
956
|
+
Defs.INPUT_TYPE.CTWA,
|
|
957
|
+
Defs.INPUT_TYPE.CART,
|
|
958
|
+
Defs.INPUT_TYPE.TEXT,
|
|
959
|
+
Defs.INPUT_TYPE.IMAGE,
|
|
960
|
+
Defs.INPUT_TYPE.STICKER,
|
|
961
|
+
Defs.INPUT_TYPE.VIDEO,
|
|
962
|
+
Defs.INPUT_TYPE.AUDIO,
|
|
963
|
+
Defs.INPUT_TYPE.DOCUMENT,
|
|
964
|
+
Defs.INPUT_TYPE.LOCATION,
|
|
965
|
+
];
|
|
966
|
+
const typeExec = {
|
|
967
|
+
[Defs.INPUT_TYPE.INACTIVITY]: type__inactivity,
|
|
968
|
+
[Defs.INPUT_TYPE.FLOWS]: type__flows,
|
|
969
|
+
[Defs.INPUT_TYPE.CTWA]: type__ctwa,
|
|
970
|
+
[Defs.INPUT_TYPE.CART]: type__cart,
|
|
971
|
+
[Defs.INPUT_TYPE.TEXT]: type__text,
|
|
972
|
+
[Defs.INPUT_TYPE.IMAGE]: type__image,
|
|
973
|
+
[Defs.INPUT_TYPE.STICKER]: type__sticker,
|
|
974
|
+
[Defs.INPUT_TYPE.VIDEO]: type__video,
|
|
975
|
+
[Defs.INPUT_TYPE.AUDIO]: type__audio,
|
|
976
|
+
[Defs.INPUT_TYPE.DOCUMENT]: type__document,
|
|
977
|
+
[Defs.INPUT_TYPE.LOCATION]: type__location,
|
|
978
|
+
};
|
|
979
|
+
const modelExec = {
|
|
980
|
+
[Defs.INPUT_MODEL.GOTO]: model__goto,
|
|
981
|
+
[Defs.INPUT_MODEL.DEBUG]: model__debug,
|
|
982
|
+
[Defs.INPUT_MODEL.CEP]: model__cep,
|
|
983
|
+
[Defs.INPUT_MODEL.CPF]: model__cpf,
|
|
984
|
+
[Defs.INPUT_MODEL.CNPJ]: model__cnpj,
|
|
985
|
+
[Defs.INPUT_MODEL.BIRTH_DAY]: model__birth_day,
|
|
986
|
+
[Defs.INPUT_MODEL.NAME]: model__name,
|
|
987
|
+
[Defs.INPUT_MODEL.FULL_NAME]: model__full_name,
|
|
988
|
+
[Defs.INPUT_MODEL.EMAIL]: model__email,
|
|
989
|
+
[Defs.INPUT_MODEL.PHONE]: model__phone,
|
|
990
|
+
[Defs.INPUT_MODEL.CAR_PLATE]: model__car_plate,
|
|
991
|
+
[Defs.INPUT_MODEL.CTWA]: model__ctwa,
|
|
992
|
+
[Defs.INPUT_MODEL.FLOWS]: model__flows,
|
|
993
|
+
[Defs.INPUT_MODEL.CART]: model__cart,
|
|
994
|
+
[Defs.INPUT_MODEL.LOCATION]: type__location,
|
|
995
|
+
};
|
|
996
|
+
type =
|
|
997
|
+
_types.find((input_type) => typeExec[input_type](reply) !== undefined) ||
|
|
998
|
+
Defs.INPUT_TYPE.TEXT;
|
|
999
|
+
payload = typeExec[type](reply);
|
|
1000
|
+
const _models = [
|
|
1001
|
+
"GOTO",
|
|
1002
|
+
"DEBUG",
|
|
1003
|
+
...(models || []),
|
|
1004
|
+
];
|
|
1005
|
+
const MODELS = [
|
|
1006
|
+
Defs.INPUT_MODEL.GOTO,
|
|
1007
|
+
Defs.INPUT_MODEL.DEBUG,
|
|
1008
|
+
Defs.INPUT_MODEL.CEP,
|
|
1009
|
+
Defs.INPUT_MODEL.CPF,
|
|
1010
|
+
Defs.INPUT_MODEL.CNPJ,
|
|
1011
|
+
Defs.INPUT_MODEL.BIRTH_DAY,
|
|
1012
|
+
Defs.INPUT_MODEL.NAME,
|
|
1013
|
+
Defs.INPUT_MODEL.FULL_NAME,
|
|
1014
|
+
Defs.INPUT_MODEL.EMAIL,
|
|
1015
|
+
Defs.INPUT_MODEL.PHONE,
|
|
1016
|
+
Defs.INPUT_MODEL.CAR_PLATE,
|
|
1017
|
+
Defs.INPUT_MODEL.CTWA,
|
|
1018
|
+
Defs.INPUT_MODEL.FLOWS,
|
|
1019
|
+
Defs.INPUT_MODEL.CART,
|
|
1020
|
+
Defs.INPUT_MODEL.LOCATION,
|
|
1021
|
+
];
|
|
1022
|
+
const invalids = _models.filter((model) => !MODELS.includes(model));
|
|
1023
|
+
if (invalids.length > 0) {
|
|
1024
|
+
throw new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.MISCONFIGURATION_ERROR, `Invalid model '${invalids.join(", ")}'. Must be one of: ${MODELS.join(", ")}`, { goto: null });
|
|
1025
|
+
}
|
|
1026
|
+
model = _models.find((input_model) => modelExec[input_model](reply) !== undefined);
|
|
1027
|
+
if (model) {
|
|
1028
|
+
payload = modelExec[model](reply);
|
|
1029
|
+
}
|
|
1030
|
+
if (reply.payload) {
|
|
1031
|
+
payload = reply.payload;
|
|
1032
|
+
}
|
|
1033
|
+
return {
|
|
1034
|
+
content,
|
|
1035
|
+
type,
|
|
1036
|
+
payload,
|
|
1037
|
+
model,
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
function input__content(text) {
|
|
1041
|
+
return String(text)
|
|
1042
|
+
.trim()
|
|
1043
|
+
.toLowerCase()
|
|
1044
|
+
.normalize("NFD")
|
|
1045
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
1046
|
+
.replace(/[^a-z0-9\s]/g, "");
|
|
1047
|
+
}
|
|
1048
|
+
function reply__intent(reply, intentGroup, config) {
|
|
1049
|
+
const { threshold } = config;
|
|
1050
|
+
const minScore = threshold && threshold.minScore ? threshold.minScore : 0.75;
|
|
1051
|
+
let intent = null;
|
|
1052
|
+
let goto = undefined;
|
|
1053
|
+
let payload = null;
|
|
1054
|
+
for (const intentSpec of intentGroup.intents) {
|
|
1055
|
+
const { type } = intentSpec;
|
|
1056
|
+
if (type === "fuzzy") {
|
|
1057
|
+
const _intent = intent__fuzzy(reply, intentSpec, intentGroup);
|
|
1058
|
+
if (_intent && (!intent || intent.score < _intent.score)) {
|
|
1059
|
+
intent = _intent;
|
|
1060
|
+
goto = intentSpec.goto;
|
|
1061
|
+
payload = intentSpec.payload || null;
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
if (type === "regex") {
|
|
1066
|
+
const _intent = intent__regex(reply, intentSpec, intentGroup);
|
|
1067
|
+
if (_intent) {
|
|
1068
|
+
intent = _intent;
|
|
1069
|
+
goto = intentSpec.goto;
|
|
1070
|
+
payload = intentSpec.payload || null;
|
|
1071
|
+
break;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
if (type === "static") {
|
|
1075
|
+
const _intent = intent__static(reply, intentSpec, intentGroup);
|
|
1076
|
+
if (_intent) {
|
|
1077
|
+
intent = _intent;
|
|
1078
|
+
goto = intentSpec.goto;
|
|
1079
|
+
payload = intentSpec.payload || null;
|
|
1080
|
+
break;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
if (intent &&
|
|
1085
|
+
intent.type === Defs.INTENT_TYPE.FUZZY &&
|
|
1086
|
+
intent.score < minScore) {
|
|
1087
|
+
intent = null;
|
|
1088
|
+
goto = undefined;
|
|
1089
|
+
}
|
|
1090
|
+
return [intent, goto, payload];
|
|
1091
|
+
}
|
|
1092
|
+
function intent__regex(reply, intentSpec, intentGroup) {
|
|
1093
|
+
const { scope, event } = intentGroup;
|
|
1094
|
+
const { id, patterns, label } = intentSpec;
|
|
1095
|
+
let intent = null;
|
|
1096
|
+
const score = 0;
|
|
1097
|
+
const type = Defs.INTENT_TYPE.REGEX;
|
|
1098
|
+
try {
|
|
1099
|
+
const normalized = input__content(reply.content);
|
|
1100
|
+
for (const pattern of patterns || []) {
|
|
1101
|
+
if (new RegExp(pattern, "i").test(reply.content) ||
|
|
1102
|
+
new RegExp(pattern, "i").test(normalized)) {
|
|
1103
|
+
intent = {
|
|
1104
|
+
id,
|
|
1105
|
+
type,
|
|
1106
|
+
scope,
|
|
1107
|
+
label,
|
|
1108
|
+
event,
|
|
1109
|
+
pattern,
|
|
1110
|
+
score,
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
catch (err) {
|
|
1116
|
+
throw new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.RUNTIME_ERROR, `Error processing regex intent ${intentSpec.id}, error: ${err.message}`, { goto: null });
|
|
1117
|
+
}
|
|
1118
|
+
return intent;
|
|
1119
|
+
}
|
|
1120
|
+
function intent__fuzzy(reply, intentSpec, intentGroup) {
|
|
1121
|
+
const { scope, event } = intentGroup;
|
|
1122
|
+
const { id, patterns, label } = intentSpec;
|
|
1123
|
+
const content = input__content(reply.content);
|
|
1124
|
+
const type = Defs.INTENT_TYPE.FUZZY;
|
|
1125
|
+
let intent = null;
|
|
1126
|
+
let highest = 0;
|
|
1127
|
+
function similarity_score(a, b) {
|
|
1128
|
+
const maxLen = Math.max(a.length, b.length);
|
|
1129
|
+
if (maxLen === 0)
|
|
1130
|
+
return 1.0;
|
|
1131
|
+
return 1 - levenshtein_distance(a, b) / maxLen;
|
|
1132
|
+
}
|
|
1133
|
+
function levenshtein_distance(a, b) {
|
|
1134
|
+
const matrix = [];
|
|
1135
|
+
for (let i = 0; i <= b.length; i++)
|
|
1136
|
+
matrix[i] = [i];
|
|
1137
|
+
for (let j = 0; j <= a.length; j++)
|
|
1138
|
+
matrix[0][j] = j;
|
|
1139
|
+
for (let i = 1; i <= b.length; i++) {
|
|
1140
|
+
for (let j = 1; j <= a.length; j++) {
|
|
1141
|
+
if (b[i - 1] === a[j - 1]) {
|
|
1142
|
+
matrix[i][j] = matrix[i - 1][j - 1];
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
return matrix[b.length][a.length];
|
|
1150
|
+
}
|
|
1151
|
+
for (const pattern of patterns || []) {
|
|
1152
|
+
const example = input__content(pattern);
|
|
1153
|
+
const score = similarity_score(content, example);
|
|
1154
|
+
if (score > highest) {
|
|
1155
|
+
highest = score;
|
|
1156
|
+
intent = {
|
|
1157
|
+
id,
|
|
1158
|
+
type,
|
|
1159
|
+
scope,
|
|
1160
|
+
label,
|
|
1161
|
+
event,
|
|
1162
|
+
pattern,
|
|
1163
|
+
score,
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
return intent;
|
|
1168
|
+
}
|
|
1169
|
+
function intent__static(reply, intentSpec, intentGroup) {
|
|
1170
|
+
const { scope, event } = intentGroup;
|
|
1171
|
+
const { id, patterns, label } = intentSpec;
|
|
1172
|
+
let intent = null;
|
|
1173
|
+
const score = 0;
|
|
1174
|
+
const type = Defs.INTENT_TYPE.STATIC;
|
|
1175
|
+
try {
|
|
1176
|
+
const words = patterns || [];
|
|
1177
|
+
if (words.includes(reply.content)) {
|
|
1178
|
+
intent = {
|
|
1179
|
+
id,
|
|
1180
|
+
type,
|
|
1181
|
+
scope,
|
|
1182
|
+
label,
|
|
1183
|
+
event,
|
|
1184
|
+
pattern: reply.content,
|
|
1185
|
+
score,
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
catch (err) {
|
|
1190
|
+
throw new __definitions_1.DwnError(__definitions_1.PREDEFINED_ERRORS.RUNTIME_ERROR, `Error processing regex intent ${intentSpec.id}, error: ${err.message}`, { goto: null });
|
|
1191
|
+
}
|
|
1192
|
+
return intent;
|
|
1193
|
+
}
|
|
1194
|
+
(0, _build_1._compiler)("MAIN-END");
|
|
1195
|
+
//# sourceMappingURL=grid_gactions_dwn.js.map
|