ReactNative: While trying to send a media image using the @twilio/conversations API I got a TypeError

84 Views Asked by At

While reading the source code, I didn't see any related to prepareMessage().addMedia() to it, maybe someone can clarify, having {logLevel: 'debug'} doesn't help much.

I already try with FormData, Blob, Buffer, Base64, the file uri, and URL of jpg, svg, in some cases got same errors, in others got something that has sense. Working with Buffer is not supported or at least it throw an error related to it. There is also a message related to FormData only working with Browser API in the docs.

The error is: TypeError: Cannot read property 'content' of undefined

This is my code in React Native: I am following the recommended way to send messages and media in Docs. I am using typescript. And it is important to say that sending a message without media does works well (just text, a url).

 const {type, fileName, base64} = myAsset?.assets[0];

 const file = await fetch(
        'https://images.unsplash.com/photo-1694445681139-a203ed671c89?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3987&q=80',
      );
 const fileBlob = await file.blob();

 // Send a media message
 const sendMediaOptions: SendMediaOptions = {
     contentType: file.headers.get('Content-Type'),
     filename: fileName,
     media: fileBlob,
 };

 conversationClient
        ?.prepareMessage()
        .setBody(newMessages[0]?.text as string)
        .setAttributes(attributes)
        .addMedia(sendMediaOptions)
        .build()
        .send();

References: https://sdk.twilio.com/js/conversations/releases/2.4.1/docs/classes/Conversation.html#prepareMessage

NPM Packages "@twilio/conversations": "^2.4.1" and "events": "^3.3.0",

Thanks for the Help!

0

There are 0 best solutions below