Skip to content

Commit

Permalink
feat: add BotTableMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
tcnguyen committed Mar 21, 2018
1 parent 286ef0b commit eb7b44a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/botfuel-dialog/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Adapter = require('./adapters/adapter');
const Bot = require('./bot');
const BotImageMessage = require('./messages/bot-image-message');
const BotTextMessage = require('./messages/bot-text-message');
const BotTableMessage = require('./messages/bot-table-message');
const BotfuelAdapter = require('./adapters/botfuel-adapter');
const BotfuelNlu = require('./nlus/botfuel-nlu');
const Brain = require('./brains/brain');
Expand Down Expand Up @@ -61,6 +62,7 @@ module.exports = {
Bot,
BotImageMessage,
BotTextMessage,
BotTableMessage,
BotfuelAdapter,
BotfuelNlu,
Brain,
Expand Down
63 changes: 63 additions & 0 deletions packages/botfuel-dialog/src/messages/bot-table-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2017 - present, Botfuel (https://www.botfuel.io).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const Message = require('./message');
const MessageError = require('../errors/message-error');

/**
* A table message sent by the bot to the user.
* Only supported with botfuel-adapter
* @extends Message
*/
class BotTableMessage extends Message {
/**
* @constructor
* @param {Object} data - the table data. See explaination and example below.
* @param {Object} [options] - the message options
*
* data = {schema: [], rows: []}
* schema: array of { key: label } with label is the displayed title for the corresponding key
* rows: An array of objects with keys defined in schema
* example:
* data = {
* schema: [ {name: "Name"}, {city: "City"}],
* rows: [
* {name: "John", city: "New York"},
* {name: "Peter", city: "Paris"}
* ]
* }
*/
constructor(data, options) {
super('table', 'bot', data, options);
this.validate();
}

/** @inheritDoc */
validate() {
super.validate();
if (!(this.data && this.data.schema && this.data.rows)) {
throw new MessageError({
name: 'data',
message: 'bad format for data',
});
}

this.validateArray('data.schema', this.data.schema);
this.validateArray('data.rows', this.data.rows);
}
}

module.exports = BotTableMessage;
30 changes: 30 additions & 0 deletions packages/botfuel-dialog/tests/messages/bot-table-message.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2017 - present, Botfuel (https://www.botfuel.io).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const BotTableMessage = require('../../src/messages/bot-table-message');
const MessageError = require('../../src/errors/message-error');

describe('BotTableMessage', () => {
test('should throw an exception when malformed', async () => {
expect(() => new BotTableMessage(null)).toThrow(MessageError);

// no rows
expect(() => new BotTableMessage({ schema: [] })).toThrow(MessageError);

// no schema
expect(() => new BotTableMessage({ rows: [] })).toThrow(MessageError);
});
});

0 comments on commit eb7b44a

Please sign in to comment.