# telegraf-wit

> wit.ai middleware for Telegraf

Latest version **2.1.0** (published 2016-10-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install telegraf-wit
pnpm add telegraf-wit
yarn add telegraf-wit
bun add telegraf-wit
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2016-10-03 |
| First published | 2016-05-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.2.4 |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 35 |
| Author | Vitaly Domnikov |
| Maintainers | dotcypress |
| Keywords | telegram, telegram bot, telegraf, bot, wit, wit.ai, natural language, nlp, wit |

## Links

- npm: https://www.npmjs.com/package/telegraf-wit
- Repository: https://github.com/telegraf/telegraf-wit
- Homepage: https://github.com/telegraf/telegraf-wit#readme
- Issues: https://github.com/telegraf/telegraf-wit/issues
- npm.io page: https://npm.io/package/telegraf-wit

## Dependencies (3)

- [qs](https://npm.io/package/qs.md) ^6.1.0
- [debug](https://npm.io/package/debug.md) ^2.2.0
- [node-fetch](https://npm.io/package/node-fetch.md) ^1.5.1

## Recent versions

- 2.1.0 (latest) — 2016-10-03
- 2.0.0 — 2016-08-06
- 1.0.0 — 2016-05-24
- 0.5.1 — 2016-05-15
- 0.5.0 — 2016-05-15
- 0.4.0 — 2016-05-09
- 0.2.0 — 2016-05-07
- 0.1.0 — 2016-05-07
- 0.0.0 — 2016-05-07

## README

[![Build Status](https://img.shields.io/travis/telegraf/telegraf-wit.svg?branch=master&style=flat-square)](https://travis-ci.org/telegraf/telegraf-wit)
[![NPM Version](https://img.shields.io/npm/v/telegraf-wit.svg?style=flat-square)](https://www.npmjs.com/package/telegraf-wit)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)

# Telegraf + wit.ai 

[wit.ai](https://wit.ai/) middleware for [Telegraf](https://github.com/telegraf/telegraf).

> Easily create text bots that humans can chat with on their preferred messaging platform.
>
> -- <cite>wit.ai</cite>

## Installation

```js
$ npm install telegraf-wit
```

## Message processing example
  
```js
const Telegraf = require('telegraf')
const TelegrafWit = require('telegraf-wit')

const telegraf = new Telegraf(process.env.BOT_TOKEN)
const wit = new TelegrafWit(process.env.WIT_TOKEN)

telegraf.on('text', (ctx) => {
  return wit.getMeaning(ctx.message.text)
    .then((result) => {
      // reply to user with wit result
      return ctx.reply(JSON.stringify(result, null, 2))
    })
})

telegraf.startPolling()

```

## Story processing example
  
```js
const Telegraf = require('telegraf')
const TelegrafWit = require('telegraf-wit')

const telegraf = new Telegraf(process.env.BOT_TOKEN)
const wit = new TelegrafWit(process.env.WIT_TOKEN)

// Session for storing story context
telegraf.use(Telegraf.memorySession())

// Add wit conversation middleware
telegraf.use(wit.middleware())

// Merge handlers
wit.on('merge', (ctx) => {
  ctx.wit.context.city = firstEntityValue(ctx.wit.entities, 'location')
})

// Message handlers
wit.on('message', (ctx) => ctx.reply(ctx.wit.message))

// Action handlers
wit.on('get-forecast', (ctx) => {
  if (ctx.wit.confidence > 0.02) {
    ctx.wit.context.forecast = 'As usual :)'
  }
})

telegraf.startPolling()

```

There are some other [examples](https://github.com/telegraf/telegraf-wit/tree/master/examples).


## Error Handling

By default TelegrafWit will print all wit errors to stderr. 
To perform custom error-handling logic you can set `onError` handler:

```js
wit.on('error', (ctx) => {
  console.error('wit error', err)
}
```

## API

* `TelegrafWit`
  * [`new TelegrafWit(token)`](#new)
  * [`.meaning(message, outcomes, context)`](#meaning)
  * [`.on(action, actionName, fn, [fn, ...])`](#on)
  * [`.middleware()`](#middleware)
 
<a name="new"></a>
#### `TelegrafWit.new(token)`

Initialize new TelegrafWit.

| Param | Type | Description |
| --- | --- | --- |
| token | `String` | Wit Token |

* * *

<a name="meaning"></a>
#### `TelegrafWit.meaning(message, msgId, threadId, context)` => `Promise`

Returns the extracted meaning from a sentence, based on the context. 

| Param | Type | Description |
| ---  | --- | --- |
| message | `String` | User message |
| messageId | `String` | Message id |
| threadId | `String` | Thread id |
| context | `Object`(Optional) | User’s context |

* * *

<a name="on"></a>
#### `TelegrafWit.on(action, fn, [fn, ...])`

Adds merge handlers to app

| Param | Type | Description |
| ---  | --- | --- |
| action  | `String` | action type(merge, message, %function name%) |
| fn  | `Function` | Middleware |

* * *

## Telegraf context

Telegraf user context props:

```js
wit.on('message', (ctx) => {
  ctx.wit.context      // wit context
  ctx.wit.confidence   // confidence
  ctx.wit.message      // wit message
  ctx.wit.entities     // entities
  ctx.wit.quickReplies // Quick replies
});
```

---
_Source: https://npm.io/package/telegraf-wit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
