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 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
- bot.processor.onMessagePreProcess(async (whatsapp, message) => {
215
- await whatsapp.messages.markAsRead({ messageId: message.id });
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
- // Handle different message types
219
- bot.processor.onMessage(MessageTypesEnum.Text, async (whatsapp, message) => {
220
- // Handle text messages
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.onMessage(MessageTypesEnum.Image, async (whatsapp, message) => {
224
- // Handle image messages
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.onMessage(MessageTypesEnum.Document, async (whatsapp, message) => {
228
- // Handle document messages
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