Skip to content

Commit

Permalink
feat: dynamic help response message
Browse files Browse the repository at this point in the history
  • Loading branch information
dyaskur committed Aug 27, 2023
1 parent 33b8174 commit bb2a4c2
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 62 deletions.
6 changes: 5 additions & 1 deletion src/handlers/BaseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export default abstract class BaseHandler {
}

protected getAnnotations(): chatV1.Schema$Annotation[] {
return this.event?.message?.annotations ?? [];
return this.event.message?.annotations ?? [];
}

protected getAnnotationByType(type: string): chatV1.Schema$Annotation | undefined {
return this.getAnnotations().find((annotation) => annotation.type === type);
}

public abstract process(): chatV1.Schema$Message | Promise<chatV1.Schema$Message>;
Expand Down
13 changes: 3 additions & 10 deletions src/handlers/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import NewPollFormCard from '../cards/NewPollFormCard';
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
import BaseHandler from './BaseHandler';
import {buildOptionsFromMessage} from '../helpers/utils';
import {generateHelpText} from '../helpers/helper';

export default class CommandHandler extends BaseHandler {
private slashCommand: chatV1.Schema$SlashCommandMetadata | undefined;
Expand Down Expand Up @@ -39,21 +40,13 @@ export default class CommandHandler extends BaseHandler {
},
};
default:
const isPrivate = this.event!.space?.type === 'DM';
return {
thread: this.event.message!.thread,
actionResponse: {
type: 'NEW_MESSAGE',
},
text: 'Hi there! I can help you create polls to enhance collaboration and efficiency ' +
'in decision-making using Google Chat™.\n' +
'\n' +
'Below is an example commands:\n' +
'`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
'`/poll "Which is the best country to visit" "Indonesia"` - to create a poll with ' +
'"Which is the best country to visit" as the topic and "Indonesia" as the answer\n' +
'\n' +
'We hope you find our service useful and please don\'t hesitate to contact us ' +
'if you have any questions or concerns.',
text: generateHelpText(isPrivate),
};
}
}
Expand Down
59 changes: 48 additions & 11 deletions src/handlers/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,68 @@ import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
import BaseHandler from './BaseHandler';
import {splitMessage} from '../helpers/utils';
import PollCard from '../cards/PollCard';
import {generateHelpText} from '../helpers/helper';

