Skip to content

Commit

Permalink
End the game after N minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
djbotha committed Mar 15, 2023
1 parent 7ea599b commit 4811853
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 6 deletions.
41 changes: 41 additions & 0 deletions functions/end_game_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { SlackAPI } from 'deno-slack-api/mod.ts';
import { DefineFunction, Schema, SlackFunction } from 'deno-slack-sdk/mod.ts';

/**
* Functions are reusable building blocks of automation that accept
* inputs, perform calculations, and provide outputs. Functions can
* be used independently or as steps in workflows.
* https://api.slack.com/future/functions/custom
*/
export const EndGameFunctionDefinition = DefineFunction({
callback_id: 'end_game_function',
title: 'End Game',
description: 'Ends the game and tallies the results',
source_file: 'functions/end_game_function.ts',
input_parameters: {
properties: {
channel_id: {
type: Schema.slack.types.channel_id,
},
},
},
output_parameters: {
properties: {
message: {
type: Schema.types.string,
description: 'Greeting for the recipient',
},
},
required: ['message'],
},
});

export default SlackFunction(EndGameFunctionDefinition, async ({ inputs, token }) => {
const { channel_id } = inputs;
console.log('djb executing message to channel', channel_id);
return {
outputs: {
message: `These are your winners:`,
},
};
});
38 changes: 36 additions & 2 deletions functions/greeting_function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SlackAPI } from 'deno-slack-api/mod.ts';
import { DefineFunction, Schema, SlackFunction } from 'deno-slack-sdk/mod.ts';
import emoji from '../assets/emoji.ts';
import EndGameWorkflow from '../workflows/end_game_workflow.ts';

/**
* Functions are reusable building blocks of automation that accept
Expand All @@ -13,6 +14,18 @@ export const GreetingFunctionDefinition = DefineFunction({
title: 'Generate a greeting',
description: 'Generate a greeting',
source_file: 'functions/greeting_function.ts',
input_parameters: {
properties: {
duration: {
type: Schema.types.integer,
description: 'How long should the game run for, in minutes',
},
channel_id: {
type: Schema.slack.types.channel_id,
description: 'Destination channel',
},
},
},
output_parameters: {
properties: {
greeting: {
Expand All @@ -24,8 +37,9 @@ export const GreetingFunctionDefinition = DefineFunction({
},
});

export default SlackFunction(GreetingFunctionDefinition, async ({ token }) => {
// TODO: generate emoji prompt here
export default SlackFunction(GreetingFunctionDefinition, async ({ inputs, token }) => {
const { channel_id, duration } = inputs;

const client = SlackAPI(token);
const res = await client.apiCall('emoji.list');
const customEmoji = Object.keys(res?.emoji);
Expand All @@ -35,6 +49,26 @@ export default SlackFunction(GreetingFunctionDefinition, async ({ token }) => {
.map(() => `:${allEmoji[Math.floor(Math.random() * allEmoji.length)]}:`)
.join('');

const now = new Date();
now.setMinutes(now.getMinutes() + duration);

// Schedule the job to end
await client.workflows.triggers.create<typeof EndGameWorkflow.definition>({
name: 'Example',
type: 'scheduled',
workflow: `#/workflows/${EndGameWorkflow.definition.callback_id}`,
inputs: {
channel_id: { value: channel_id },
},
schedule: {
start_time: now.toISOString(),
timezone: 'UTC',
frequency: {
type: 'once',
},
},
});

return {
outputs: {
greeting: `Given these ten emojis, create as meaningful a sentence as possible. The more emojis a sentence consists, the more points it gets.\n\n${greeting}`,
Expand Down
5 changes: 3 additions & 2 deletions manifest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Manifest } from 'deno-slack-sdk/mod.ts';
import EndGameWorkflow from './workflows/end_game_workflow.ts';
import GreetingWorkflow from './workflows/greeting_workflow.ts';

/**
Expand All @@ -10,7 +11,7 @@ export default Manifest({
name: 'polymoji',
description: 'A sample that demonstrates using a function, workflow and trigger to send a greeting',
icon: 'assets/default_new_app_icon.png',
workflows: [GreetingWorkflow],
workflows: [GreetingWorkflow, EndGameWorkflow],
outgoingDomains: [],
botScopes: ['commands', 'chat:write', 'chat:write.public', 'emoji:read'],
botScopes: ['commands', 'chat:write', 'chat:write.public', 'emoji:read', 'triggers:write'],
});
31 changes: 31 additions & 0 deletions workflows/end_game_workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { DefineWorkflow, Schema } from 'deno-slack-sdk/mod.ts';
import { EndGameFunctionDefinition } from '../functions/end_game_function.ts';

/**
* A workflow is a set of steps that are executed in order.
* Each step in a workflow is a function.
* https://api.slack.com/future/workflows
*/
const EndGameWorkflow = DefineWorkflow({
callback_id: 'end_game_workflow',
title: 'End the game',
description: 'Ends the game and tallies the results',
input_parameters: {
properties: {
channel_id: {
type: Schema.slack.types.channel_id,
},
},
},
});

const endGameFunctionStep = EndGameWorkflow.addStep(EndGameFunctionDefinition, {
channel_id: EndGameWorkflow.inputs.channel_id,
});

EndGameWorkflow.addStep(Schema.slack.functions.SendMessage, {
channel_id: EndGameWorkflow.inputs.channel_id,
message: endGameFunctionStep.outputs.message,
});

export default EndGameWorkflow;
12 changes: 10 additions & 2 deletions workflows/greeting_workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ const inputForm = GreetingWorkflow.addStep(Schema.slack.functions.OpenForm, {
type: Schema.slack.types.channel_id,
default: GreetingWorkflow.inputs.channel,
},
{
name: 'duration',
title: 'How long should the game run for, in minutes',
type: Schema.types.integer,
},
],
required: ['channel'],
required: ['channel', 'duration'],
},
});

const greetingFunctionStep = GreetingWorkflow.addStep(GreetingFunctionDefinition, {});
const greetingFunctionStep = GreetingWorkflow.addStep(GreetingFunctionDefinition, {
channel_id: inputForm.outputs.fields.channel,
duration: inputForm.outputs.fields.duration,
});

GreetingWorkflow.addStep(Schema.slack.functions.SendMessage, {
channel_id: inputForm.outputs.fields.channel,
Expand Down

0 comments on commit 4811853

Please sign in to comment.