Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Adding middleware to talk to rasa core and get back responses. Works … #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/botkitRasaCore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const request = require('request-promise')
const debug = require('debug')('botkit:rasa')

module.exports = config => {
if (!config) {
config = {}
}

if (!config.rasa_uri) {
config.rasa_uri = 'http://localhost:5000'
}

var middleware = {
receive: (bot, message, next) => {
if (!message.text || message.is_echo) {
next()
return
}

debug('Sending message to Rasa', message.text)
const options = {
method: 'POST',
uri: `${config.rasa_uri}/conversations/default/respond`,
body: {
query: message.text
},
json: true
}

request(options)
.then(response => {
debug('Rasa response', response)
if(response && response[0]) {
var coreResponse = JSON.stringify(response[0])
var parsedResponse = JSON.parse(coreResponse)
message.recipient_id = response.recipient_id
message.text = response.text
}
else {
message.recipient_id = 'default'
message.text = 'Sorry, the bot could not parse the response'
}
next()
})
},

hears: (patterns, message) => {
return patterns.some(pattern => {
if (message.recipient_id === 'default') {
debug('Rasa core response matched the recipient id', message.recipient_id, pattern)
return true
}
})
}

}
return middleware
}