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
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.
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