-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d71d27
commit fa4028e
Showing
11 changed files
with
514 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
Oops, something went wrong.