Completed
Push — master ( 4fcad4...efcce7 )
by Muhammad Dyas
19s queued 16s
created

src/handlers/MessageHandler.ts   A

Complexity

Total Complexity 9
Complexity/F 9

Size

Lines of Code 53
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 43
mnd 8
bc 8
fnc 1
dl 0
loc 53
rs 10
bpm 8
cpm 9
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C MessageHandler.process 0 43 9
1
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
2
import BaseHandler from './BaseHandler';
3
import {splitMessage} from '../helpers/utils';
4
import PollCard from '../cards/PollCard';
5
6
export default class MessageHandler extends BaseHandler {
7
  process(): chatV1.Schema$Message {
8
    const argumentText = this.event.message?.argumentText?.trim() ?? '';
9
    const helpResponse = {
10
      thread: this.event.message!.thread,
11
      actionResponse: {
12
        type: 'NEW_MESSAGE',
13
      },
14
      text: 'Hi there! I can help you create polls to enhance collaboration and efficiency ' +
15
        'in decision-making using Google Chat™.\n' +
16
        '\n' +
17
        'Below is an example commands:\n' +
18
        '`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
19
        '`/poll "Which is the best country to visit" "Indonesia"` - to create a poll with ' +
20
        '"Which is the best country to visit" as the topic and "Indonesia" as the answer\n' +
21
        '\n' +
22
        'We hope you find our service useful and please don\'t hesitate to contact us ' +
23
        'if you have any questions or concerns.',
24
    };
25
    switch (argumentText) {
26
      case 'help':
27
        return helpResponse;
28
      default:
29
        const choices = splitMessage(argumentText);
30
        if (choices.length > 2) {
31
          const pollCard = new PollCard({
32
            choiceCreator: undefined,
33
            topic: choices.shift() ?? '',
34
            author: this.event.user,
35
            choices: choices,
36
            votes: {},
37
            anon: false,
38
            optionable: true,
39
          });
40
          const message = pollCard.createMessage();
41
          return {
42
            thread: this.event.message!.thread,
43
            actionResponse: {
44
              type: 'NEW_MESSAGE',
45
            },
46
            ...message,
47
          };
48
        }
49
        return helpResponse;
50
    }
51
  }
52
}
53