Skip to content
Merged
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
3 changes: 1 addition & 2 deletions apps/docs/examples/slack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ new Trigger({
on: github.events.repoIssueEvent({
repo: "my-github-org/my-github-repo",
}),

run: async (event, ctx) => {
const response = await slack.postMessage("send-to-slack", {
channel: "my-slack-channel-name",
channelName: "my-slack-channel-name",
text: `A new issue has been created or modified. ${event.action}`,
});

Expand Down
18 changes: 10 additions & 8 deletions examples/send-to-slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Trigger, customEvent } from "@trigger.dev/sdk";
import { slack } from "@trigger.dev/integrations";
import { z } from "zod";

const trigger = new Trigger({
new Trigger({
id: "send-to-slack-on-new-domain",
name: "Send to Slack on new domain",
apiKey: "trigger_dev_zC25mKNn6c0q",
Expand All @@ -21,17 +21,19 @@ const trigger = new Trigger({
"Received domain.created event, waiting for 1 minutes..."
);

await ctx.waitFor("initial-wait", { seconds: 5 });

const response = await slack.postMessage("send-to-slack", {
channel: "test-integrations",
channelName: "test-integrations",
text: `New domain created: ${event.domain} by customer ${event.customerId} cc @Eric #general`,
});

await ctx.logger.debug("Debug message");
await ctx.waitFor("initial-wait", { seconds: 5 });

const secondResponse = await slack.postMessage("send-to-slack-channel-id", {
channelId: response.channel,
text: `Sent using the channelId: ${response.channel}`,
});

return response.message;
return {};
},
});
}).listen();

trigger.listen();
12 changes: 10 additions & 2 deletions packages/internal-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ program
const pizzly_host = options.pizzlyhost ?? "http://localhost:3004";
const providers = getProviders(true);

console.log(`Using pizzly host: ${pizzly_host}`);

const client = new SecretsManagerClient({
region: "us-east-1",
credentials: fromIni({ profile: options.awsprofile ?? "default" }),
Expand Down Expand Up @@ -128,7 +130,10 @@ program
provider.authentication.scopes,
options.pizzlysecretkey
);
console.log(`Updated config for ${service}`);
console.log(
`Updated config for ${service} with scopes`,
provider.authentication.scopes
);
} else {
const response = await createConfig(
pizzly_host,
Expand All @@ -138,7 +143,10 @@ program
provider.authentication.scopes,
options.pizzlysecretkey
);
console.log(`Created config for ${service}`);
console.log(
`Created config for ${service} with scopes`,
provider.authentication.scopes
);
}
} catch (error) {
// For a list of exceptions thrown, see
Expand Down
71 changes: 47 additions & 24 deletions packages/internal-integrations/src/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
AccessInfo,
} from "../types";
import { slack } from "@trigger.dev/providers";

import debug from "debug";
import { getAccessToken } from "../accessInfo";
import { z } from "zod";
Expand Down Expand Up @@ -66,7 +65,9 @@ class SlackRequestIntegration implements RequestIntegration {
switch (endpoint) {
case "chat.postMessage": {
return {
title: `Post message to #${params.channel}`,
title: `Post message to ${
"channelName" in params ? params.channelName : params.channelId
}`,
properties: [
{
key: "Text",
Expand All @@ -86,7 +87,7 @@ class SlackRequestIntegration implements RequestIntegration {
params: any,
cache?: CacheService
): Promise<PerformedRequestResponse> {
const parsedParams = slack.schemas.PostMessageBodySchema.parse(params);
const parsedParams = slack.schemas.PostMessageOptionsSchema.parse(params);

log("chat.postMessage %O", parsedParams);

Expand All @@ -97,18 +98,30 @@ class SlackRequestIntegration implements RequestIntegration {
baseUrl: this.baseUrl,
});

const channel = await this.#findChannelId(
service,
parsedParams.channel,
cache
);
const channelId = await this.#findChannelId(service, params, cache);

if (!channelId) {
return {
ok: false,
isRetryable: false,
response: {
output: {
message: `channelId not found`,
},
context: {
statusCode: 404,
headers: {},
},
},
};
}

log("found channelId %s", channel);
log("found channelId %s", channelId);

const response = await service.performRequest(this.#postMessageEndpoint, {
...parsedParams,
link_names: 1,
channel,
channel: channelId,
});

if (!response.success) {
Expand All @@ -118,7 +131,7 @@ class SlackRequestIntegration implements RequestIntegration {
ok: false,
isRetryable: this.#isRetryable(response.statusCode),
response: {
output: null,
output: {},
context: {
statusCode: response.statusCode,
headers: response.headers,
Expand All @@ -130,19 +143,19 @@ class SlackRequestIntegration implements RequestIntegration {
if (!response.data.ok && response.data.error === "not_in_channel") {
log(
"chat.postMessage failed with not_in_channel, attempting to join channel %s",
channel
channelId
);

// Attempt to join the channel, and then retry the request
const joinResponse = await service.performRequest(
this.#joinChannelEndpoint,
{
channel,
channel: channelId,
}
);

if (joinResponse.success && joinResponse.data.ok) {
log("joined channel %s, retrying postMessage", channel);
log("joined channel %s, retrying postMessage", channelId);

return this.#postMessage(accessInfo, params);
}
Expand Down Expand Up @@ -182,14 +195,23 @@ class SlackRequestIntegration implements RequestIntegration {
// unless the channel is already provided in the format of a channelID (for example: "D8572TUFR" or "C01BQJZLJGZ")
async #findChannelId(
service: HttpService,
channel: string,
params: z.infer<typeof slack.schemas.ChannelNameOrIdSchema>,
cache?: CacheService
): Promise<string> {
if (channel.startsWith("C") || channel.startsWith("D")) {
return channel;
): Promise<string | undefined> {
if ("channelId" in params) {
return params.channelId;
}

const cachedChannelId = await cache?.get(channel);
if (!("channelName" in params)) {
throw new Error("Invalid params, mising channelId and channelName");
}

//if the channelName starts with a #, remove it
if (params.channelName.startsWith("#")) {
params.channelName = params.channelName.substring(1);
}

const cachedChannelId = await cache?.get(params.channelName);

if (cachedChannelId) {
return cachedChannelId;
Expand All @@ -202,16 +224,17 @@ class SlackRequestIntegration implements RequestIntegration {
if (response.success && response.data.ok) {
const { channels } = response.data;

const channelInfo = channels.find((c: any) => c.name === channel);
const channelInfo = channels.find(
(c: any) => c.name === params.channelName
);

if (channelInfo) {
await cache?.set(channel, channelInfo.id, 60 * 60 * 24);
await cache?.set(params.channelName, channelInfo.id, 60 * 60 * 24);
return channelInfo.id;
}

return channelInfo?.id || channel;
}

return channel;
return undefined;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/internal-integrations/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface NormalizedRequest {
}

export interface NormalizedResponse {
output: any;
output: NonNullable<any>;
context: any;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/trigger-integrations/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @trigger.dev/integrations

## 0.1.6

### Patch Changes

- ce0d4b9: When posting a message to Slack, you must explicitly specify either channelId or channelName
- Updated dependencies [ce0d4b9]
- @trigger.dev/providers@0.1.4
- @trigger.dev/sdk@0.2.3

## 0.1.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/trigger-integrations/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trigger.dev/integrations",
"version": "0.1.5",
"version": "0.1.6",
"description": "trigger.dev integrations",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
import { slack } from "@trigger.dev/providers";

export type PostMessageOptions = z.infer<
typeof slack.schemas.PostMessageBodySchema
typeof slack.schemas.PostMessageOptionsSchema
>;

export type PostMessageResponse = z.infer<
Expand Down
6 changes: 6 additions & 0 deletions packages/trigger-providers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @trigger.dev/providers

## 0.1.4

### Patch Changes

- ce0d4b9: When posting a message to Slack, you must explicitly specify either channelId or channelName

## 0.1.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/trigger-providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trigger.dev/providers",
"version": "0.1.3",
"version": "0.1.4",
"description": "trigger.dev API providers with schemas",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
10 changes: 9 additions & 1 deletion packages/trigger-providers/src/providers/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ export const slack = {
enabledFor: "all",
authentication: {
type: "oauth",
scopes: ["channels:read", "channels:join", "chat:write"],
scopes: [
"channels:read",
"channels:join",
"channels:manage",
"chat:write",
"groups:write",
"im:write",
"mpim:write",
],
},
schemas,
};
11 changes: 11 additions & 0 deletions packages/trigger-providers/src/providers/slack/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export const PostMessageBodySchema = z.object({
text: z.string(),
});

export const ChannelNameOrIdSchema = z.union([
z.object({ channelId: z.string() }),
z.object({ channelName: z.string() }),
]);

export const PostMessageOptionsSchema = z
.object({
text: z.string(),
})
.and(ChannelNameOrIdSchema);

export const JoinConversationSuccessResponseSchema = z.object({
ok: z.literal(true),
channel: z.object({
Expand Down
6 changes: 6 additions & 0 deletions packages/trigger-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @trigger.dev/sdk

## 0.2.3

### Patch Changes

- ce0d4b9: When posting a message to Slack, you must explicitly specify either channelId or channelName

## 0.2.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/trigger-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trigger.dev/sdk",
"version": "0.2.2",
"version": "0.2.3",
"description": "trigger.dev Node.JS SDK",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down