Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function Witbot (witToken) {
var self = this
const actions = {
say(sessionId, context, message, cb) {
cb();
},
merge(sessionId, context, entities, message, cb) {
cb(context);
},
error(sessionId, context, error) {
console.log(error.message);
},
};
self._wit = new Wit(witToken, actions)
// process text with Wit.ai.
// 1st argument is the text of the message
// Remaining arguments will be passed as the first arguments of each registered callback
self.process = function (text, context) {
var args = Array.prototype.slice.call(arguments)
var intents = new Intents()
var matched = false
args.shift()
args.shift()
self._wit.message(text, context, function (err, res) {
if (err) return console.error('Wit.ai Error: ', err)
// only consider the 1st outcome
if (res.outcomes && res.outcomes.length > 0) {
var outcome = res.outcomes[0]
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
const isAnOption = (options, text) => options.some(option => option === text)
export const createTextQuickReplies = (options) => options.map(option => new QuickReply({
title: option,
content_type: 'text',
payload: 'empty'
}))
//
// Initialize Wit.ai
// ---
// API Reference: https://wit.ai/docs
//
export const wit = new Wit({
accessToken: WIT_TOKEN,
actions: actions,
logger: new log.Logger(log.INFO)
})
// BOT TESTING MODE
if (TESTING_MODE) {
console.log('Bot testing mode!')
dbConnect()
interactive(wit)
}
var sessions = {};
const findOrCreateSession = (sessions, fbid, cb) => {
if (!sessions[fbid]) {
console.log("New Session for:", fbid);
sessions[fbid] = {context: {}};
}
cb(sessions, fbid);
};
// Wit.ai bot specific code
// Import our bot actions and setting everything up
const actions = require('./wit.actions');
const wit = new Wit(config.WIT_TOKEN, actions);
// Starting our webserver and putting it all together
const app = express();
app.set('port', PORT);
app.listen(app.get('port'));
app.use(bodyParser.json());
// Webhook setup
app.get('/', (req, res) => {
if (!config.FB_VERIFY_TOKEN) {
throw new Error('missing FB_VERIFY_TOKEN');
}
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === config.FB_VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
} else {
const express = require('express');
const Wit = require('node-wit').Wit;
const settings = require('./settings.js');
// Webserver parameter
const PORT = process.env.PORT || 8445;
// Messenger Helper Functions
const fbMessage = require('./fb-connect.js').fbMessage;
const getFirstMessagingEntry = require('./parser.js').getFirstMessagingEntry;
// Bot Stuff
const findOrCreateSession = require('./sessions.js').findOrCreateSession;
const actions = require('./bot.js').actions;
const sessions = require('./sessions.js').sessions;
const wit = new Wit(settings.WIT_TOKEN, actions);
// Starting our webserver and putting it all together
const app = express();
app.set('port', PORT);
app.listen(app.get('port'));
app.use(bodyParser.json());
// Webhook setup
app.get('/fb', (req, res) => {
if (!settings.FB_VERIFY_TOKEN) {
throw new Error('missing FB_VERIFY_TOKEN');
}
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === settings.FB_VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
context.temp_max = Math.round(result.main.temp_max * 9/5 - 459.67);
context.humidity = result.main.humidity;
cb(context);
});
},
};
const apiCall = (location) => {
return axios.get(`http://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${OPEN_WEATHER_TOKEN}`).then((res) => {
return res.data;
})
}
// Setting up our bot
const wit = new Wit(WIT_TOKEN, actions);
// Starting our webserver and putting it all together
const app = express();
app.set('port', PORT);
app.listen(app.get('port'));
app.use(bodyParser.json());
// Webhook setup
app.get('/fb', (req, res) => {
if (!FB_VERIFY_TOKEN) {
throw new Error('missing FB_VERIFY_TOKEN');
}
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === FB_VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
} else {
return new Promise(function(resolve, reject) {
var location = firstEntityValue(entities, 'location')
if (location) {
context.forecast = 'sunny in ' + location; // we should call a weather API here
delete context.missingLocation;
} else {
context.missingLocation = true;
delete context.forecast;
}
return resolve(context);
});
},
};
const client = new Wit({
accessToken: wit_token,
actions: actions
});
interactive(client);
var getWit = function () {
console.log('GRABBING WIT')
return new Wit(Config.WIT_TOKEN, actions)
}
const getWit = () => {
return new Wit(Config.WIT_TOKEN, actions);
};
"use strict";
const { Wit, log } = require("node-wit");
const config = require("../config/config-loader").load();
const client = new Wit({
accessToken: config.wit.token,
logger: new log.Logger(log.DEBUG)
});
module.exports = client;