Just for fun, and maybe for future use, I'm trying to run a VBA code where it sends a WhatsApp message whenever something happens.
Right now I'm having trouble on converting the API CURL command that WhatsApp provides:
curl -X POST \
'https://graph.facebook.com/v18.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": "text",
"text": { // the text object
"preview_url": false,
"body": "MESSAGE_CONTENT"
}
}'
Using some code examples on how to send SMS messages, I got to this point:
Private Sub CommandButton4_Click()
Dim INSTANCE_ID, URL As String
Dim REQUEST As Object
INSTANCE_ID = "EAA..."
URL = "https://graph.facebook.com/v17.0/196.../messages"
Set REQUEST = CreateObject("MSXML2.ServerXMLHTTP.6.0")
With REQUEST
.Open "POST", URL
.setRequestHeader "Authorization", "Bearer " & INSTANCE_ID
.setRequestHeader "Content-Type", "application/json"
.send "messaging_product=whatsapp&recipient_type=individual&to=5511...&type=text&text:{body:TEST}"
End With
Application.Wait Now() + TimeSerial(0, 0, 1)
Debug.Print "Status: " & REQUEST.Status & " - Text: " & REQUEST.responseText
Set REQUEST = Nothing
End Sub
Executing the code above, the response is:
Status: 400 - Text: {"error":{"message":"(#100) Invalid parameter","type":"OAuthException","code":100,"error_data":{"messaging_product":"whatsapp","details":"Parameter 'text' is mandatory for type 'text'"},"fbtrace_id":"AWr..."}}
It's my first time trying to execute an API via VBA. I know that running this on Java would be much easier, but I think that this challenge would improve my understanding on the topic.