meta-cloud-api 1.4.0 → 1.6.0
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/README.md +69 -9
- package/dist/index.d.ts +589 -276
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -13
- package/dist/index.js.map +1 -1
- package/dist/types/{enums-Dx5yD3dt.d.ts → enums-DEu7hLmx.d.ts} +4 -1
- package/dist/types/enums-DEu7hLmx.d.ts.map +1 -0
- package/dist/types/{enums-DR1DVLAK.js → enums-U752Hos7.js} +1 -1
- package/dist/types/enums-U752Hos7.js.map +1 -0
- package/dist/types/enums.d.ts +1 -1
- package/dist/types/enums.js +1 -1
- package/dist/types/index.d.ts +14 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -1
- package/dist/utils/index.d.ts +43 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -2
- package/dist/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/types/enums-DR1DVLAK.js.map +0 -1
- package/dist/types/enums-Dx5yD3dt.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -211,21 +211,81 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|
|
211
211
|
import { MessageTypesEnum } from 'meta-cloud-api';
|
|
212
212
|
|
|
213
213
|
// Pre-process all messages (e.g., mark as read)
|
|
214
|
-
|
|
215
|
-
|
|
214
|
+
// ✨ Use processed.messageId for consistent ID access across all message types
|
|
215
|
+
bot.processor.onMessagePreProcess(async (whatsapp, processed) => {
|
|
216
|
+
// messageId is automatically extracted from the correct location
|
|
217
|
+
// (message.id for most types, message.context.id for nfm_reply)
|
|
218
|
+
await whatsapp.messages.markAsRead({ messageId: processed.messageId });
|
|
216
219
|
});
|
|
217
220
|
|
|
218
|
-
//
|
|
219
|
-
|
|
220
|
-
|
|
221
|
+
// ✨ Type-safe specialized handlers (recommended)
|
|
222
|
+
// These methods provide better type safety - no need for optional chaining!
|
|
223
|
+
|
|
224
|
+
bot.processor.onText(async (whatsapp, processed) => {
|
|
225
|
+
const { message, messageId } = processed;
|
|
226
|
+
// message.text is guaranteed to exist - no need for message.text?.body
|
|
227
|
+
console.log('Received:', message.text.body);
|
|
228
|
+
await whatsapp.messages.text({
|
|
229
|
+
to: message.from,
|
|
230
|
+
text: { body: `Echo: ${message.text.body}` }
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Mark as read using the extracted messageId
|
|
234
|
+
await whatsapp.messages.markAsRead({ messageId });
|
|
221
235
|
});
|
|
222
236
|
|
|
223
|
-
bot.processor.
|
|
224
|
-
|
|
237
|
+
bot.processor.onImage(async (whatsapp, processed) => {
|
|
238
|
+
const { message } = processed;
|
|
239
|
+
// message.image is guaranteed to exist
|
|
240
|
+
console.log('Image ID:', message.image.id);
|
|
241
|
+
console.log('Caption:', message.image.caption);
|
|
225
242
|
});
|
|
226
243
|
|
|
227
|
-
bot.processor.
|
|
228
|
-
|
|
244
|
+
bot.processor.onInteractive(async (whatsapp, processed) => {
|
|
245
|
+
const { message, messageId } = processed;
|
|
246
|
+
// message.interactive is guaranteed to exist
|
|
247
|
+
if (message.interactive.type === 'nfm_reply') {
|
|
248
|
+
const flowResponse = JSON.parse(message.interactive.nfm_reply.response_json);
|
|
249
|
+
console.log('Flow response:', flowResponse);
|
|
250
|
+
|
|
251
|
+
// messageId works correctly for nfm_reply (uses context.id automatically)
|
|
252
|
+
await whatsapp.messages.markAsRead({ messageId });
|
|
253
|
+
} else if (message.interactive.type === 'button_reply') {
|
|
254
|
+
console.log('Button clicked:', message.interactive.button_reply.id);
|
|
255
|
+
} else if (message.interactive.type === 'list_reply') {
|
|
256
|
+
console.log('List item selected:', message.interactive.list_reply.id);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Other specialized handlers available:
|
|
261
|
+
// onVideo, onAudio, onDocument, onSticker, onButton,
|
|
262
|
+
// onLocation, onContacts, onReaction, onOrder, onSystem
|
|
263
|
+
|
|
264
|
+
// 📝 Legacy generic handler (still supported)
|
|
265
|
+
bot.processor.onMessage(MessageTypesEnum.Text, async (whatsapp, processed) => {
|
|
266
|
+
// With generic handler, you need optional chaining
|
|
267
|
+
console.log(processed.message.text?.body); // ⚠️ Need ? because text might be undefined
|
|
268
|
+
|
|
269
|
+
// But messageId is always available regardless of message type!
|
|
270
|
+
await whatsapp.messages.markAsRead({ messageId: processed.messageId });
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// 🔍 TypeScript Discriminated Union for Manual Type Narrowing
|
|
274
|
+
// Use this when you need to check message types dynamically in pre/post-process handlers
|
|
275
|
+
bot.processor.onMessagePreProcess(async (whatsapp, processed) => {
|
|
276
|
+
const { message, messageId } = processed;
|
|
277
|
+
|
|
278
|
+
// TypeScript automatically narrows the type based on the type property!
|
|
279
|
+
if (message.type === MessageTypesEnum.Text) {
|
|
280
|
+
console.log(message.text.body); // ✅ No optional chaining needed!
|
|
281
|
+
} else if (message.type === MessageTypesEnum.Image) {
|
|
282
|
+
console.log(message.image.id); // ✅ Type-safe!
|
|
283
|
+
} else if (message.type === MessageTypesEnum.Interactive) {
|
|
284
|
+
console.log(message.interactive.type); // ✅ Guaranteed to exist!
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// messageId works for all types - automatically extracted from the correct location
|
|
288
|
+
await whatsapp.messages.markAsRead({ messageId });
|
|
229
289
|
});
|
|
230
290
|
|
|
231
291
|
// Handle message status updates
|