Integrating whatsapp Cloud Api using External api

577 views
Skip to first unread message

Aly Radwan

unread,
Aug 15, 2022, 6:19:30 PM8/15/22
to rapidpro-dev
Hello
Does anyone integrate with WhatsApp cloud API using Rapidpro external Api channel ?
thanks
Aly

Gift

unread,
Aug 24, 2022, 3:51:12 AM8/24/22
to Aly Radwan, rapidpro-dev
Yes, I did that and I have 10 WhatsApp Chatbots running using Rapidpro External API and WhatsApp Cloud API

--
You received this message because you are subscribed to the Google Groups "rapidpro-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rapidpro-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rapidpro-dev/0b82db8b-91fd-4420-8fe1-6e0506acc4cfn%40googlegroups.com.

Alex Bwanakweli

unread,
Aug 24, 2022, 3:57:55 AM8/24/22
to Gift, Aly Radwan, rapidpro-dev
Hi @mgift9319
would you please advise on to achieve that ?



--

Message has been deleted

Gift Marimo

unread,
Aug 24, 2022, 5:12:44 AM8/24/22
to Alex Bwanakweli, Aly Radwan, rapidpro-dev
1. Firstly I am running RP7.0.0 so here is what I did, when creating a External API Channel under URN Type leave as Phone Number

2. Under Phone Number put the phone number of your whatsapp chatbot starting with country code, in my case my number start with 263 for my country Zimbabwe

3. Leave method as HTTP POST

4. Change Content Type to Application/json

5. For WhatsApp I changed Max Length to 6000 instead of 160 which is suitably for SMS

7. On send URL is the webhook that accepts POST request when RapidPro want to send a message. In my case I am using glitch.com to accept request from RP then post that data to https://graph.facebook.com/v14.0/ example of send URL would be https://myurl.glitch.me/webhookrp

8. On Request Body use this Json format {"text":{{text}},"to_no_plus":{{to_no_plus}},"from_no_plus":{{from_no_plus}}}

9. On Reponse put anything like OK

Then Save the channel and it will take you to next step where it generates Receive URL, this URL is the one that receives data posted to RP and triggers a Flow.

Now in my case I am using glitch.com but you can develop your own API that can stand between RP and facebook's Graph API in this case I will demostrate with what I did.

1. Open glitch.com and create a project. The easiest way is to go to Facebook webhook documentation under this link https://developers.facebook.com/docs/whatsapp/sample-app-endpoints and click Remix On Glitch. It will automatically load basic API project that actually work.

2. to connect to RapidPro change few things there under apps.js which are:
a) Just below let msg_body = xxxxx, add your GET request to RapidPro like this
//posting data to rp
      axios({
        method: "GET",
        url: "https://your-rapidpro-receive-url/receive?from="+from+"&"+"text="+msg_body      
      }).catch((err) => {
        console.log("Error: There is an error in Post Req - " + err);
      });
    }
    res.sendStatus(200);
  } else {
    res.sendStatus(404);
  }
});

After sending data to RP, RP responds through Flows and it send data through Send URL that you set during creating the channel. This URL should be defined inside glitch so accept post request from RP and send that data to WhatsApp Cloud API like this:
//accepting POST request from RP then forward the data to WA
app.post("/webhookrp", (req, res) => {
  //Parsing request from RP
  let body = req.body
  let from = req.body.to_no_plus;
  let phone_number_id = "123456789123" //put your phone number ID here
  let msg_body = req.body.text;
 
  //after getting the data, call graph API
  axios({
        method: "POST", // Required, HTTP method, a string, e.g. POST, GET
        url:
          "https://graph.facebook.com/v14.0/" +
          phone_number_id +
          "/messages?access_token=" +
          token,
        data: {
          messaging_product: "whatsapp",
          to: from,
          text: { body: msg_body },
        },
        headers: { "Content-Type": "application/json" },
      }).catch((err) => {
        console.log("Error sending to Graph API: There is an error - " + err);
      });
});

That's it. Don't forget to add your webhook inside Meta dashboard and also copy Access token and add it under .env inside glitch.

I know that RP7.4 is coming with WhatsApp Cloud API channel but for now this is working for me.

Hope this will assist.

Regards
Gift

Alex Bwanakweli

unread,
Aug 24, 2022, 11:13:24 AM8/24/22
to Gift Marimo, Aly Radwan, rapidpro-dev
Thanks a lot. i will definitely give it a try and revert back to you 
i'm curious about how you handle whatsapp messages templates 

--

Aly Radwan

unread,
Aug 24, 2022, 2:22:02 PM8/24/22
to rapidpro-dev
Thanks very much

--
You received this message because you are subscribed to a topic in the Google Groups "rapidpro-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rapidpro-dev/7Xw670V0buk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rapidpro-dev...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages