Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-wit' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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 {
var err = new Error('Wit.ai is not setup.'+
'Could not find access token.');
donna.logger.warn(err);
return cb(err);
}
// Get textual form of input
var data = input.getData();
// Data Types are unique and each have their own
// specific object format.
// In this case, a 'text' data type is a simple
// object with the field 'text'.
var text = data.text; // Extract text format from data
wit.captureTextIntent(accessToken, text, function (err, res) {
// console.log("Response from Wit for text input: ", res);
if (err) {
cb(err)
} else {
var outcomes = res.outcomes;
// Iterate thru all intents in response
var intents = [];
for (var i = 0, len=outcomes.length; i
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)
}
//
// 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)
}
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);
return;
})
.catch(() => {
context.missingUrl = true;
})
.then(() => {
return context;
});
}
};
// Setting up our bot
const wit = new Wit({
accessToken: config.WIT_TOKEN,
actions,
logger: new log.Logger(log.INFO)
});
var app = express();
app.set('port', config.port);
app.use(bodyParser.json({verify: bot.verifyRequestMiddleware()}));
app.get('/', function (req, res) {
res.status(200).send('Hello world');
});
/*
* Use your own validation token. Check that the token used in the Webhook
* setup is the same token used here.
*
*/
app.get('/webhook', bot.verifyBotMiddleware());