Skip to content

Commit

Permalink
Merge pull request #9 from dyaskur/feature/winner_count_config
Browse files Browse the repository at this point in the history
Feature/winner count config
  • Loading branch information
dyaskur authored May 22, 2023
2 parents d6eb802 + 2e8a57b commit a478e70
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: "Deploy"

on:
release:
types: [ published ]
jobs:
deploy:
name: "Deploy Release"
Expand Down
12 changes: 8 additions & 4 deletions handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {buildActionResponse} from './helpers/response.js';
*/
export async function updateWinnerCardHandler(requestBody) {
const names = requestBody.names;
const winner = getRandomWinners(names);
const winnerCount = requestBody.count ?? 1;
const winner = getRandomWinners(names, winnerCount);
const messageText = `Congratulations! We have shuffled the list of names and the winner is *${winner.join(',')}*.`;
const message = buildMessageBody(buildNameListWinnerSection(names, winner), messageText);
const request = {
Expand All @@ -25,9 +26,10 @@ export async function updateWinnerCardHandler(requestBody) {
* @param {array} names - list of name that will be shuffled
* @param {string} space - google chat space name
* @param {string} thread - chat thread/parent
* @param {integer} count - winner count
* @returns {Promise<void>} will post the message to google API
*/
export async function createMessageFromNameListHandler(names, space, thread = null) {
export async function createMessageFromNameListHandler(names, space, thread = null, count = 1) {
const cardSection = buildNameListSection(names);
const message = buildMessageBody(cardSection);

Expand All @@ -43,6 +45,7 @@ export async function createMessageFromNameListHandler(names, space, thread = nu
const payload = {
messageId,
names: names,
count,
};
await delayUpdateMessage(JSON.stringify(payload));
}
Expand All @@ -55,8 +58,8 @@ export function helpCommandHandler(event) {
const message = `Hi ${event.user.displayName}
Here are the list of available commands:
*/random* Opens a dialog where you can input the items/names to be shuffled. By default, it is pre-filled with the list of members in the current space.
*/random_members* Quickly get a member of the current space. It's using a simple animation during randomizing
*/random_gpt* Get quick random staff using GPT command
*/random_members {count}* Quickly get a member of the current space. It's using a simple animation during randomizing
*/random_gpt {your_prompt}* Get quick random staff using GPT command
example 1: */random_gpt number from 1 to 100* it will return a number between 1 to 100 (e.g 12)
example 2: */random_gpt place to visit in Egypt* it will return a random place in Egypt
example 3: */random_gpt joke about programmer* it will return a joke related to programmer
Expand All @@ -66,6 +69,7 @@ export function helpCommandHandler(event) {
Discover the power of randomization by mentioning this app. By enclosing your request within double quotes, you can retrieve a random staff from the chat message. Alternatively, without double quotes, the app will utilize your message as a GPT command to generate a staff for you.
example 1(with double quote): *@Randombot "Zubair Manta" "Hasan Star" "Kawasaki Honda"* will shuffle the list given
example 2(without double quote): *@Randombot number between 10 and 20* just like */random_gpt* command, it will return a number between 10 and 20
example 3: *@Randombot number between 10 and 20* just like */random_gpt* command, it will return a number between 10 and 20
`;
return {
thread: event.message.thread,
Expand Down
22 changes: 21 additions & 1 deletion helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@ export function getRandomWinners(names, winnerCount = 1) {
* @param {string} string - text that will be extracted
* @returns {array} list of extracted text
*/
export function extractMessage(string) {
export function extractMessageByDoubleQuote(string) {
const regex = /"[^"]+"/g;
const extracted = string.match(regex);
if (extracted) {
return extracted.map((s) => s.replace(/"(.+)"/, '$1'));
}
return [];
}

/**
* @param {string} string - text that will be extracted
* @returns {array} list of extracted text
*/
export function extractMessageAndConfig(string) {
const regex = /"[^"]+"|[^\s]+/g;
return string.match(regex).map((s) => s.replace(/"(.+)"/, '$1'));
}

/**
* Config means anything else beside string inside quote
* @param {string} argumentText -
* @param {array} messages - extracted messages
* @returns {array} extracted command
*/
export function extractConfig(argumentText, messages) {
const extractedTextCommands = extractMessageAndConfig(argumentText);
return extractedTextCommands.filter((val) => !messages.includes(val));
}
32 changes: 26 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
helpCommandHandler,
updateWinnerCardHandler,
} from './handlers.js';
import {extractMessage} from './helpers/utils.js';
import {extractConfig, extractMessageByDoubleQuote} from './helpers/utils.js';
import {buildInputForm} from './helpers/components.js';
import {getRandomFromGpt} from './helpers/gpt.js';

Expand Down Expand Up @@ -43,7 +43,18 @@ export async function app(req, res) {
} else if (message.slashCommand?.commandId === '2') { // /random_members command
const members = await getMembers(event.space.name);
const memberNames = members.map((a) => a.member.displayName);
await createMessageFromNameListHandler(memberNames, event.space.name, event.threadKey);
const winnerCount = parseInt(event.message.argumentText) || 1;
if (winnerCount > memberNames.length) {
reply = {
thread: event.message.thread,
actionResponse: {
type: 'NEW_MESSAGE',
},
text: 'Your space members is not enough',
};
} else {
await createMessageFromNameListHandler(memberNames, event.space.name, event.threadKey, winnerCount);
}
} else if (message.slashCommand?.commandId === '3') { // /help command
reply = helpCommandHandler(event);
} else if (message.slashCommand?.commandId === '4') { // /config command
Expand All @@ -59,9 +70,18 @@ export async function app(req, res) {
};
} else if (message.text) {
const argumentText = event.message?.argumentText;
const extractedText = extractMessage(argumentText);
const extractedText = extractMessageByDoubleQuote(argumentText);
if (extractedText.length > 1) {
await createMessageFromNameListHandler(extractedText, event.space.name, event.threadKey);
let winnerCount = 1;
const configs = extractConfig(argumentText, extractedText);
configs.forEach((config) => {
const winnerConfig = parseInt(config);
// If the argument is a number, we will assume it is a config indicating the number of winners to choose.
if (winnerConfig && winnerConfig < extractedText.length) {
winnerCount = winnerConfig;
}
});
await createMessageFromNameListHandler(extractedText, event.space.name, event.threadKey, winnerCount);
} else {
const answer = await getRandomFromGpt(argumentText ?? 'whatever');
reply = {
Expand All @@ -78,8 +98,8 @@ export async function app(req, res) {
if (action === 'create_shuffle') {
const formValues = event.common?.formInputs;
const items = formValues?.['items']?.stringInputs.value[0]?.trim();
await createMessageFromNameListHandler(items.split('\n'), event.uspace.name, event.threadKey);
reply = buildActionResponseStatus('Your items/names are being shffle');
await createMessageFromNameListHandler(items.split('\n'), event.space.name, event.threadKey);
reply = buildActionResponseStatus('Your items/names are being shuffle');
}
} else if (event.type === 'ADDED_TO_SPACE') {
const message = `Hi ${event.user.displayName ?? 'there'}, Thanks for installing our app
Expand Down
59 changes: 56 additions & 3 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {extractMessage, getRandomWinners} from '../helpers/utils.js';
import {
extractConfig,
extractMessageAndConfig,
extractMessageByDoubleQuote,
getRandomWinners,
} from '../helpers/utils.js';

test('get the winner', () => {
const names = ['Ibrahim', 'Isa', 'Moses', 'Ismail'];
Expand All @@ -17,15 +22,63 @@ const dataSet = [
'"anu kae" "super" "sekali kae lo"', [
'anu kae', 'super', 'sekali kae lo',
]],
[
'"Indonesia" "Malaysia" Bali', ['Indonesia', 'Malaysia'],
],
[
'nganu kae', [],
],
[
'z', [],
],
[
'', [],
],
];
it.each(dataSet)('Extract message using inside quote only', (input, expectedValue) => {
const extractedStrings = extractMessageByDoubleQuote(input);

expect(extractedStrings).toEqual(expectedValue);
});

const dataSet2 = [
[
'"anu kae" "super" "sekali kae lo"', [
'anu kae', 'super', 'sekali kae lo',
]],
[
'"Indonesia" "Malaysia" Bali', ['Indonesia', 'Malaysia', 'Bali'],
],
[
'nganu kae', ['nganu', 'kae'],
],
[
'z', ['z'],
'', [],
],
];
it.each(dataSet2)('Extract message and command', (input, expectedValue) => {
const extractedStrings = extractMessageAndConfig(input);

expect(extractedStrings).toEqual(expectedValue);
});

const dataSet3 = [
[
'"anu kae" "super" "sekali kae lo"', []],
[
'"Indonesia" "Malaysia" Bali', ['Bali'],
],
[
'nganu kae', ['nganu', 'kae'],
],
[
'z', ['z'],
'', [],
],
];
it.each(dataSet)('Extract message using regex', (input, expectedValue) => {
const extractedStrings = extractMessage(input);
it.each(dataSet3)('Extract message and command', (input, expectedValue) => {
const extractedStrings = extractConfig(input, extractMessageByDoubleQuote(input));

expect(extractedStrings).toEqual(expectedValue);
});

0 comments on commit a478e70

Please sign in to comment.