heyoo icon indicating copy to clipboard operation
heyoo copied to clipboard

The send Image function doesn't allow to share files from local system

Open sumitgsh opened this issue 1 year ago • 4 comments

The send image functions expects an image link i.e URI ,but what to do when the file is from a local Machine and I am sharing the file's absolute location as a parameter to the script. i.e D:/folder-1/folder-2/cus.png On using the above it will give not a valid URI error

sumitgsh avatar Aug 29 '22 20:08 sumitgsh

Hi @sumitgsh, That feature is not supported yet, however, there is a way to do that and I was having an issue with the integration which thus why I didn't include it at all in the library.

You will need to upload your media(image, video, sticker) to WhatsApp Cloud API, and then it will give you a media ID which you will then use it on existing methods as they already support sending media given media id.

Here is a link to the official documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#

I have included a new method upload_media to mimic the curl command in the documentation but I'm facing this weird error that messaging_product is missing in my query.

2022-08-30 20:30:40,541 - root - INFO - Uploading media imagee.jpg
2022-08-30 20:30:41,457 - root - INFO - Error uploading media imagee.jpg
2022-08-30 20:30:41,457 - root - INFO - Status code: 400
2022-08-30 20:30:41,457 - root - INFO - Response: {'error': {'message': '(#100) The parameter messaging_product is required.', 'type': 'OAuthException', 'code': 100, 'fbtrace_id': 'AbSnMtWM4sUKWLq43Q62TfF'}

There is another fellow developer who looks like was able to successfully do that but in PHP, have a look at it, and maybe you could help to make the upload_media() method get work in python.

Let me know if you need any further clarification

Thank you

Kalebu avatar Aug 30 '22 17:08 Kalebu

@Kalebu I tried to test the upload_media method by just cloning it ,was able to successfully generate the id of the image.

The id looks like {'id': '7512727592XXXX'} After that used the same Id as a parameter to `send_image' method end yeah file got shared.

So probably might me a case with token or the file's absolute location..

Was not able to recreate the issue that you mentioned ..

 2022-08-30 20:30:40,541 - root - INFO - Uploading media imagee.jpg
2022-08-30 20:30:41,457 - root - INFO - Error uploading media imagee.jpg
2022-08-30 20:30:41,457 - root - INFO - Status code: 400
2022-08-30 20:30:41,457 - root - INFO - Response: {'error': {'message': '(#100) The parameter messaging_product is required.', 'type': 'OAuthException', 'code': 100, 'fbtrace_id': 'AbSnMtWM4sUKWLq43Q62TfF'}

sumitgsh avatar Aug 30 '22 20:08 sumitgsh

This is interesting @sumitgsh because I tried thousands of times but it kept raising that exception

I updated the upload_media to get the full path of the file before loading automatically but still didn't work

Guess this is more of something to do with my account rather than WhatsApp Cloud API

Won't you mind sharing the snippet of code your ran and a bit of file structure?

Kalebu avatar Aug 30 '22 21:08 Kalebu

yes sure @Kalebu ,end Sry for the late reply.

Here is the sample code that I tested.

base_url = "https://graph.facebook.com/v14.0"

# File structure  
media="D:/dir-1/dir-2/dockre.png"

phone_number_id="101791896XXXXXX"
token=' XXXXXXX'

headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer {}".format(token),
        }
form_data = {
            "file": (media, open(media, "rb"), mimetypes.guess_type(media)[0]),
            "messaging_product": "whatsapp",
            "type": mimetypes.guess_type(media)[0],
        }

form_data = MultipartEncoder(fields=form_data)
headers =  headers.copy()
headers["Content-Type"] = form_data.content_type
       
r = requests.post(f"{base_url}/{phone_number_id}/media",headers=headers,data=form_data,)
if r.status_code == 200:
    print (r.json())

The above code is successfully generating the Image ID i.e {'id': '7512727592XXXX'} and the snippet is same as the upload_media method , So yes As u mentioned There might be a Problem with the account.

End I would love to contribute and be part of the repository and the cause as well..:)

sumitgsh avatar Sep 08 '22 13:09 sumitgsh