Total Complexity | 10 |
Complexity/F | 5 |
Lines of Code | 54 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import NewPollFormCard from '../cards/NewPollFormCard'; |
||
2 | import {chat_v1 as chatV1} from '@googleapis/chat'; |
||
3 | import BaseHandler from './BaseHandler'; |
||
4 | import {buildOptionsFromMessage} from '../helpers/utils'; |
||
5 | import {generateHelpText} from '../helpers/helper'; |
||
6 | |||
7 | export default class CommandHandler extends BaseHandler { |
||
8 | private slashCommand: chatV1.Schema$SlashCommandMetadata | undefined; |
||
9 | |||
10 | public constructor(event: chatV1.Schema$DeprecatedEvent) { |
||
11 | super(event); |
||
12 | this.parseSlashCommand(); |
||
13 | |||
14 | if (this.slashCommand === undefined) { |
||
15 | throw new Error('No Slash Command found'); |
||
16 | } |
||
17 | } |
||
18 | |||
19 | parseSlashCommand() { |
||
20 | this.getAnnotations().forEach((annotation) => { |
||
21 | if (annotation.type === 'SLASH_COMMAND') { |
||
22 | this.slashCommand = annotation.slashCommand!; |
||
23 | } |
||
24 | }); |
||
25 | } |
||
26 | |||
27 | process(): chatV1.Schema$Message { |
||
28 | switch (this.slashCommand!.commandName) { |
||
29 | case '/poll': |
||
30 | const argumentText = this.event.message?.argumentText?.trim() ?? ''; |
||
31 | const options = buildOptionsFromMessage(argumentText); |
||
32 | return { |
||
33 | actionResponse: { |
||
34 | type: 'DIALOG', |
||
35 | dialogAction: { |
||
36 | dialog: { |
||
37 | body: new NewPollFormCard(options, this.getUserTimezone()).create(), |
||
38 | }, |
||
39 | }, |
||
40 | }, |
||
41 | }; |
||
42 | default: |
||
43 | const isPrivate = this.event.space?.type === 'DM'; |
||
44 | return { |
||
45 | thread: this.event.message!.thread, |
||
46 | actionResponse: { |
||
47 | type: 'NEW_MESSAGE', |
||
48 | }, |
||
49 | text: generateHelpText(isPrivate), |
||
50 | }; |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 |