export default class MessageHandler extends BaseHandler {
process(): chatV1.Schema$Message {
const argumentText = this.event.message?.argumentText?.trim() ?? '';
const buttonCard: chatV1.Schema$CardWithId = {
'cardId': 'welcome-card',
'card': {
'sections': [
{
'widgets': [
{
'buttonList': {
'buttons': [
{
'text': 'Create Poll',
'onClick': {
'action': {
'function': 'show_form',
'interaction': 'OPEN_DIALOG',
'parameters': [],
},
},
},
{
'text': 'Contact Us',
'onClick': {
'openLink': {
'url': 'https://github.com/dyaskur/google-chat-poll/issues',
},
},
},
],
},
},
],
},
],
},
};

const helpResponse = {
thread: this.event.message!.thread,
actionResponse: {
type: 'NEW_MESSAGE',
},
text: 'Hi there! I can help you create polls to enhance collaboration and efficiency ' +
'in decision-making using Google Chat™.\n' +
'\n' +
'Below is an example commands:\n' +
'`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
'`/poll "Which is the best country to visit" "Indonesia"` - to create a poll with ' +
'"Which is the best country to visit" as the topic and "Indonesia" as the answer\n' +
'\n' +
'We hope you find our service useful and please don\'t hesitate to contact us ' +
'if you have any questions or concerns.',
text: '',
cardsV2: [buttonCard],
};
const isPrivate = this.event!.space?.type === 'DM';

switch (argumentText) {
case 'help':
helpResponse.text = generateHelpText(isPrivate);
return helpResponse;
case 'test dyas':
helpResponse.text = 'Hello <https://github.com/dyaskur/google-chat-poll|google-chat-poll>';
return helpResponse;
default:
const choices = splitMessage(argumentText);
if (choices.length > 2) {
const annotation = this.getAnnotationByType('USER_MENTION');
if (annotation && choices.length > 2) {
const pollCard = new PollCard({
choiceCreator: undefined,
topic: choices.shift() ?? '',
Expand All @@ -46,6 +82,7 @@ export default class MessageHandler extends BaseHandler {
...message,
};
}
helpResponse.text = generateHelpText(isPrivate);
return helpResponse;
}
}
Expand Down
82 changes: 82 additions & 0 deletions src/helpers/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function getRandomString(string: Array<string>) {
return string[Math.floor(Math.random() * string.length)];
}

function getRandomFooter() {
const footers = [
'We trust that you\'ll discover our service valuable, and we encourage you to reach out to us without hesitation should you have any inquiries or apprehensions.',
'We hope you find our service useful and please don\'t hesitate to contact us if you have any questions or concerns.',
'We trust that our service proves valuable to you. Should you have any inquiries or apprehensions, please feel free to reach out to us without hesitation.',
'We anticipate that you\'ll find our service valuable, and please don\'t hesitate to get in touch if you have any questions or concerns.',
'We appreciate your usage of our application and please don\'t hesitate to contact us if you have any questions or concerns.',
'Thank you for using our app. We hope you\'ll find it to be a valuable tool',
];
return getRandomString(footers);
}

function getRandomGreet() {
const greets = [
'Hi there! ',
'Greetings! ',
'Hello, ',
'Hi, ',
'Yay! ',
];
return getRandomString(greets);
}

function getRandomDescription() {
const descriptions = [
'I\'m here to assist you in creating polls that can improve teamwork and streamline the process of making decisions.',
'I can help you create polls to enhance collaboration and efficiency in decision-making using Google Chat™.',
'I can help you make polls for better teamwork using Google Chat™',
'I\'m capable of assisting you in generating polls that can boost collaboration and streamline decision-making through Google Chat™.',
'My purpose is to support you in crafting polls that enhance teamwork and simplify the decision-making process.',
'',
];
return getRandomString(descriptions);
}

function getRandomExample() {
const examples = [
'"Which is the best country to visit" "Indonesia" "Bali"',
'"When should we schedule our team days?" "Monday" "Tuesday" "Wednesday"',
'"What would be the ideal date for our vacation?" "30 th February" "26 th March" "15 th April"',
'"Which date works best for [event]?" "30 th February" "26 th March" "15 th April"',
'"Your Question" "Answer 1" "Answer 2"',
];
return getRandomString(examples);
}

function getRandomInfo() {
const info = [
'This app is designed to be used within group or space rooms with multiple members. However, if you\'d like to learn more about it, you can test it in a direct message (DM) conversation here.',
'This app is a collaborative tool and is intended to be used in group or space rooms with multiple members. However, you can also test it out in a direct message (DM) here to discover more about its features.',
'As a collaborative application, this is intended for use in group or space rooms with multiple members. However, you can also explore and test it further in a direct message (DM) here to gain a better understanding.',
'This app is meant for group/space collaboration, but you can also test it via DM for more insights',
'This is a collaborative app designed for group or space rooms, but you can also test it via DM for a better grasp.',
'To enhance its functionality, install this app in a group or space room with multiple members. However, you can also test it in a direct message (DM) here to learn more.',
];
return getRandomString(info);
}

export function generateHelpText(isPrivate: boolean = false) {
const greet = getRandomGreet();
const description = getRandomDescription();
const footer = getRandomFooter();
const example = getRandomExample();
const example2 = getRandomExample();
let additionalMessage = '';
if (isPrivate) {
additionalMessage = getRandomInfo() + '\n\n';
}
return greet + description + '\n' +
'\n' + additionalMessage +
'Below is an example commands:\n' +
'`/poll` - You will need to fill out the topic and answers in the form that will be displayed.\n' +
'`/poll ' + example + '` - to create a poll with autofill \n' +
'\n' +
'Alternatively, you can create poll by mentioning me with question and answers. ' +
'e.g *@Absolute Poll ' + example2 + '*\n\n' +
footer;
}
18 changes: 12 additions & 6 deletions src/helpers/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) {
getStateFromCardWhenHasHeader(card);
}

function getChoicesFromInput(formValues: PollFormInputs) {
const choices = [];
for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
const choice = formValues[`option${i}`]?.stringInputs!.value![0]!.trim();
if (choice) {
choices.push(choice);
}
}
return choices;
}

export function getConfigFromInput(formValues: PollFormInputs) {
const state: PollConfig = {topic: '', choices: []};
state.topic = formValues.topic.stringInputs!.value![0]!.trim() ?? '';
state.anon = formValues.is_anonymous?.stringInputs!.value![0] === '1';
state.optionable = formValues.allow_add_option?.stringInputs!.value![0] === '1';
state.type = parseInt(formValues.type?.stringInputs!.value![0] ?? '1') as ClosableType;
state.choices = getChoicesFromInput(formValues);

for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
const choice = formValues[`option${i}`]?.stringInputs!.value![0]!.trim();
if (choice) {
state.choices.push(choice);
}
}
return state;
}

Expand Down
14 changes: 3 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
import CommandHandler from './handlers/CommandHandler';
import MessageHandler from './handlers/MessageHandler';
import ActionHandler from './handlers/ActionHandler';
import {generateHelpText} from './helpers/helper';

export const app: HttpFunction = async (req, res) => {
if (!(req.method === 'POST' && req.body)) {
Expand Down Expand Up @@ -41,7 +42,7 @@ export const app: HttpFunction = async (req, res) => {
'text': 'Contact Us',
'onClick': {
'openLink': {
'url': 'https://absolute-poll.yaskur.com/contact-us',
'url': 'https://github.com/dyaskur/google-chat-poll/issues',
},
},
},
Expand Down Expand Up @@ -88,23 +89,14 @@ export const app: HttpFunction = async (req, res) => {
'decision-making efficiency on Google Chat™.\n' +
'\n' +
'To create a poll, simply use the */poll* command or click on the "Create Poll" button below. ' +
'You can also test our app in a direct message if you prefer.\n' +
'\n' +
'Alternatively, you can ' +
'You can also test our app in a direct message if you prefer.\n' +
'\n' +
'We hope you find our service useful and please don\'t hesitate to contact us ' +
'if you have any questions or concerns.';
} else if (spaceType === 'DM') {
message.text = 'Hey there! ' +
'Before creating a poll in a group space, you can test it out here in a direct message.\n' +
'\n' +
'To create a poll, you can use the */poll* command or click on the "Create Poll" button below.\n' +
'\n' +
'Thank you for using our bot. We hope that it will prove to be a valuable tool for you and your team.\n' +
'\n' +
'Don\'t hesitate to reach out if you have any questions or concerns in the future.' +
' We are always here to help you and your team';
message.text = generateHelpText(true);
}

message.cardsV2 = [buttonCard];
Expand Down
2 changes: 1 addition & 1 deletion tests/command-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('process command from google chat message event', () => {
const handler = new CommandHandler(event);
const result = handler.process();
expect(result.actionResponse.type).toEqual('NEW_MESSAGE');
expect(result.text).toContain('Hi there! I can help you create polls to enhance collaboration and efficiency');
expect(result.text).toContain('Below is an example commands:');
});

it('should throw an error when no slash command is found in the message annotations', () => {
Expand Down
23 changes: 23 additions & 0 deletions tests/helpers/helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {generateHelpText} from '../../src/helpers/helper';

describe('generateHelpText', () => {
it('should return a string with a greeting, description, example commands, and footer', () => {
const isPrivate = false;

const result = generateHelpText(isPrivate);

expect(typeof result).toBe('string');
expect(result).toContain('Below is an example commands');
expect(result).not.toContain('group'); // group is additional message from getRandomInfo()
});


it('should include an additional message if isPrivate is true', () => {
const isPrivate = true;

const result = generateHelpText(isPrivate);

expect(result).toContain('You will need to fill out the topic and answers in the form that will be displayed');
expect(result).toContain('group'); // group is additional message from getRandomInfo()
});
});
Loading

0 comments on commit bb2a4c2

Please sign in to comment.