Skip to content

Commit

Permalink
feat(utils): add methods to create message buttons
Browse files Browse the repository at this point in the history
add utility methods to create web_url and postback buttons in bot messages
  • Loading branch information
Antonio Pintus committed Jan 30, 2019
1 parent 052a5ff commit 0a9ff89
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 161 deletions.
86 changes: 15 additions & 71 deletions examples/dummy-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ manager.registerAgent('custom', async (msg) => {
response.context['token'] = msg.environment.token;
}
response.messages = [
index_1.BotMessage.createSimpleTextMessage('Hello! I am a DummyBot from Bot SDK 3.2.0 😎'),
index_1.BotMessage.createSimpleTextMessage('Hello! I am a DummyBot from Bot SDK 3.3.0 😎'),
{
code: 'message',
type: 'text',
Expand Down Expand Up @@ -187,76 +187,20 @@ manager.registerAgent('custom', async (msg) => {
{
title: 'Here is the full list of commands you can send me',
buttons: [
{
type: 'postback',
title: 'quick',
payload: 'quick'
},
{
type: 'postback',
title: 'team',
payload: 'team'
},
{
type: 'postback',
title: 'cat',
payload: 'cat'
},
{
type: 'postback',
title: 'cats',
payload: 'cats'
},
{
type: 'postback',
title: 'code',
payload: 'code'
},
{
type: 'postback',
title: 'async',
payload: 'async'
},
{
type: 'postback',
title: 'attach',
payload: 'attach'
},
{
type: 'postback',
title: 'up-attach-file',
payload: 'up-attach-file'
},
{
type: 'postback',
title: 'up-attach-url',
payload: 'up-attach-url'
},
{
type: 'postback',
title: 'send-action',
payload: 'send-action'
},
{
type: 'postback',
title: 'send-is-writing',
payload: 'send-is-writing'
},
{
type: 'postback',
title: 'transfer',
payload: 'trasfer'
},
{
type: 'postback',
title: 'transfer-with-data',
payload: 'transfer-with-data'
},
{
type: 'postback',
title: 'bye',
payload: 'bye'
}
index_1.BotMessage.createPostbackButton('quick', 'quick'),
index_1.BotMessage.createPostbackButton('team', 'team'),
index_1.BotMessage.createPostbackButton('cat', 'cat'),
index_1.BotMessage.createPostbackButton('cats', 'cats'),
index_1.BotMessage.createPostbackButton('code', 'code'),
index_1.BotMessage.createPostbackButton('async', 'async'),
index_1.BotMessage.createPostbackButton('attach', 'attach'),
index_1.BotMessage.createPostbackButton('up-attach-file', 'up-attach-file'),
index_1.BotMessage.createPostbackButton('up-attach-url', 'up-attach-url'),
index_1.BotMessage.createPostbackButton('send-action', 'send-action'),
index_1.BotMessage.createPostbackButton('send-is-writing', 'send-is-writing'),
index_1.BotMessage.createPostbackButton('transfer', 'transfer'),
index_1.BotMessage.createPostbackButton('transfer-with-data', 'transfer-with-data'),
index_1.BotMessage.createPostbackButton('bye', 'bye')
]
}
] // end elements
Expand Down
86 changes: 15 additions & 71 deletions examples/dummy-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ manager.registerAgent(
response.context['token'] = (msg.environment as any).token;
}
response.messages = [
BotMessage.createSimpleTextMessage('Hello! I am a DummyBot from Bot SDK 3.2.0 😎'),
BotMessage.createSimpleTextMessage('Hello! I am a DummyBot from Bot SDK 3.3.0 😎'),
{
code: 'message',
type: 'text',
Expand Down Expand Up @@ -193,76 +193,20 @@ manager.registerAgent(
{
title: 'Here is the full list of commands you can send me',
buttons: [
{
type: 'postback',
title: 'quick',
payload: 'quick'
},
{
type: 'postback',
title: 'team',
payload: 'team'
},
{
type: 'postback',
title: 'cat',
payload: 'cat'
},
{
type: 'postback',
title: 'cats',
payload: 'cats'
},
{
type: 'postback',
title: 'code',
payload: 'code'
},
{
type: 'postback',
title: 'async',
payload: 'async'
},
{
type: 'postback',
title: 'attach',
payload: 'attach'
},
{
type: 'postback',
title: 'up-attach-file',
payload: 'up-attach-file'
},
{
type: 'postback',
title: 'up-attach-url',
payload: 'up-attach-url'
},
{
type: 'postback',
title: 'send-action',
payload: 'send-action'
},
{
type: 'postback',
title: 'send-is-writing',
payload: 'send-is-writing'
},
{
type: 'postback',
title: 'transfer',
payload: 'trasfer'
},
{
type: 'postback',
title: 'transfer-with-data',
payload: 'transfer-with-data'
},
{
type: 'postback',
title: 'bye',
payload: 'bye'
}
BotMessage.createPostbackButton('quick', 'quick'),
BotMessage.createPostbackButton('team', 'team'),
BotMessage.createPostbackButton('cat', 'cat'),
BotMessage.createPostbackButton('cats', 'cats'),
BotMessage.createPostbackButton('code', 'code'),
BotMessage.createPostbackButton('async', 'async'),
BotMessage.createPostbackButton('attach', 'attach'),
BotMessage.createPostbackButton('up-attach-file', 'up-attach-file'),
BotMessage.createPostbackButton('up-attach-url', 'up-attach-url'),
BotMessage.createPostbackButton('send-action', 'send-action'),
BotMessage.createPostbackButton('send-is-writing', 'send-is-writing'),
BotMessage.createPostbackButton('transfer', 'transfer'),
BotMessage.createPostbackButton('transfer-with-data', 'transfer-with-data'),
BotMessage.createPostbackButton('bye', 'bye')
]
}
] // end elements
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"clean:all": "npm run clean && rimraf node_modules",
"prebuild": "rimraf dist",
"build": "tsc",
"build:examples": "(cd examples/lambda-bot-manager && npm i && npm run build) && (cd examples/lambda-bot-filter && npm i && npm run build)",
"build:examples": "tsc -p examples && (cd examples/lambda-bot-manager && npm i && npm run build) && (cd examples/lambda-bot-filter && npm i && npm run build)",
"build:all": "npm run build && tsc -p test && npm run build:examples",
"commit": "git-cz",
"check-coverage": "./node_modules/.bin/nyc check-coverage --statements 90 --branches 60 --functions 80 --lines 90",
Expand Down
2 changes: 1 addition & 1 deletion src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface BotAgentRegistry {
}

export class BotAgentManager extends API {
constructor(version: string = '3.2.0', title: string = 'Vivocha BotAgentManager API') {
constructor(version: string = '3.3.0', title: string = 'Vivocha BotAgentManager API') {
super({
swagger: '2.0',
info: {
Expand Down
2 changes: 1 addition & 1 deletion src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class BotFilter extends API {
swagger: '2.0',
info: {
title: 'Vivocha BotFilter API',
version: '3.2.0'
version: '3.3.0'
},
paths: {}
});
Expand Down
38 changes: 38 additions & 0 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ export interface QuickReply {
payload?: string | number;
image_url?: string;
}
export interface WebUrlButton {
type: 'web_url';
title: string;
url: string;
}
export interface PostbackButton {
type: 'postback';
title: string;
payload: string;
}

/**
* Utility class exposing static methods to compose common used Vivocha Bot Messages.
*
* @export
* @class BotMessage
*/
export class BotMessage {
public static createSimpleTextMessage(body: string): TextMessage {
if (!body) {
Expand Down Expand Up @@ -62,4 +78,26 @@ export class BotMessage {
});
}
}
public static createWebUrlButton(title: string, url: string): WebUrlButton {
if (!title || !url) {
throw new Error('In a WebURLButton, title and url are required');
} else {
return {
type: 'web_url',
title,
url
};
}
}
public static createPostbackButton(title: string, payload: string): PostbackButton {
if (!title || !payload) {
throw new Error('In a PostbackButton, title and payload are required');
} else {
return {
type: 'postback',
title,
payload
};
}
}
}
89 changes: 89 additions & 0 deletions test/ts/agent-general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as chai from 'chai';
import { BotAgentManager, BotRequest } from '../../dist';
import * as http from 'request-promise-native';
import { dummyBot } from './dummy-bot';
import bodyParser = require('body-parser');

chai.should();

Expand Down Expand Up @@ -171,6 +172,58 @@ describe('Testing a generic Bot Agent (Dummy Bot) ', function() {
result1.messages[0].quick_replies.should.have.lengthOf(3);
return;
});
it('sending ciao, it should reply with a text message', async function() {
const request1: BotRequest = {
language: 'en',
event: 'continue',
settings: getSettings(),
message: getTextMessage('ciao')
};
//console.dir(request1, { colors: true, depth: 20 });
const result1 = await http(getHTTPOptions(request1));
//console.dir(result1, { colors: true, depth: 20 });
result1.event.should.equal('continue');
result1.messages.should.have.lengthOf(1);
result1.messages[0].should.deep.equal({
code: 'message',
type: 'text',
body: 'hello :)'
});
return;
});
it('sending quick2, it should reply with three quick_replies', async function() {
const request1: BotRequest = {
language: 'en',
event: 'continue',
settings: getSettings(),
message: getTextMessage('quick2')
};
//console.dir(request1, { colors: true, depth: 20 });
const result1 = await http(getHTTPOptions(request1));
//console.dir(result1, { colors: true, depth: 20 });
result1.event.should.equal('continue');
result1.messages.should.have.lengthOf(1);
result1.messages[0].body.should.contain('Just an example of quick replies... choose a color?');
result1.messages[0].quick_replies.should.have.lengthOf(3);
result1.messages[0].quick_replies.should.deep.equal([
{
content_type: 'text',
title: 'Red',
payload: 'red'
},
{
content_type: 'text',
title: 'Blue',
payload: 'blue'
},
{
content_type: 'text',
title: 'White',
payload: 'white'
}
]);
return;
});
it('sending cat, it should reply with a template', async function() {
const request1: BotRequest = {
language: 'en',
Expand All @@ -194,6 +247,42 @@ describe('Testing a generic Bot Agent (Dummy Bot) ', function() {
template.elements[0].should.have.property('buttons');
return;
});

it('sending cat2, it should reply with a template', async function() {
const request1: BotRequest = {
language: 'en',
event: 'continue',
settings: getSettings(),
message: getTextMessage('cat2')
};
//console.dir(request1, { colors: true, depth: 20 });
const result1 = await http(getHTTPOptions(request1));
//console.dir(result1, { colors: true, depth: 20 });
result1.event.should.equal('continue');
result1.messages.should.have.lengthOf(1);
result1.messages[0].should.have.property('template');
const template = result1.messages[0].template;
template.type.should.equal('generic');
template.elements.should.have.lengthOf(1);
template.elements[0].title.should.equal('Meow2!');
template.elements[0].should.have.property('image_url');
template.elements[0].should.have.property('subtitle');
template.elements[0].should.have.property('default_action');
template.elements[0].should.have.property('buttons');
const buttons = template.elements[0].buttons;
buttons[0].should.deep.equal({
type: 'web_url',
url: 'https://en.wikipedia.org/wiki/Cat',
title: 'View Website'
});
buttons[1].should.deep.equal({
type: 'postback',
title: 'OK',
payload: 'abcd 123'
});
return;
});

it('should fail when sending a malformed message', async function() {
const request1: BotRequest = {
language: 'en',
Expand Down
Loading

0 comments on commit 0a9ff89

Please sign in to comment.