1
|
|
|
import {PollState} from './interfaces'; |
2
|
|
|
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1'; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Add a new option to the state(like DB) |
6
|
|
|
* |
7
|
|
|
* @param {string} option - the new option name |
8
|
|
|
* @param {object} state - the current message state |
9
|
|
|
* @param {string} creator - who add the new option |
10
|
|
|
* @returns {void} card |
11
|
|
|
*/ |
12
|
|
|
export function addOptionToState(option: string, state: PollState, creator = '') { |
13
|
|
|
const choiceLength = state.choices.length; |
14
|
|
|
state.choices.push(option); |
15
|
|
|
if (state.choiceCreator === undefined) { |
16
|
|
|
state.choiceCreator = {[choiceLength]: creator}; |
17
|
|
|
} else { |
18
|
|
|
state.choiceCreator[choiceLength] = creator; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) { |
23
|
|
|
const card = event.message?.cardsV2?.[0]?.card as chatV1.Schema$GoogleAppsCardV1Card; |
24
|
|
|
return getStateFromCardName(card) || getStateFromParameter(event) || getStateFromCardWhenNoHeader(card) || |
25
|
|
|
getStateFromCardWhenHasHeader(card); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function getStateFromCardWhenNoHeader(card: chatV1.Schema$GoogleAppsCardV1Card) { |
29
|
|
|
return card?.sections?.[0].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function getStateFromCardWhenHasHeader(card: chatV1.Schema$GoogleAppsCardV1Card) { |
33
|
|
|
// when has header the first section is header |
34
|
|
|
return card?.sections?.[1].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function getStateFromCardName(card: chatV1.Schema$GoogleAppsCardV1Card) { |
38
|
|
|
// when has header the first section is header |
39
|
|
|
return card?.name; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function getStateFromParameter(event: chatV1.Schema$DeprecatedEvent) { |
43
|
|
|
const parameters = event.common?.parameters; |
44
|
|
|
|
45
|
|
|
return parameters?.['state']; |
46
|
|
|
} |
47
|
|
|
|