howdyai / botkit-middleware-witai

Middleware for using Wit.ai with Botkit-powered bots
MIT License
87 stars 49 forks source link

Npm package is outdated. #17

Open Magellol opened 8 years ago

Magellol commented 8 years ago

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.

simpixelated commented 8 years ago

You can also point directly to the github repo:

npm install https://github.com/howdyai/botkit-middleware-witai.git
jason-riddle commented 7 years ago

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);
}
michaelmior commented 6 years ago

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