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

src/index.ts   A

Complexity

Total Complexity 13
Complexity/F 0

Size

Lines of Code 121
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 92
mnd 13
bc 13
fnc 0
dl 0
loc 121
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import {HttpFunction} from '@google-cloud/functions-framework/build/src/functions';
2
3
4
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
5
import CommandHandler from './handlers/CommandHandler';
6
import MessageHandler from './handlers/MessageHandler';
7
import ActionHandler from './handlers/ActionHandler';
8
9
export const app: HttpFunction = async (req, res) => {
10
  if (!(req.method === 'POST' && req.body)) {
11
    res.status(400).send('');
12
  }
13
  const buttonCard: chatV1.Schema$CardWithId = {
14
    'cardId': 'welcome-card',
15
    'card': {
16
      'sections': [
17
        {
18
          'widgets': [
19
            {
20
              'buttonList': {
21
                'buttons': [
22
                  {
23
                    'text': 'Create Poll',
24
                    'onClick': {
25
                      'action': {
26
                        'function': 'show_form',
27
                        'interaction': 'OPEN_DIALOG',
28
                        'parameters': [],
29
                      },
30
                    },
31
                  },
32
                  {
33
                    'text': 'Terms and Conditions',
34
                    'onClick': {
35
                      'openLink': {
36
                        'url': 'https://absolute-poll.yaskur.com/terms-and-condition',
37
                      },
38
                    },
39
                  },
40
                  {
41
                    'text': 'Contact Us',
42
                    'onClick': {
43
                      'openLink': {
44
                        'url': 'https://absolute-poll.yaskur.com/contact-us',
45
                      },
46
                    },
47
                  },
48
                ],
49
              },
50
            },
51
          ],
52
        },
53
      ],
54
    },
55
  };
56
  const event = req.body;
57
  console.log(event.type,
58
    event.common?.invokedFunction || event.message?.slashCommand?.commandId || event.message?.argumentText,
59
    event.user.displayName, event.user.email, event.space.type, event.space.name);
60
61
  let reply: chatV1.Schema$Message = {
62
    thread: event.message.thread,
63
    actionResponse: {
64
      type: 'NEW_MESSAGE',
65
    },
66
    text: 'Hi! To create a poll, you can use the */poll* command. \n \n' +
67
      'Alternatively, you can create poll by mentioning me with question and answers. ' +
68
      'e.g *@Absolute Poll "Your Question" "Answer 1" "Answer 2"*',
69
  };
70
  // Dispatch slash and action events
71
  if (event.type === 'MESSAGE') {
72
    const message = event.message;
73
    if (message.slashCommand?.commandId) {
74
      reply = new CommandHandler(event).process();
75
    } else if (message.text) {
76
      reply = new MessageHandler(event).process();
77
    }
78
  } else if (event.type === 'CARD_CLICKED') {
79
    reply = await (new ActionHandler(event).process());
80
  } else if (event.type === 'ADDED_TO_SPACE') {
81
    const message: chatV1.Schema$Message = {
82
      text: undefined,
83
      cardsV2: undefined,
84
    };
85
    const spaceType = event.space.type;
86
    if (spaceType === 'ROOM') {
87
      message.text = 'Hi there! I\'d be happy to assist you in creating polls to improve collaboration and ' +
88
        'decision-making efficiency on Google Chat™.\n' +
89
        '\n' +
90
        'To create a poll, simply use the */poll* command or click on the "Create Poll" button below. ' +
91
        'You can also test our app in a direct message if you prefer.\n' +
92
        '\n' +
93
        'Alternatively, you can ' +
94
        'You can also test our app in a direct message if you prefer.\n' +
95
        '\n' +
96
        'We hope you find our service useful and please don\'t hesitate to contact us ' +
97
        'if you have any questions or concerns.';
98
    } else if (spaceType === 'DM') {
99
      message.text = 'Hey there! ' +
100
        'Before creating a poll in a group space, you can test it out here in a direct message.\n' +
101
        '\n' +
102
        'To create a poll, you can use the */poll* command or click on the "Create Poll" button below.\n' +
103
        '\n' +
104
        'Thank you for using our bot. We hope that it will prove to be a valuable tool for you and your team.\n' +
105
        '\n' +
106
        'Don\'t hesitate to reach out if you have any questions or concerns in the future.' +
107
        ' We are always here to help you and your team';
108
    }
109
110
    message.cardsV2 = [buttonCard];
111
112
    reply = {
113
      actionResponse: {
114
        type: 'NEW_MESSAGE',
115
      },
116
      ...message,
117
    };
118
  }
119
  res.json(reply);
120
};
121
122
123