forked from EvolutionAPI/evolution-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractiveMessage.helper.ts
More file actions
141 lines (127 loc) · 3.67 KB
/
interactiveMessage.helper.ts
File metadata and controls
141 lines (127 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Button, KeyType } from '@api/dto/sendMessage.dto';
import { BinaryNode } from 'baileys';
export function buildInteractiveBizNode(): BinaryNode {
return {
tag: 'biz',
attrs: {},
content: [
{
tag: 'interactive',
attrs: { type: 'native_flow', v: '1' },
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
},
],
};
}
/**
* Biz node específico para `listMessage` legado.
* Necessário para o WhatsApp Web/Desktop renderizar a lista — o moderno
* (`interactiveMessage` + `single_select`) não é renderizado no Web.
*/
export function buildListBizNode(): BinaryNode {
return {
tag: 'biz',
attrs: {},
content: [{ tag: 'list', attrs: { type: 'product_list', v: '2' } }],
};
}
type NativeFlowButton = { name: string; buttonParamsJson: string };
type NativeFlowDeps = {
generateRandomId: () => string;
mapKeyType: Map<KeyType, string>;
};
export function toNativeFlowButton(button: Button, deps: NativeFlowDeps): NativeFlowButton {
const displayText = button.displayText ?? '';
switch (button.type) {
case 'url':
return {
name: 'cta_url',
buttonParamsJson: JSON.stringify({
display_text: displayText,
url: button.url,
merchant_url: button.url,
}),
};
case 'call':
return {
name: 'cta_call',
buttonParamsJson: JSON.stringify({
display_text: displayText,
phone_number: button.phoneNumber,
}),
};
case 'copy':
return {
name: 'cta_copy',
buttonParamsJson: JSON.stringify({
display_text: displayText,
copy_code: button.copyCode,
}),
};
case 'reply':
return {
name: 'quick_reply',
buttonParamsJson: JSON.stringify({
display_text: displayText,
id: button.id ?? deps.generateRandomId(),
}),
};
case 'pix':
return {
name: 'payment_info',
buttonParamsJson: JSON.stringify({
currency: button.currency,
total_amount: { value: 0, offset: 100 },
reference_id: deps.generateRandomId(),
type: 'physical-goods',
order: {
status: 'pending',
subtotal: { value: 0, offset: 100 },
order_type: 'ORDER',
items: [
{ name: '', amount: { value: 0, offset: 100 }, quantity: 0, sale_amount: { value: 0, offset: 100 } },
],
},
payment_settings: [
{
type: 'pix_static_code',
pix_static_code: {
merchant_name: button.name,
key: button.key,
key_type: deps.mapKeyType.get(button.keyType),
},
},
],
share_payment_status: false,
}),
};
default:
throw new Error(`Unsupported button type: ${(button as Button).type}`);
}
}
type ListSection = {
title: string;
rows: Array<{ title: string; description?: string; rowId: string }>;
};
export function buildSingleSelectButton(buttonText: string, sections: ListSection[]): NativeFlowButton {
const buttonParams = {
title: buttonText || ' ',
sections: (sections || []).map((section) => ({
title: section.title || ' ',
highlight_label: '',
rows: (section.rows || []).map((row, index) => {
const rowTitle = row.title || ' ';
return {
header: rowTitle,
title: rowTitle,
description: row.description || ' ',
id: row.rowId || `row_${index}`,
};
}),
})),
};
return {
name: 'single_select',
buttonParamsJson: JSON.stringify(buttonParams),
};
}