0

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.

1
  • Is the text {body:TEST} valid JSON? I think it'll need to be {"body":"TEST"} but because you are in a VBA string you will have to double your quotes so {""body"":""test""} Commented Dec 23, 2023 at 22:32

2 Answers 2

0

Eureka!

After realising that the error was being caused by the terrible JSON i made (Thanks to S Meaden), i have found a really good post here on StackOverflow, showing how to create a propper JSON.

The solution: Objects to JSON

The working code:

Private Sub CommandButton4_Click()
Dim INSTANCE_ID, URL As String
Dim REQUEST As Object
Dim body As String
Set dictSubValues = CreateObject("Scripting.Dictionary")
Set dictBody = CreateObject("Scripting.Dictionary")

INSTANCE_ID = "EAA..."
URL = "https://graph.facebook.com/v17.0/196.../messages"

Set REQUEST = CreateObject("MSXML2.ServerXMLHTTP.6.0")

dictSubValues.Add "preview_url", "false"
dictSubValues.Add "body", "TEST"

dictBody.Add "messaging_product", "whatsapp"
dictBody.Add "recipient_type", "individual"
dictBody.Add "to", "5511..."
dictBody.Add "type", "text"
dictBody.Add "text", dictSubValues

body = ToJson(dictBody)

With REQUEST
    .Open "POST", URL, False
    .setRequestHeader "Authorization", "Bearer " & INSTANCE_ID
    .setRequestHeader "Content-Type", "application/json"
    .send body
End With

Application.Wait Now() + TimeSerial(0, 0, 1)
Debug.Print "Status: " & REQUEST.Status & " - Text: " & REQUEST.responseText
Set REQUEST = Nothing
End Sub

The response:

Status: 200 - Text: {"messaging_product":"whatsapp","contacts":[{"input":"5511...","wa_id":"5511..."}],"messages":[{"id":"wamid.HBg..."}]}

Sign up to request clarification or add additional context in comments.

Comments

0

Hi there It may my solution help you .. Just find the below solution for sending the custom template Because we are getting same problem

Steps:-

1:- check your template .is you are passing the all param as your template need like you if you area passing header image etc.

2- add the type param in body Like :- "type": "template"(don't miss it).

3 :- check my json

{
    "messaging_product": "whatsapp",
    "to": "911234567890",
    "type": "template",//don't miss it
    "template": {
        "name": "website",
        "language": {
            "code": "en"
        },
        "components": [
            {
                "type": "body",
                "parameters": [
                    {
                        "type": "text",
                        "text": "Ritik"
                    },
                    {
                        "type": "text",
                        "text": "Rawat"
                    },
                    {
                        "type": "text",
                        "text": "748869282"
                    }
                ]
            }
        ]
    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.