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