Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Aug 14, 2018
1 parent 5d71d27 commit fa4028e
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 4
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ignore modules pulled in from npm
node_modules/

# rc-apps package output
dist/

.DS_Store
17 changes: 17 additions & 0 deletions PollApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
IConfigurationExtend, ILogger,
} from '@rocket.chat/apps-ts-definition/accessors';
import { App } from '@rocket.chat/apps-ts-definition/App';
import { IAppInfo } from '@rocket.chat/apps-ts-definition/metadata';

import { PollCommand } from './command';

export class PollApp extends App {
constructor(info: IAppInfo, logger: ILogger) {
super(info, logger);
}

public async initialize(configuration: IConfigurationExtend): Promise<void> {
await configuration.slashCommands.provideSlashCommand(new PollCommand());
}
}
15 changes: 15 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "c33fa1a6-68a7-491e-bf49-9d7b99671c48",
"version": "0.0.1",
"requiredApiVersion": "^0.9.13",
"iconFile": "icon.png",
"author": {
"name": "Diego Sampaio",
"homepage": "https://sampaio.site",
"support": "https://sampaio.site"
},
"name": "Poll",
"nameSlug": "poll",
"classFile": "PollApp.ts",
"description": "Simple Poll"
}
73 changes: 73 additions & 0 deletions command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { IHttp, IModify, IPersistence, IRead } from '@rocket.chat/apps-ts-definition/accessors';
import { ISlashCommand, SlashCommandContext } from '@rocket.chat/apps-ts-definition/slashcommands';

const emojis = [
':zero:',
':one:',
':two:',
':three:',
':four:',
':five:',
':six:',
':seven:',
':eight:',
':nine:',
':keycap_ten:',
];

const clearQuotes = (item) => item.replace(/(^['"]|['"]$)/g, '');

export class PollCommand implements ISlashCommand {
public command = 'poll';
public i18nParamsExample = 'params_example';
public i18nDescription = 'cmd_description';
public providesPreview = false;

public async executor(context: SlashCommandContext, read: IRead, modify: IModify, http: IHttp, persis: IPersistence): Promise<void> {
const params = context.getArguments().join(' ');
const match = params.match(/((["'])(?:(?=(\\?))\3.)*?\2)/g);

if (!match) {
throw new Error('Invalid params');
}

// let question;
// const options: Array<string> = [];

const options = match.map(clearQuotes);
const question = options.shift();

// match.forEach((item, i) => {
// const clearItem = item.replace(/(^['"]|['"]$)/g, '');
// if (i === 0) {
// question = clearItem;
// } else {
// options.push(`${ emojis[(options.length + 1)] } ${ clearItem }`);
// }
// });

const builder = modify.getCreator().startMessage()
.setSender(context.getSender())
.setRoom(context.getRoom())
.setAvatarUrl('https://user-images.githubusercontent.com/8591547/44113440-751b9ff8-9fde-11e8-9e8c-8a555e6e382b.png')
.setText('_Please vote using reactions_')
.setUsernameAlias('Poll');

try {
builder.addAttachment({
color: '#73a7ce',
title: {
value: question,
},
text: options.map((option, index) => `${ emojis[index + 1] } ${ option }`).join('\n'),
});

await modify.getCreator().finish(builder);
} catch (e) {
// this.app.getLogger().error('Failed getting a gif', e);
builder.setText('An error occured when trying to send the gif :disappointed_relieved:');

modify.getNotifer().notifyUser(context.getSender(), builder.getMessage());
}
}
}
4 changes: 4 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"cmd_description": "Create a simple poll",
"params_example": "First parameter is the question, the next one are the poll options. Add them between quotes."
}
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fa4028e

Please sign in to comment.