Skip to content

Instantly share code, notes, and snippets.

@ekpangmichael
Created September 9, 2019 10:59
Show Gist options
  • Save ekpangmichael/6f3f10ebecffd5e79c76fc45502cba1d to your computer and use it in GitHub Desktop.
Save ekpangmichael/6f3f10ebecffd5e79c76fc45502cba1d to your computer and use it in GitHub Desktop.
class WhatsappBot {
/**
* @memberof WhatsappBot
* @param {object} req - Request sent to the route
* @param {object} res - Response sent from the controller
* @param {object} next - Error handler
* @returns {object} - object representing response message
*/
static async googleSearch(req, res, next) {
const twiml = new MessagingResponse();
const q = req.body.Body;
const options = { cx, q, auth: googleApiKey };
try {
const result = await customsearch.cse.list(options);
const firstResult = result.data.items[0];
const searchData = firstResult.snippet;
const link = firstResult.link;
twiml.message(`${searchData} ${link}`);
res.set('Content-Type', 'text/xml');
return res.status(200).send(twiml.toString());
} catch (error) {
return next(error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment