Skip to content

Commit

Permalink
Merge pull request #9 from bkimminich/easter-eggs
Browse files Browse the repository at this point in the history
Add support for custom actions and callbacks
  • Loading branch information
bkimminich committed Aug 20, 2020
2 parents 1d13861 + b065c9a commit b6301ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function train () {

function process (query, token) {
if (users.get(token)) {
return { action: 'response', body: model.process(trainingSet.lang, query) }
return model.process(trainingSet.lang, query)
} else {
return { action: 'unrecognized', body: 'user does not exist' }
}
Expand Down
20 changes: 8 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Bot {
constructor (name, greeting, trainingSet, defaultResponse) {
this.name = name
this.greeting = greeting
this.defaultResponse = defaultResponse
this.defaultResponse = { action: 'response', body: defaultResponse }
this.training = {
state: false,
data: trainingSet
Expand Down Expand Up @@ -40,19 +40,15 @@ class Bot {
}

async respond (query, token) {
const response = this.factory.run(`process("${query}", "${token}")`)
let message
if (response.action === 'response') {
message = await response.body
message = message.answer
const response = (await this.factory.run(`process("${query}", "${token}")`)).answer
if (!response) {
return this.defaultResponse
} else {
message = response.body
}
if (!message) {
message = this.defaultResponse
if (response.body) {
response.body = this.render(response.body, token)
}
return response
}
message = this.render(message, token)
return { action: response.action, body: message }
}

train () {
Expand Down
27 changes: 25 additions & 2 deletions test/initialize-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const chai = require('chai')
const expect = chai.expect
const juice = require('../index')

const trainingSet = {
lang: 'en',
intents: [
Expand All @@ -23,16 +24,38 @@ const trainingSet = {
{
question: 'howdy',
intent: 'greetings.hello'
},
{
question: 'how much is X',
intent: 'queries.productprice'
},
{
question: 'how much does X cost',
intent: 'queries.productprice'
}
],
answers: [
{
intent: 'greetings.bye',
answer: 'Ok Cya'
answer: {
action: 'response',
body: 'Ok Cya'
}
},
{
intent: 'greetings.hello',
answer: 'Hello <customer-name>'
answer: {
action: 'response',
body: 'Hello <customer-name>'
}
},
{
intent: 'queries.productprice',
answer: {
action: 'function',
handler: 'productPrice',
body: ''
}
}
]
}
Expand Down

0 comments on commit b6301ba

Please sign in to comment.