@flink-app/whatsapp-plugin 2.0.0-alpha.74
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/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +80 -0
- package/bin/flink-whatsapp.js +22 -0
- package/dist/WhatsappConnectionManager.d.ts +17 -0
- package/dist/WhatsappConnectionManager.js +60 -0
- package/dist/WhatsappTransport.d.ts +18 -0
- package/dist/WhatsappTransport.js +241 -0
- package/dist/autoRegisteredWhatsappHandlers.d.ts +6 -0
- package/dist/autoRegisteredWhatsappHandlers.js +8 -0
- package/dist/cli/send.d.ts +21 -0
- package/dist/cli/send.js +90 -0
- package/dist/compiler.d.ts +25 -0
- package/dist/compiler.js +27 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +192 -0
- package/dist/types.d.ts +252 -0
- package/dist/types.js +2 -0
- package/dist/whatsappContext.d.ts +4 -0
- package/dist/whatsappContext.js +236 -0
- package/dist/whatsappFormatting.d.ts +13 -0
- package/dist/whatsappFormatting.js +51 -0
- package/dist/whatsappRouter.d.ts +6 -0
- package/dist/whatsappRouter.js +54 -0
- package/package.json +39 -0
- package/src/WhatsappConnectionManager.ts +67 -0
- package/src/WhatsappTransport.ts +297 -0
- package/src/autoRegisteredWhatsappHandlers.ts +7 -0
- package/src/cli/send.ts +102 -0
- package/src/compiler.ts +32 -0
- package/src/index.ts +197 -0
- package/src/types.ts +284 -0
- package/src/whatsappContext.ts +385 -0
- package/src/whatsappFormatting.ts +59 -0
- package/src/whatsappRouter.ts +48 -0
- package/tsconfig.dist.json +4 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert standard Markdown to WhatsApp formatting.
|
|
3
|
+
*
|
|
4
|
+
* WhatsApp uses:
|
|
5
|
+
* *bold*
|
|
6
|
+
* _italic_
|
|
7
|
+
* ~strikethrough~
|
|
8
|
+
* ```code```
|
|
9
|
+
* `inline code` (not officially documented but works in most clients)
|
|
10
|
+
*
|
|
11
|
+
* Fenced code blocks and inline code are preserved as-is.
|
|
12
|
+
*/
|
|
13
|
+
export function toWhatsappFormat(md: string): string {
|
|
14
|
+
// Split into code-fenced blocks vs everything else so we never
|
|
15
|
+
// mangle content inside ``` … ```
|
|
16
|
+
const parts = md.split(/(```[\s\S]*?```)/g);
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < parts.length; i++) {
|
|
19
|
+
// Odd indices are fenced code blocks — leave them alone
|
|
20
|
+
if (i % 2 === 1) continue;
|
|
21
|
+
|
|
22
|
+
let text = parts[i];
|
|
23
|
+
|
|
24
|
+
// Protect inline code spans from further transforms
|
|
25
|
+
const inlineCode: string[] = [];
|
|
26
|
+
text = text.replace(/`[^`]+`/g, (match) => {
|
|
27
|
+
inlineCode.push(match);
|
|
28
|
+
return `\x00IC${inlineCode.length - 1}\x00`;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Headings: # … → *…* (bold as best approximation)
|
|
32
|
+
text = text.replace(/^#{1,6}\s+(.+)$/gm, "*$1*");
|
|
33
|
+
|
|
34
|
+
// Bold: **text** or __text__ → *text*
|
|
35
|
+
// Must run before italic so we don't eat the double markers
|
|
36
|
+
text = text.replace(/\*\*(.+?)\*\*/g, "*$1*");
|
|
37
|
+
text = text.replace(/__(.+?)__/g, "*$1*");
|
|
38
|
+
|
|
39
|
+
// Italic: *text* or _text_ → _text_
|
|
40
|
+
// Only match single * that aren't part of ** (already converted above)
|
|
41
|
+
text = text.replace(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/g, "_$1_");
|
|
42
|
+
|
|
43
|
+
// Strikethrough: ~~text~~ → ~text~
|
|
44
|
+
text = text.replace(/~~(.+?)~~/g, "~$1~");
|
|
45
|
+
|
|
46
|
+
// Links: [text](url) → text (url)
|
|
47
|
+
text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, "$1 ($2)");
|
|
48
|
+
|
|
49
|
+
// Images:  → url
|
|
50
|
+
text = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, "$2");
|
|
51
|
+
|
|
52
|
+
// Restore inline code spans
|
|
53
|
+
text = text.replace(/\x00IC(\d+)\x00/g, (_, idx) => inlineCode[Number(idx)]);
|
|
54
|
+
|
|
55
|
+
parts[i] = text;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return parts.join("");
|
|
59
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { WhatsappMessage, WhatsappRouteProps } from "./types";
|
|
2
|
+
|
|
3
|
+
function matchesStringOrRegExp(value: string, pattern: string | RegExp): boolean {
|
|
4
|
+
if (pattern instanceof RegExp) {
|
|
5
|
+
return pattern.test(value);
|
|
6
|
+
}
|
|
7
|
+
return value === pattern;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns true if the message matches the given route criteria.
|
|
12
|
+
* An empty/undefined Route always matches (catch-all).
|
|
13
|
+
*/
|
|
14
|
+
export function matchesRoute(message: WhatsappMessage, route: WhatsappRouteProps): boolean {
|
|
15
|
+
if (route.type !== undefined) {
|
|
16
|
+
if (Array.isArray(route.type)) {
|
|
17
|
+
if (!route.type.includes(message.type)) return false;
|
|
18
|
+
} else {
|
|
19
|
+
if (message.type !== route.type) return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (route.from !== undefined) {
|
|
24
|
+
if (typeof route.from === "function") {
|
|
25
|
+
if (!route.from(message)) return false;
|
|
26
|
+
} else if (!matchesStringOrRegExp(message.from, route.from)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (route.pattern !== undefined) {
|
|
32
|
+
if (typeof route.pattern === "function") {
|
|
33
|
+
if (!route.pattern(message)) return false;
|
|
34
|
+
} else if (!message.text || !matchesStringOrRegExp(message.text, route.pattern)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (route.connectionId !== undefined) {
|
|
40
|
+
if (Array.isArray(route.connectionId)) {
|
|
41
|
+
if (!route.connectionId.includes(message.connectionId)) return false;
|
|
42
|
+
} else {
|
|
43
|
+
if (message.connectionId !== route.connectionId) return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return true;
|
|
48
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2017",
|
|
4
|
+
"lib": ["esnext"],
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"allowSyntheticDefaultImports": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"module": "commonjs",
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"isolatedModules": true,
|
|
15
|
+
"noEmit": false,
|
|
16
|
+
"declaration": true,
|
|
17
|
+
"experimentalDecorators": true,
|
|
18
|
+
"checkJs": false,
|
|
19
|
+
"outDir": "dist",
|
|
20
|
+
"typeRoots": ["./node_modules/@types"]
|
|
21
|
+
},
|
|
22
|
+
"include": ["./src/**/*"],
|
|
23
|
+
"exclude": ["./node_modules/*"]
|
|
24
|
+
}
|