|
| 1 | +/*! |
| 2 | + * Copyright 2024 WPPConnect Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { assertWid } from '../../assert'; |
| 18 | +import { downloadImage } from '../../util'; |
| 19 | +import { CatalogStore, Wid } from '../../whatsapp'; |
| 20 | +import { |
| 21 | + defaultSendMessageOptions, |
| 22 | + RawMessage, |
| 23 | + SendMessageOptions, |
| 24 | + SendMessageReturn, |
| 25 | +} from '..'; |
| 26 | +import { sendRawMessage } from '.'; |
| 27 | + |
| 28 | +export interface CatalogMessageOptions extends SendMessageOptions { |
| 29 | + jpegThumbnail?: string; |
| 30 | + title?: string; |
| 31 | + description?: string; |
| 32 | + textMessage?: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Send catalog message |
| 37 | + * |
| 38 | + * @example |
| 39 | + * ```javascript |
| 40 | + * WPP.chat.sendCatalogMessage( |
| 41 | + * '[number]@c.us', |
| 42 | + * '[number]@c.us', |
| 43 | + * { |
| 44 | + * title: 'My Catalog', |
| 45 | + * description: 'This is my catalog', |
| 46 | + * textMessage: 'Check out my catalog', |
| 47 | + * jpegThumbnail: 'data:image/jpeg;base64,...' |
| 48 | + * } |
| 49 | + * ); |
| 50 | + * ``` |
| 51 | + * |
| 52 | + * @category Message |
| 53 | + */ |
| 54 | +export async function sendCatalogMessage( |
| 55 | + chatToSend: string | Wid, |
| 56 | + chatFromCatalog: string | Wid, |
| 57 | + opts: CatalogMessageOptions |
| 58 | +): Promise<SendMessageReturn> { |
| 59 | + const options = { |
| 60 | + ...defaultSendMessageOptions, |
| 61 | + ...opts, |
| 62 | + }; |
| 63 | + chatToSend = assertWid(chatToSend); |
| 64 | + chatFromCatalog = assertWid(chatFromCatalog); |
| 65 | + const catalog = CatalogStore.get(chatFromCatalog); |
| 66 | + |
| 67 | + if (!options.jpegThumbnail) { |
| 68 | + const url = (catalog?.productCollection as any)?._models[0]?.imageCdnUrl; |
| 69 | + if (url) { |
| 70 | + try { |
| 71 | + const download = await downloadImage(url); |
| 72 | + options.jpegThumbnail = download.data.split(',', 2)[1]; |
| 73 | + } catch (error) {} |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + const catalogLink = `https://wa.me/c/${chatFromCatalog.toString().split('@')[0]}`; |
| 78 | + let rawMessage: RawMessage = {}; |
| 79 | + rawMessage = { |
| 80 | + type: 'chat', |
| 81 | + description: options?.description ?? 'Learn more about this catalog', |
| 82 | + matchedText: catalogLink, |
| 83 | + subtype: 'url', |
| 84 | + thumbnail: options.jpegThumbnail, |
| 85 | + thumbnailHeight: options.jpegThumbnail ? 100 : undefined, |
| 86 | + thumbnailWidth: options.jpegThumbnail ? 100 : undefined, |
| 87 | + title: options?.title ?? `View catalog on WhatsApp`, |
| 88 | + body: options.textMessage |
| 89 | + ? `${options.textMessage}\n${catalogLink}` |
| 90 | + : catalogLink, |
| 91 | + richPreviewType: 0, |
| 92 | + } as any; |
| 93 | + return await sendRawMessage(chatToSend, rawMessage, options); |
| 94 | +} |
0 commit comments