MT Designer
MT Designer

Reputation: 105

Send message with cloud api whatsapp and google app script

Meta recently released the cloud api to send messages from Whatsapp business, but I can't send it from the google app script.

I have this code, it runs fine... but it doesn't reach the user

    function SendToUser() {
      var headers = {
        'Authorization' : 'Bearer ACCESS_TOKEN',
        'Content-Type': 'application/json'
        };
         
        var payload = {
        "messaging_product": "whatsapp",
        "recipient_type": "individual",
        "to": "PHONE_NUMBER",
        "type": "text",
        "text": { // the text object
           "preview_url": false,
           "body": "MESSAGE_CONTENT"
        }
       }
       
      var options = {
        method: "POST",
        headers: headers,
        payload: JSON.stringify(payload) // <--- Modified
      }
    
      let response = UrlFetchApp.fetch("https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages", options);

      Logger.log(response)
            
    }

Upvotes: 0

Views: 4868

Answers (5)

Ashutosh Pathak
Ashutosh Pathak

Reputation: 1

only approved templates can be sent as first message from whatsapp API account/phone number, try sending any message say 'Hi' back to api whatsapp account/phone number by the recipient to whom you were sending the message in the above script and then run the script, it runs for me by doing this.


Upvotes: 0

apositivo
apositivo

Reputation: 493

dear friends of the stackoverflow community, The official documentation indicates that in order to send such messages, the conversation must be initiated by the user. https://developers.facebook.com/docs/whatsapp/conversation-types

enter image description here

Upvotes: 0

Jordan Kalebu
Jordan Kalebu

Reputation: 72

Have look at Heyooh, it's a Javascript Wrapper for WhatsApp Cloud API

Installation

npm install heyooh

Here how sending to send messages;

import WhatsApp from heyhooh
let messenger = new WhatsApp('TOKEN',  phone_number_id='104xxxxxx')
messenger.send_template("hello_world", "255757xxxxxx")

Upvotes: 0

Jones Heckler
Jones Heckler

Reputation: 11

See this project...

https://github.com/pro-cms/whatsappcloud-php

This work for me :)

Upvotes: 1

Leandro Ascierto
Leandro Ascierto

Reputation: 11

The same thing happens to me, the answer is correct but the message does not arrive, only the example of the Hellow_Word template is working, the others are not.

Upvotes: 1

Related Questions