Skip to content

Commit

Permalink
Using the standard connectors config API for Slack bot enabled (#3133)
Browse files Browse the repository at this point in the history
* Using the standard connectors config API for Slack bot enabled

* Remove unused file
  • Loading branch information
lasryaric authored Jan 11, 2024
1 parent 6bef353 commit a7c0041
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 241 deletions.
122 changes: 0 additions & 122 deletions connectors/src/api/bot_enabled.ts

This file was deleted.

6 changes: 0 additions & 6 deletions connectors/src/api_server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import bodyParser from "body-parser";
import express from "express";

import {
getBotEnabledAPIHandler,
setBotEnabledAPIHandler,
} from "@connectors/api/bot_enabled";
import { createConnectorAPIHandler } from "@connectors/api/create_connector";
import { deleteConnectorAPIHandler } from "@connectors/api/delete_connector";
import { getConnectorAPIHandler } from "@connectors/api/get_connector";
Expand Down Expand Up @@ -65,8 +61,6 @@ export function startServer(port: number) {
app.post("/connectors/resume/:connector_id", resumeConnectorAPIHandler);
app.delete("/connectors/delete/:connector_id", deleteConnectorAPIHandler);
app.get("/connectors/:connector_id", getConnectorAPIHandler);
app.get("/connectors/:connector_id/bot_enabled", getBotEnabledAPIHandler);
app.post("/connectors/:connector_id/bot_enabled", setBotEnabledAPIHandler);
app.post("/connectors/sync/:connector_id", syncConnectorAPIHandler);
app.get(
"/connectors/:connector_id/permissions",
Expand Down
59 changes: 6 additions & 53 deletions connectors/src/connectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectorProvider, ModelId } from "@dust-tt/types";
import { ConnectorProvider } from "@dust-tt/types";

import {
cleanupGithubConnector,
Expand Down Expand Up @@ -34,8 +34,6 @@ import {
updateIntercomConnector,
} from "@connectors/connectors/intercom";
import {
BotEnabledGetter,
BotToggler,
ConnectorBatchResourceTitleRetriever,
ConnectorCleaner,
ConnectorConfigGetter,
Expand Down Expand Up @@ -65,17 +63,15 @@ import {
import {
cleanupSlackConnector,
createSlackConnector,
getSlackConfig,
retrieveSlackChannelsTitles,
retrieveSlackConnectorPermissions,
setSlackConfig,
setSlackConnectorPermissions,
updateSlackConnector,
} from "@connectors/connectors/slack";
import {
getBotEnabled,
toggleSlackbot,
} from "@connectors/connectors/slack/bot";
import { launchSlackSyncWorkflow } from "@connectors/connectors/slack/temporal/client";
import { Err, Ok, Result } from "@connectors/lib/result";
import { Err, Ok } from "@connectors/lib/result";
import logger from "@connectors/logger/logger";

import {
Expand Down Expand Up @@ -165,45 +161,6 @@ export const RESUME_CONNECTOR_BY_TYPE: Record<
},
};

const toggleBotNotImplemented = async (
connectorId: ModelId
): Promise<Result<void, Error>> => {
return new Err(
new Error(`Toggling bot for connector ${connectorId} is not implemented.`)
);
};

export const TOGGLE_BOT_BY_TYPE: Record<ConnectorProvider, BotToggler> = {
slack: toggleSlackbot,
notion: toggleBotNotImplemented,
github: toggleBotNotImplemented,
google_drive: toggleBotNotImplemented,
intercom: toggleBotNotImplemented,
webcrawler: toggleBotNotImplemented,
};

const getBotEnabledNotImplemented = async (
connectorId: ModelId
): Promise<Result<boolean, Error>> => {
return new Err(
new Error(
`Getting botEnabled for connector ${connectorId} is not implemented.`
)
);
};

export const GET_BOT_ENABLED_BY_TYPE: Record<
ConnectorProvider,
BotEnabledGetter
> = {
slack: getBotEnabled,
notion: getBotEnabledNotImplemented,
github: getBotEnabledNotImplemented,
google_drive: getBotEnabledNotImplemented,
intercom: getBotEnabledNotImplemented,
webcrawler: getBotEnabledNotImplemented,
};

export const SYNC_CONNECTOR_BY_TYPE: Record<ConnectorProvider, SyncConnector> =
{
slack: launchSlackSyncWorkflow,
Expand Down Expand Up @@ -285,9 +242,7 @@ export const SET_CONNECTOR_CONFIG_BY_TYPE: Record<
ConnectorProvider,
ConnectorConfigSetter
> = {
slack: () => {
throw new Error("Not implemented");
},
slack: setSlackConfig,
notion: async () => {
throw new Error("Not implemented");
},
Expand All @@ -307,9 +262,7 @@ export const GET_CONNECTOR_CONFIG_BY_TYPE: Record<
ConnectorProvider,
ConnectorConfigGetter
> = {
slack: () => {
throw new Error("Not implemented");
},
slack: getSlackConfig,
notion: async () => {
throw new Error("Not implemented");
},
Expand Down
9 changes: 0 additions & 9 deletions connectors/src/connectors/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ export type ConnectorResumer = (
connectorId: string
) => Promise<Result<string, Error>>;

export type BotToggler = (
connectorId: ModelId,
botEnabled: boolean
) => Promise<Result<void, Error>>;

export type BotEnabledGetter = (
connectorId: ModelId
) => Promise<Result<boolean, Error>>;

export type SyncConnector = (
connectorId: string,
fromTs: number | null
Expand Down
57 changes: 56 additions & 1 deletion connectors/src/connectors/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { ModelId } from "@dust-tt/types";
import { WebClient } from "@slack/web-api";
import PQueue from "p-queue";

import { ConnectorPermissionRetriever } from "@connectors/connectors/interface";
import {
ConnectorConfigGetter,
ConnectorPermissionRetriever,
} from "@connectors/connectors/interface";
import { getChannels } from "@connectors/connectors/slack//temporal/activities";
import {
getBotEnabled,
toggleSlackbot,
} from "@connectors/connectors/slack/bot";
import { joinChannel } from "@connectors/connectors/slack/lib/channels";
import {
getSlackAccessToken,
Expand Down Expand Up @@ -629,3 +636,51 @@ export async function retrieveSlackChannelsTitles(

return new Ok(titles);
}

export const getSlackConfig: ConnectorConfigGetter = async function (
connectorId: ModelId,
configKey: string
) {
const connector = await Connector.findOne({
where: { id: connectorId },
});
if (!connector) {
return new Err(new Error(`Connector not found with id ${connectorId}`));
}

switch (configKey) {
case "botEnabled": {
const botEnabledRes = await getBotEnabled(connectorId);
if (botEnabledRes.isErr()) {
return botEnabledRes;
}
return new Ok(botEnabledRes.value.toString());
}
default:
return new Err(new Error(`Invalid config key ${configKey}`));
}
};

export async function setSlackConfig(
connectorId: ModelId,
configKey: string,
configValue: string
) {
const connector = await Connector.findOne({
where: { id: connectorId },
});
if (!connector) {
return new Err(new Error(`Connector not found with id ${connectorId}`));
}

switch (configKey) {
case "botEnabled": {
const res = await toggleSlackbot(connectorId, configValue === "true");
return res;
}

default: {
return new Err(new Error(`Invalid config key ${configKey}`));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function handler(
},
});
}
const { botEnabled } = req.body;
const { botEnabled }: { botEnabled: boolean } = req.body;

const dataSource = await DataSource.findOne({
where: {
Expand Down Expand Up @@ -75,9 +75,10 @@ async function handler(
}

const connectorsAPI = new ConnectorsAPI(logger);
const connectorRes = await connectorsAPI.setBotEnabled(
const connectorRes = await connectorsAPI.setConnectorConfig(
dataSource.connectorId,
botEnabled
"botEnabled",
botEnabled ? "true" : "false"
);

if (connectorRes.isErr()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ async function handler(

switch (req.method) {
case "GET":
const botEnabledRes = await connectorsAPI.getBotEnabled(
dataSource.connectorId
const botEnabledRes = await connectorsAPI.getConnectorConfig(
dataSource.connectorId,
"botEnabled"
);

if (botEnabledRes.isErr()) {
Expand All @@ -89,7 +90,9 @@ async function handler(
});
}

res.status(200).json(botEnabledRes.value);
res
.status(200)
.json({ botEnabled: botEnabledRes.value.configValue === "true" });
return;

case "POST":
Expand All @@ -116,9 +119,10 @@ async function handler(
});
}

const setBotEnabledRes = await connectorsAPI.setBotEnabled(
const setBotEnabledRes = await connectorsAPI.setConnectorConfig(
dataSource.connectorId,
bodyValidation.right.botEnabled
"botEnabled",
bodyValidation.right.botEnabled ? "true" : "false"
);

if (setBotEnabledRes.isErr()) {
Expand Down
Loading

0 comments on commit a7c0041

Please sign in to comment.