├── .gitignore ├── CONTRIBUTING.md ├── package.json ├── readme.md └── src └── middleware.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Instructions for Contributing Code 2 | 3 | This project welcomes contributions and suggestions. Most contributions require you to agree to a 4 | Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us 5 | the rights to use your contribution. For details, visit https://cla.microsoft.com. 6 | 7 | When you submit a pull request, a CLA-bot will automatically determine whether you need to provide 8 | a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions 9 | provided by the bot. You will only need to do this once across all repos using our CLA. 10 | 11 | When you submit a pull request, a CLA-bot will automatically determine whether you need to provide 12 | a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions 13 | provided by the bot. You will only need to do this once across all repos using our CLA. 14 | 15 | ## Code of Conduct 16 | 17 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "botkit-rasa", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "src/middleware.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "ben@howdy.ai", 10 | "license": "ISC", 11 | "dependencies": { 12 | "debug": "^2.6.8", 13 | "request": "^2.81.0", 14 | "request-promise": "^4.2.1" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Botkit / rasa NLU plugin 2 | 3 | This plugin provides Botkit developers a way to use the [rasa NLU](https://rasa.ai/) open source, self hosted natural language API. 4 | 5 | 6 | ``` 7 | var rasa = require('botkit-rasa')({rasa_uri: 'http://localhost:5000'}); 8 | controller.middleware.receive.use(rasa.receive); 9 | 10 | controller.hears(['my_intent'],'message_received', rasa.hears, function(bot, message) { 11 | 12 | console.log('Intent:', message.intent); 13 | console.log('Entities:', message.entities); 14 | 15 | }); 16 | ``` 17 | -------------------------------------------------------------------------------- /src/middleware.js: -------------------------------------------------------------------------------- 1 | const request = require('request-promise') 2 | const debug = require('debug')('botkit:rasa') 3 | 4 | module.exports = config => { 5 | if (!config) { 6 | config = {} 7 | } 8 | 9 | if (!config.rasa_uri) { 10 | config.rasa_uri = 'http://localhost:5000' 11 | } 12 | 13 | var middleware = { 14 | receive: (bot, message, next) => { 15 | if (!message.text || message.is_echo) { 16 | next() 17 | return 18 | } 19 | 20 | debug('Sending message to Rasa', message.text) 21 | const options = { 22 | method: 'POST', 23 | uri: `${config.rasa_uri}/parse`, 24 | body: { 25 | q: message.text 26 | }, 27 | json: true 28 | } 29 | 30 | request(options) 31 | .then(response => { 32 | debug('Rasa response', response) 33 | message.intent = response.intent 34 | message.entities = response.entities 35 | next() 36 | }) 37 | }, 38 | 39 | hears: (patterns, message) => { 40 | return patterns.some(pattern => { 41 | if (message.intent.name === pattern) { 42 | debug('Rasa intent matched hear pattern', message.intent, pattern) 43 | return true 44 | } 45 | }) 46 | } 47 | 48 | } 49 | return middleware 50 | } 51 | --------------------------------------------------------------------------------