Passed
Pull Request — master (#2)
by Muhammad Dyas
01:30
created

MessageHandler.process   C

Complexity

Conditions 9

Size

Total Lines 44
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 38
dl 0
loc 44
rs 6.6346
c 0
b 0
f 0
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
  constructor(event: chatV1.Schema$DeprecatedEvent) {
8
    super(event);
9
  }
10
11
  process(): chatV1.Schema$Message {
12
    const argumentText = this.event.message?.argumentText?.trim() ?? '';
13
    const helpResponse = {
14
      thread: this.event.message!.thread,
15
      actionResponse: {
16
        type: 'NEW_MESSAGE',
17
      },
18
      text: 'Hi there! I can help you create polls to enhance collaboration and efficiency ' +
19
        'in decision-making using Google Chat™.\n' +
20
        '\n' +
21
        'Below is an example commands:\n' +
22
        '`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
23
        '`/poll "Which is the best country to visit" "Indonesia"` - to create a poll with ' +
24
        '"Which is the best country to visit" as the topic and "Indonesia" as the answer\n' +
25
        '\n' +
26
        'We hope you find our service useful and please don\'t hesitate to contact us ' +
27
        'if you have any questions or concerns.',
28
    };
29
    switch (argumentText) {
30
      case 'help':
31
        return helpResponse;
32
      default:
33
        const choices = splitMessage(argumentText);
34
        if (choices.length > 2) {
35
          const pollCard = new PollCard({
36
            choiceCreator: undefined,
37
            topic: choices.shift() ?? '',
38
            author: this.event.user,
39
            choices: choices,
40
            votes: {},
41
            anon: false,
42
            optionable: true,
43
          });
44
          const message = pollCard.createMessage();
45
          return {
46
            thread: this.event.message!.thread,
47
            actionResponse: {
48
              type: 'NEW_MESSAGE',
49
            },
50
            ...message,
51
          };
52
        }
53
        return helpResponse;
54
    }
55
  }
56
}
57