@tec.pet/tecpet-sdk 0.0.173 → 0.0.174
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.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ChatbotActionJson, ChatbotTriggerJson } from '../../domain/chatbot/dto/chatbot-settings.json';
|
|
2
|
+
export interface ChatbotTriggerMatch {
|
|
3
|
+
trigger: ChatbotTriggerJson;
|
|
4
|
+
action: ChatbotActionJson;
|
|
5
|
+
}
|
|
6
|
+
export interface MatchChatbotTriggerInput {
|
|
7
|
+
message: string;
|
|
8
|
+
triggers?: ChatbotTriggerJson[];
|
|
9
|
+
actions?: ChatbotActionJson[];
|
|
10
|
+
now?: Date;
|
|
11
|
+
}
|
|
12
|
+
export declare function normalizeChatbotTriggerText(value?: string): string;
|
|
13
|
+
export declare function isChatbotTriggerExpired(trigger: ChatbotTriggerJson, now?: Date): boolean;
|
|
14
|
+
export declare function matchChatbotTrigger({ message, triggers, actions, now, }: MatchChatbotTriggerInput): ChatbotTriggerMatch | null;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeChatbotTriggerText = normalizeChatbotTriggerText;
|
|
4
|
+
exports.isChatbotTriggerExpired = isChatbotTriggerExpired;
|
|
5
|
+
exports.matchChatbotTrigger = matchChatbotTrigger;
|
|
6
|
+
const chatbot_trigger_pattern_type_enum_1 = require("../../domain/chatbot/enum/chatbotTriggerEnums/chatbot-trigger-pattern-type.enum");
|
|
7
|
+
const DIACRITICS = /\p{Diacritic}/gu;
|
|
8
|
+
const INVISIBLE_CHARACTERS = /[\u00AD\u200B-\u200F\u2060\uFEFF]/gu;
|
|
9
|
+
const NON_ALPHANUMERIC = /[^\p{L}\p{N}\s]/gu;
|
|
10
|
+
const WHITESPACE = /\s+/gu;
|
|
11
|
+
function normalizeChatbotTriggerText(value) {
|
|
12
|
+
if (!value) {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
return value
|
|
16
|
+
.normalize('NFD')
|
|
17
|
+
.replace(DIACRITICS, '')
|
|
18
|
+
.replace(INVISIBLE_CHARACTERS, '')
|
|
19
|
+
.replace(NON_ALPHANUMERIC, ' ')
|
|
20
|
+
.replace(WHITESPACE, ' ')
|
|
21
|
+
.trim()
|
|
22
|
+
.toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
function isChatbotTriggerExpired(trigger, now = new Date()) {
|
|
25
|
+
if (!trigger.limit) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const limit = new Date(trigger.limit);
|
|
29
|
+
if (Number.isNaN(limit.getTime())) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const endOfLimitDay = Date.UTC(limit.getUTCFullYear(), limit.getUTCMonth(), limit.getUTCDate(), 23, 59, 59, 999);
|
|
33
|
+
return endOfLimitDay < now.getTime();
|
|
34
|
+
}
|
|
35
|
+
function matchChatbotTrigger({ message, triggers, actions, now = new Date(), }) {
|
|
36
|
+
const normalizedMessage = normalizeChatbotTriggerText(message);
|
|
37
|
+
if (!normalizedMessage || !triggers?.length || !actions?.length) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const enabledActions = actions.filter(action => action.enabled);
|
|
41
|
+
for (const trigger of triggers) {
|
|
42
|
+
if (!trigger.enabled || isChatbotTriggerExpired(trigger, now)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const normalizedKeyText = normalizeChatbotTriggerText(trigger.keyText);
|
|
46
|
+
if (!normalizedKeyText) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const matched = trigger.patternType === chatbot_trigger_pattern_type_enum_1.ChatbotTriggerPatternTypeEnum.EXACTLY_MATCH
|
|
50
|
+
? normalizedMessage === normalizedKeyText
|
|
51
|
+
: trigger.patternType === chatbot_trigger_pattern_type_enum_1.ChatbotTriggerPatternTypeEnum.CONTAIN_TEXT &&
|
|
52
|
+
normalizedMessage.includes(normalizedKeyText);
|
|
53
|
+
if (!matched) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const action = enabledActions.find(enabledAction => enabledAction.id === trigger.chatbotAction?.id);
|
|
57
|
+
if (!action) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
return { trigger, action };
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./validators/cpf.validator"), exports);
|
|
18
|
+
__exportStar(require("./chatbot/chatbot-trigger.matcher"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tec.pet/tecpet-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.174",
|
|
4
4
|
"description": "TecPet SDK for integration with TecPet services",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc",
|
|
23
|
-
"watch": "tsc-watch"
|
|
23
|
+
"watch": "tsc-watch",
|
|
24
|
+
"test": "jest"
|
|
24
25
|
},
|
|
25
26
|
"engines": {
|
|
26
27
|
"node": ">=23"
|
|
@@ -32,6 +33,9 @@
|
|
|
32
33
|
"date-fns": "^4.1.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
36
|
+
"@types/jest": "^29.5.14",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"ts-jest": "^29.3.0",
|
|
35
39
|
"tsc-watch": "^6.2.1",
|
|
36
40
|
"typescript": "^5.8.2"
|
|
37
41
|
}
|