Skip to content

Instantly share code, notes, and snippets.

@ekpangmichael
Created August 28, 2019 18:08
Show Gist options
  • Save ekpangmichael/f575645761c361866d87f5bb1d301ccc to your computer and use it in GitHub Desktop.
Save ekpangmichael/f575645761c361866d87f5bb1d301ccc to your computer and use it in GitHub Desktop.
import { google } from 'googleapis';
import dotenv from 'dotenv';
import twilio from 'twilio';
dotenv.config();
const {
SID: accountSid,
KEY: TwilloAuthToken,
APIKEY: googleApiKey,
CX: cx
} = process.env;
twilio(accountSid, TwilloAuthToken);
const { MessagingResponse } = twilio.twiml;
const customsearch = google.customsearch('v1');
/**
* @class WhatsappBot
* @description class will implement bot functionality
*/
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);
}
}
}
export default WhatsappBot;
@gitnyasha
Copy link

can you make it to return more results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment