Created
August 28, 2019 18:08
-
-
Save ekpangmichael/f575645761c361866d87f5bb1d301ccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you make it to return more results