Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #1

Merged
merged 5 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/SLACK_MESSAGE_MESSAGE_BUILDER.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ _Arguments_:
| addTimestamp | No | timestamp (Date, required) | `this` for chaining | Adds a timestamp for the latest attachment |
| addField | No | title (string, required), value (string, required), isShort (boolean, optional) | `this` for chaining | Adds a field to the latest attachment |
| addAction | No | text (string, required), name (string, required), value (string, required), style (string, optional) | `this` for chaining | Adds an action button to the latest attachment, you can add up to 5 buttons per attachment, style can be 'primary' or 'danger' |
| addLinkButton | No | text (string, required), url (url, required), style (string, optional) | `this` for chaining | Adds a URL button to the latest attachment, you can add up to 5 buttons per attachment, style can be 'primary' or 'danger' |
| getLatestAction | No | No args. | `this` for chaining | Returns the latest action, for internal use |
| addConfirmation | No | title (string, required), text (string, required), okLabel (string, optional), dismissLabel (string, optional) | `this` for chaining | Adds a confimation popup for the latest action, default labels are 'Ok' and 'Dismiss' |
| get | Yes | No args. | `this` for chaining | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Slack |
Expand Down
24 changes: 24 additions & 0 deletions lib/slack/format-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ class SlackTemplate {
return this;
}

addLinkButton(text, url, style) {
if (this.getLatestAttachment().actions.length === 5)
throw new Error('You can not add more than 5 actions and link buttons');

if (!text || !url)
throw new Error('Text and URL are requeired for addLinkButton method');

if (!isUrl(url))
throw new Error('URL need to be a valid');

const action = {
type: 'button',
text: text,
url: url
};

if (style)
action.style = style;

this.getLatestAttachment().actions.push(action);

return this;
}

getLatestAction() {
const actions = this.getLatestAttachment().actions;

Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "claudia-bot-builder",
"version": "4.0.0",
"version": "4.1.0",
"description": "Create chat-bots for various platforms and deploy to AWS Lambda quickly",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 25 additions & 0 deletions spec/slack/slack-format-message-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,31 @@ describe('Slack format message', () => {
.addAction('A5', 'foo', 'bar');
expect(() => message.addAction('A6', 'foo', 'bar')).toThrowError('You can not add more than 5 actions');
});

it('should throw an error if you addLinkButton without valid data', () => {
let message = new formatSlackMessage().addAttachment();
expect(() => message.addLinkButton()).toThrowError('Text and URL are requeired for addLinkButton method');
});

it('should throw an error if you addLinkButton without valid data', () => {
let message = new formatSlackMessage().addAttachment();
expect(() => message.addLinkButton('Button name')).toThrowError('Text and URL are requeired for addLinkButton method');
});

it('should throw an error if you addLinkButton without valid data', () => {
let message = new formatSlackMessage().addAttachment();
expect(() => message.addLinkButton('Button Name', 'foo')).toThrowError('URL need to be a valid');
});

it('should throw an error if you try to add more than 5 actions and link buttons', () => {
let message = new formatSlackMessage().addAttachment()
.addAction('A1', 'foo', 'bar')
.addAction('A2', 'foo', 'bar')
.addAction('A3', 'foo', 'bar')
.addAction('A4', 'foo', 'bar')
.addAction('A5', 'foo', 'bar');
expect(() => message.addLinkButton('Button', 'http://foo.bar')).toThrowError('You can not add more than 5 actions and link buttons');
});

it('should throw an error if you try to add confirmation before adding an action', () => {
let message = new formatSlackMessage().addAttachment();
Expand Down