Hello everyone, I hope you are all feeling well.
I am trying to implement a whatsapp bot that will be intermediary between users and my strapi backend. The question is that to do this I have to have an endpoint “/whatsapp” that verifies the token of the webhook and another one that allows me to receive messages in my backend.
I have implemented this in a common nodeJS project like this:
const process = require(‘…/shared/processMessage’);
const dotenv = require(‘dotenv’);
dotenv.config();
const whatsappService = require(‘…/services/whatsappService’);
const verifyToken = (req, res) =>{
try {
var accessToken = process.env.WHATSAPP_WEBHOOK_KEY;
console.log(accessToken);
var token = req.query[“hub.verify_token”];
var challenge = req.query[“hub.challenge”];
if (challenge != null && token != null && token == accessToken) {
res.send(challenge);
} else {
res.status(400).send();
}
} catch (error) {
res.status(400).send();
}
}
async function recivedMessage (req, res) {
try {
var entry = (req.body[‘entry’])[0];
var changes = (entry[‘changes’])[0];
var value = changes[‘value’];
var messageObject = value[‘messages’];
if (typeof messageObject != 'undefined'){
var messages = messageObject[0];
var number = messages['from'];
var text = getTextUser(messages);
console.log(text);
console.log(number);
await process.processMessage(text, number);
}
res.send('EVENT_RECEIVED');
} catch (e) {
res.send('EVENT_RECEIVED');
}
}
function getTextUser (messages) {
var text = ''";
var typeMessage = messages['type'];
if (typeMessage == 'text') {
text = (messages['text'])['body'];
}
else {
console.log('message invalid');
}
return text;
}
module.exports = {
verifyToken,
recivedMessage
}
try to adapt this in a satrpi driver that you create for this:
‘use strict’;
/**
- whatsapp controller
*/
const dotenv = require(‘dotenv’);
dotenv.config();
const { getTextUser } = require(‘./getTextUser’);
const { createCoreController } = require(‘@strapi/strapi’).factories;
module.exports = createCoreController(‘api::whatsapp.whatsapp’, ({strapi}) => ({
async verifyToken (ctx) {
try {
var accessToken = process.env.WHATSAPP_WEBHOOK_KEY;
var token = ctx.request.query[“hub.verify_token”];
var challenge = ctx.request.query[“hub.challenge”];
if (challenge != null && token != null && token == accessToken) {
ctx.body = challenge;
} else {
ctx.status = 400;
}
} catch (error) {
ctx.status = 400;
}
},
async recivedMessage (ctx) {
try {
var entry = (ctx.body['entry'])[0];
var changes = (entry['changes'])[0];
var value = changes['value'];
var messageObject = value['messages'];
if(typeof messageObject != 'undefined'){
var messages = messageObject[0];
var number = messages['from'];
var text = getTextUser(messages);
if (text != '') {
await process.processMessage(text, number);
}
else if (/location/.test(text)) {
//this condition is for typescript
var data = samples.sampleLocation(number);
whatsappService.sendMessageWhatsapp(data);
}
}
ctx.response.body = 'EVENT_RECEIVED';
} catch (error) {
ctx.response.body = 'EVENT_RECEIVED';;
}
}
}));
and so configure the routes in routes/custom.js:
module.exports = {
routes : [
{
method : ‘GET’,
path: ‘/whatsapp’,
handler: ‘whatsapp.verifyToken’,
config : {
auth: false,
}
},
{
method : ‘POST’,
path: ‘/whatsapp’,
handler: ‘whatsapp.recivedMessage’,
config : {
auth: false,
}
}
]
}
but when I go to my railway domain it gives me a 404 and facebook obviously doesn’t accept the url.
sorry for the long message