Does anyone have a sample code for sending a PDF file to a WhatsApp recipient from a Win64 VCL program? I’m using Delphi RIO 10.3
Thank you in advance
This is the relevant code from the WhatsApp decumentation:
Step1: Upload media to cloud API.
curl -X POST \
'https://graph.facebook.com/v15.0/FROM_PHONE_NUMBER_ID/media' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-F 'file=@/local/path/file.jpg;type=image/jpeg'
-F 'messaging_product=whatsapp'
A successful response includes an object with an identifier for the media:
{
“id”:“ID”
}
Step 2: Send message using media ID.
curl -X POST \
'https://graph.facebook.com/v15.0/FROM-PHONE-NUMBER-ID/messages' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "PHONE-NUMBER",
"type": "image",
"image": {
"id" : "MEDIA-OBJECT-ID"
}
}'
curl -X POST \
'https://graph.facebook.com/v15.0/FROM_PHONE_NUMBER_ID/media' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-F 'file=@/local/path/file.jpg;type=image/jpeg'
-F 'messaging_product=whatsapp'
I don’t know how to translate the file parameter.
-F 'file=@/local/path/file.jpg;type=image/jpeg'
It includes both path and file type attributes.
Hi,
Sorry, not well tested, but if using the built in Rest Client, maybe something like.
(Assumes there is a RestRequest1, RestClient1 and RestResponse1 on the form)
Is it supposed to send json? this doesnt.
procedure TForm1.Button1Click(Sender: TObject);
var
fStream: TFileStream;
fAccessToken: String;
begin
fAccessToken := 'TheAccessToken';
fStream := TFileStream.Create('..\image1.jpg', fmOpenRead);
try
//RestClient1.BaseURL := 'https://graph.facebook.com/v15.0/FROM_PHONE_NUMBER_ID/media';
RestClient1.Params.Clear;
RestClient1.Params.AddItem('Authorization', 'Bearer ' + fAccessToken, pkHTTPHEADER, [poDoNotEncode], ctNone);
RestRequest1.Method := rmPost;
Restrequest1.Params.Clear;
Restrequest1.Params.AddItem('file',nil, pkFile, [], 'image/jpeg');
Restrequest1.Params.AddItem('messaging_product', 'whatsapp', pkGetOrPost, [poDoNotEncode]);
Restrequest1.Params[0].SetStream(fStream);
RestRequest1.Execute;
finally
FreeAndNil(fStream);
end;
compilation error for this line:
request.Params.AddItem('file', nil, pkFile, [], 'image/jpeg');
[dcc64 Error] fmMain.pas(384): E2250 There is no overloaded version of ‘AddItem’ that can be called with these arguments
This line compiled but I got error result,
request.Params.AddItem('file', nil, pkFile, [], ctIMAGE_JPEG );
Bad Request: {“error”:{“message”:“(#100) The parameter file is required.”,“type”:“OAuthException”,“code”:100,“fbtrace_id”:“A5tt6T05cD7MDL5G0gghnKm”}}
That last error message looks like an authentication problem.