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 2 years 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

I cannot send image through this method

umerghayaz avatar Nov 11 '22 15:11 umerghayaz

Hi @umerghayaz Can you share the exact error messages you're encountering so that we can better understand your situation ?

Kalebu avatar Nov 11 '22 18:11 Kalebu

import requests import json media="C:/PYTHON/Python3.8/Scripts/office_work/Capture001.png"

print(media)

caption='JJJ' PHONE_ID = "" TOKEN = "" NUMBER = "" MESSAGE = "hello"

URL = "https://graph.facebook.com/v15.0/"+PHONE_ID+"/media" headers = { "Authorization": "Bearer "+TOKEN, "Content-Type": "application/json" } data = { "messaging_product": "whatsapp", "to": NUMBER, "type": "image/png", "file":media ,

} response = requests.post(URL, headers=headers, data=data) response_json = response.json() print(response_json) my error {'error': {'message': 'An unknown error has occurred.', 'type': 'OAuthException', 'code': 1, 'fbtrace_id': 'AUxIoQ-SmKUe9d-jbEHpph_'}}

umerghayaz avatar Nov 12 '22 09:11 umerghayaz

media_id = whatsapp.upload_media( media='', ) messenger.send_image( image=media_id, recipient_id="", link=False ) my error 2022-11-12 14:13:53,853 - root - INFO - Content-Type: multipart/form-data; boundary=417ee1c9e953485f9d22bb9506e3efb2 2022-11-12 14:13:53,855 - root - INFO - Uploading media C:/PYTHON/Python3.8/Scripts/office_work/Capture001.png 2022-11-12 14:13:54,498 - root - INFO - Error uploading media C:/PYTHON/Python3.8/Scripts/office_work/Capture001.png 2022-11-12 14:13:54,503 - root - INFO - Status code: 400 2022-11-12 14:13:54,506 - root - INFO - Response: {'error': {'message': '(#100) The parameter messaging_product is required.', 'type': 'OAuthException', 'code': 100, 'fbtrace_id': 'ABvV0tOV9sTGWiPbzVwpk_T'}} 2022-11-12 14:13:54,912 - root - INFO - Status code: 400 2022-11-12 14:13:54,915 - root - ERROR - {'error': {'message': '(#100) Either one of media ID or link must be present', 'type': 'OAuthException', 'code': 100, 'error_subcode': 2494009, 'fbtrace_id': 'AMSJJxy83UQuXmoWSsFG8jN'}}

umerghayaz avatar Nov 12 '22 09:11 umerghayaz

Hi @umerghayaz

Here that's where I am confused because it didn't work for me either, I was having that issue that messaging_product was missing while its present, and then came @sumitgsh and told me it worked for me so I thought it was more of a personal account issue but here you comes and experiencing the same issue I was encountering so I guess it still an open issue to be fixed

Hey @soerenetler Does this work for you ?

Kalebu avatar Nov 12 '22 16:11 Kalebu

2022-12-20 08:35:40,109 - root - INFO - Error uploading media final.png 2022-12-20 08:35:40,109 - root - INFO - Status code: 400 2022-12-20 08:35:40,109 - root - INFO - Response: {'error': {'message': '(#100) The parameter messaging_product is required.', 'type': 'OAuthException', 'code': 100, 'fbtrace_id': 'ApwyF_hzAPhT6qusNDUNeHG'}}

Hi. I have the same issue. @Kalebu can you please help? please reach me in mail [email protected] thank you

rinab301 avatar Dec 20 '22 09:12 rinab301

I fixed it: in def upload media do: header=header (not self.header). the problem is that self.headers really missing messaging product

rinab301 avatar Dec 21 '22 16:12 rinab301

I fixed it: in def upload media do: header=header (not self.header). the problem is that self.headers really missing messaging product

Great Rina, Thanks so much for spotting it

I've already added to the main source code, Will be added to the next release

Kalebu avatar Dec 21 '22 16:12 Kalebu

I have tried again and facing the same issue.{'error': {'message': '(#100) Either one of media ID or link must be present', 'type': 'OAuthException', 'code': 100, 'error_subcode': 2494009, 'fbtrace_id': 'AZdGMJW8CW5YtLy2dEr0k3e'}} @Kale and @rinab301 please tell how you have solved the problem. I have updated the library but still I am facing this issue. thanks

umerghayaz avatar Dec 28 '22 05:12 umerghayaz

@umerghayaz its another error. the local file you are sending is jpeg?

rinab301 avatar Dec 28 '22 06:12 rinab301

can you please tell me how to locally send pictures and documents

umerghayaz avatar Dec 28 '22 07:12 umerghayaz

yes I am sending jpeg file

umerghayaz avatar Dec 28 '22 07:12 umerghayaz

I have tried again and facing the same issue.{'error': {'message': '(#100) Either one of media ID or link must be present', 'type': 'OAuthException', 'code': 100, 'error_subcode': 2494009, 'fbtrace_id': 'AZdGMJW8CW5YtLy2dEr0k3e'}} @kale and @rinab301 please tell how you have solved the problem. I have updated the library but still I am facing this issue. thanks

Does the Error Message show that you neither attached the link nor media_id, can you please log the response from upload_media function ?

Kalebu avatar Dec 29 '22 09:12 Kalebu

MY FULL CODE AND LOG: CODE: media_id = messenger.upload_media( media='C:/Users/IT/PycharmProjects/whatsapptry/static/images/1.jpeg', ) messenger.send_image( image=media_id, recipient_id="",

)

ERROR: 2022-12-29 14:19:22,961 - root - INFO - Content-Type: multipart/form-data; boundary=1ee1a0d5ca524dc0b3042e06cbca6403 2022-12-29 14:19:22,962 - root - INFO - Uploading media C:/Users/IT/PycharmProjects/whatsapptry/static/images/1.jpeg 2022-12-29 14:19:26,924 - root - INFO - Media C:/Users/IT/PycharmProjects/whatsapptry/static/images/1.jpeg uploaded 2022-12-29 14:19:26,925 - root - INFO - Sending image to 923462901820 2022-12-29 14:19:26,925 - root - INFO - Sending document to {'id': '881430619716409'} 2022-12-29 14:19:28,223 - root - INFO - Image not sent to 923462901820 2022-12-29 14:19:28,223 - root - INFO - Status code: 400 2022-12-29 14:19:28,223 - root - ERROR - {'error': {'message': "(#100) Param image['link'] is not a valid URI.", 'type': 'OAuthException', 'code': 100, 'fbtrace_id': 'Ahcu1JsU5WSP9j1hDQB0QFa'}}

umerghayaz avatar Dec 29 '22 09:12 umerghayaz

it is working I forgot to add link = false

umerghayaz avatar Dec 29 '22 09:12 umerghayaz

it is working I forgot to add link = false

Great, Thank you all for raising the issue and bringing ideas to fix it @umerghayaz @rinab301 @sumitgsh

I will now close this issue

Kalebu avatar Dec 29 '22 11:12 Kalebu