uploadMediaFileByUrl method

Future<void> uploadMediaFileByUrl({
  1. required String phoneNumberId,
  2. required String accessToken,
  3. required String fileUrl,
  4. required String fileType,
})

Implementation

Future<void> uploadMediaFileByUrl({
  required String phoneNumberId,
  required String accessToken,
  required String fileUrl,
  required String fileType,
}) async {
  HttpOverrides.global = MyHttpOverrides();

  try {
    // Download the file from the public URL
    var fileResponse = await http.get(Uri.parse(fileUrl));
    if (fileResponse.statusCode != 200) {
      throw Exception('Failed to download file');
    }

    // Get the file bytes
    Uint8List fileBytes = fileResponse.bodyBytes;

    var uri =
        Uri.parse('https://graph.facebook.com/v20.0/$phoneNumberId/media');

    var request = http.MultipartRequest('POST', uri);
    request.headers.addAll({"Authorization": "Bearer $accessToken"});
    request.fields['type'] = fileType;
    request.fields['messaging_product'] = 'whatsapp';

    request.files.add(http.MultipartFile.fromBytes(
      'file',
      fileBytes,
      filename: 'upload.jpg', // Replace with actual filename if available
      contentType: MediaType.parse(fileType),
    ));

    var resp = await request.send();
    var res = await http.Response.fromStream(resp);

    // Declare your class variables here or ensure they are accessible
    httpCode = res.statusCode;
    response = res.body;

    if (res.statusCode >= 400) {
      // Handle error status codes
      var jsonResponse = jsonDecode(res.body);
      errorMessage = jsonResponse['error']?['message'] ?? 'Unknown error';
    } else {
      // Handle successful response
      var jsonResponse = jsonDecode(res.body);
      mediaId = jsonResponse['id'] ?? '';
    }
  } catch (e) {
    // Handle exceptions
    error = 'Exception uploading media: $e';
  }
}