botkit-middleware-witai icon indicating copy to clipboard operation
botkit-middleware-witai copied to clipboard

Npm package is outdated.

Open Magellol opened this issue 9 years ago • 3 comments
trafficstars

Hey guys, just a headsup about the npm package. It looks like it's not up to date to the latest release from this repo and thus doesn't work with the current Wit API version.

To make it work, I've downloaded the code from this repo directly and included in my project.

Magellol avatar Oct 11 '16 14:10 Magellol

You can also point directly to the github repo:

npm install https://github.com/howdyai/botkit-middleware-witai.git

simpixelated avatar Nov 26 '16 17:11 simpixelated

For others who may be watching this issue, the botkit-witai middleware looks like it's more up to date.

With that library I was able to put the following together which assumes that you have already completed at least up to 4) in https://wit.ai/docs/quickstart. Also, Javascript is not my strong suit, but the code should be clear enough to get the message across.

let config = null;

try {
  config = require('./config.json');
} catch (e) {
  console.log('Unable to load ./config.json')
}

const Botkit = require('botkit');
const wit = require('botkit-witai')({
  accessToken: process.env.WIT_TOKEN || config.WIT_TOKEN,
  minConfidence: 0.4,
  logLevel: 'debug'
});

const controller = Botkit.slackbot({
  debug: false
});

const bot = controller.spawn({
  token: process.env.SLACK_TOKEN || config.SLACK_TOKEN
}).startRTM();

controller.setupWebserver(process.env.PORT || 3001, (err, webserver) => {
  controller.createWebhookEndpoints(webserver, bot);
});

controller.middleware.receive.use(wit.receive);

controller.hears(['weather'], 'direct_message', (bot, message) => {
  bot.botkit.log("Wit.ai detected entities", message.entities);
  bot.botkit.log(`Message: ${prettyJSON(message)}`);
  const intent = firstEntityValue(message.entities, 'intent');
  bot.botkit.log(`Intent: ${intent}`);
  const location = firstEntityValue(message.entities, 'location');
  bot.botkit.log(`Location: ${location}`);
  bot.reply(message, `You want to know more about ${intent} for ${location} correct?`);
});

const firstEntityValue = (entities, entity) => {
  const val = entities && entities[entity] &&
    Array.isArray(entities[entity]) &&
    entities[entity].length > 0 &&
    entities[entity][0].value
  ;
  if (!val) {
    return null;
  }
  return typeof val === 'object' ? val.value : val;
};

function prettyJSON(obj) {
  return JSON.stringify(obj, null, 2);
}

jason-riddle avatar Jan 02 '17 04:01 jason-riddle

botkit-witai is currently broken due to changes on how intents are handled.

michaelmior avatar Jul 16 '18 17:07 michaelmior