百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

witbot

> 编程语言
开源

使用 wit.ai 创建机器人的简单 Node.js

100 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

使用 wit.ai 创建机器人的简单 Node.js

Witbot - a simple Wit interface for bots

Wit.ai is a really approachable API for adding Natural Language Processing (NLP) capabilities to bots. It works great for Slackbots that run on Beep Boop

Install

npm install --save witbot

Usage

Initialize

var Witbot = require('witbot')
var witbot = Witbot(witToken)

Wire up

Witbot doesn't assume any bot framework or even Slack but you have to wire up message events or whatever text will trigger calls to Wit.ai. The first parameter to process is the text for Wit to consider. The remaining arguments will be passed to the handlers your register for each Wit intent.

witbot.process(text, any, additional, arguments)

For example, if you're using botkit with Slack and you want Wit.ai to process messages for all direct messages and direct mentions:

controller.hears('.*', 'direct_message,direct_mention', function (bot, message) {
  var wit = witbot.process(message.text, bot, message)
})

Register Intent handlers

Use hears to receive a message that matches an intent for messages you process:

wit.hears(intent_name, confidence, callback)

hears expects these parameters:

  • intent_name is the name of a Wit intent

  • confidence is the minimum confidence (e.g. 0.6) between 0 and 1

  • callback is a function to fire when the intent is matched. Callback will be passed the parameters you registered with process and then an outcome parameter

    wit.hears('greeting', 0.5, function (bot, message, outcome) { bot.reply(message, 'Hello to you as well!') })

outcome has the following properties:

  • _text is the original text passed to wit
  • confidence is a number between 0 and 1
  • intent is the first intent that was matched
  • entities see the Wit API to learn more about matched entities

For example, if you call process with foo and bar:

var wit = witbot.process(text, foo, bar)

Then register intent handlers that expect those parameters in the callback:

wit.hears(hello, 0.5, function (foo, bar, outcome) {
  // use foo, bar and outcome
})

Match any intent with any

wit.any(function (foo, bar, outcome) { // access intent through outcome.intent })

Catch-All with otherwise

If you're processing incoming messages with witbot and want to provide a catch-all for unmatched intents and/or no intents, use witbot.otherwise like this:

wit.otherwise(function (foo, bar) {
  // use foo, bar and outcome
})

Your otherwise callback will pass along the same parameters you registered with process

Chaining

You can chain all of the witbot calls for convenience. For example you could do

witbot.process(response.text)
  .hears('good', 0.5, function (outcome) {
    convo.say('I am so glad to hear it!')
    convo.next()
  })
  .hears('bad', 0.5, function (outcome) {
    convo.say('I\'m sorry, that is terrible')
    convo.next()
  })
  .otherwise(function (outcome) {
    convo.say('I\'m cofused')
    convo.repeat()
    convo.next()
  })

Example

Here's a full example using botkit including a nested conversation flow and this sample wit.ai project

var Botkit = require('botkit')
var Witbot = require('../')

var slackToken = process.env.SLACK_TOKEN
var witbot = Witbot(process.env.WIT_TOKEN)
var controller = Botkit.slackbot({ debug: false })

controller.spawn({ token: slackToken }).startRTM(function (err, bot, payload) {
  if (err) throw new Error('Error connecting to Slack: ', err)
  console.log('Connected to Slack')
})

// wire up DMs and direct mentions to wit.ai
controller.hears('.*', 'direct_message,direct_mention', function (bot, message) {
  var wit = witbot.process(message.text, bot, message)

  wit.hears('hello', 0.53, function (bot, message, outcome) {
    bot.startConversation(message, function (_, convo) {
      convo.say('Hello!')
      convo.ask('How are you?', function (response, convo) {
        witbot.process(response.text)
          .hears('good', 0.5, function (outcome) {
            convo.say('I am so glad to hear it!')
            convo.next()
          })
          .hears('bad', 0.5, function (outcome) {
            convo.say('I\'m sorry, that is terrible')
            convo.next()
          })
          .otherwise(function (outcome) {
            convo.say('I\'m cofused')
            convo.repeat()
            convo.next()
          })
      })
    })
  })

  wit.otherwise(function (bot, message) {
    bot.reply(message, 'You are so intelligent, and I am so simple. I don\'t understnd')
  })
})

Don't forget to install this module and botkit:

bash
npm install --save botkit witbot

Issues· 3 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言