Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Move trigger construction into triggers/ directory #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 7 additions & 33 deletions functions/create_welcome_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";

import { SendWelcomeMessageWorkflow } from "../workflows/send_welcome_message.ts";
import { WelcomeMessageDatastore } from "../datastores/messages.ts";
import { newSendWelcomeMessageTrigger } from "../triggers/send_welcome_message_event.ts";

/**
* This custom function will take the initial form input, store it
Expand Down Expand Up @@ -60,7 +61,12 @@ export default SlackFunction(

// Create a new user_joined_channel trigger if none exist
if (!triggers.exists) {
const newTrigger = await saveUserJoinedChannelTrigger(client, channel);
const sendWelcomeMessageTrigger = newSendWelcomeMessageTrigger(channel);

const newTrigger = await client.workflows.triggers.create<
typeof SendWelcomeMessageWorkflow.definition
>(sendWelcomeMessageTrigger);

if (!newTrigger.ok) {
return {
error: `Failed to create welcome trigger: ${newTrigger.error}`,
Expand Down Expand Up @@ -99,35 +105,3 @@ export async function findUserJoinedChannelTrigger(
const exists = joinedTriggers.length > 0;
return { exists };
}

/**
* saveUserJoinedChannelTrigger creates a new user_joined_channel trigger
* for the "Send Welcome Message" workflow in a channel.
*/
export async function saveUserJoinedChannelTrigger(
client: SlackAPIClient,
channel: string,
): Promise<{ ok: boolean; error?: string }> {
const triggerResponse = await client.workflows.triggers.create<
typeof SendWelcomeMessageWorkflow.definition
>({
type: "event",
name: "User joined channel",
description: "Send a message when a user joins the channel",
workflow:
`#/workflows/${SendWelcomeMessageWorkflow.definition.callback_id}`,
event: {
event_type: "slack#/events/user_joined_channel",
channel_ids: [channel],
},
inputs: {
channel: { value: channel },
triggered_user: { value: "{{data.user_id}}" },
},
});

if (!triggerResponse.ok) {
return { ok: false, error: triggerResponse.error };
}
return { ok: true };
}
26 changes: 26 additions & 0 deletions triggers/send_welcome_message_event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Trigger } from "deno-slack-api/types.ts";
import { SendWelcomeMessageWorkflow } from "../workflows/send_welcome_message.ts";

/**
* newSendWelcomeMessageTrigger constructs a new user_joined_channel trigger
* for the "Send Welcome Message" workflow in a specified channel.
*/
export function newSendWelcomeMessageTrigger(
channel: string,
): Trigger<typeof SendWelcomeMessageWorkflow.definition> {
return {
type: "event",
name: "User joined channel",
description: "Send a message when a user joins the channel",
workflow:
`#/workflows/${SendWelcomeMessageWorkflow.definition.callback_id}`,
event: {
event_type: "slack#/events/user_joined_channel",
channel_ids: [channel],
},
inputs: {
channel: { value: channel },
triggered_user: { value: "{{data.user_id}}" },
},
};
}