Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit 3e25301

Browse files
committed
Update the app
1 parent 6797683 commit 3e25301

File tree

7 files changed

+76
-24
lines changed

7 files changed

+76
-24
lines changed

.vscode/settings.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
{
2-
"deno.enable": true
3-
}
2+
"deno.enable": true,
3+
"deno.lint": true,
4+
"deno.suggest.imports.hosts": {
5+
"https://deno.land": false
6+
},
7+
"[typescript]": {
8+
"editor.formatOnSave": true,
9+
"editor.defaultFormatter": "denoland.vscode-deno"
10+
},
11+
"editor.tabSize": 2
12+
}

README.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# run-on-slack-deepl
22

3-
This sample app is a translator app that runs [DeepL's awesome APIs](https://www.deepl.com/en/docs-api) for translating Slack message text into a different language.
3+
This sample app is a translator app that runs
4+
[DeepL's awesome APIs](https://www.deepl.com/en/docs-api) for translating Slack
5+
message text into a different language.
46

57
## Steps to enable this app
68

79
### 0. Enable the next-gen platform in your workspace
810

9-
To use this sample, you first need to install and configure the Slack CLI. Step-by-step instructions can be found in [Quickstart Guide](https://api.slack.com/future/quickstart). Also, the beta platform needs to be enabled for your paid Slack workspace.
11+
To use this sample, you first need to install and configure the Slack CLI.
12+
Step-by-step instructions can be found in
13+
[Quickstart Guide](https://api.slack.com/future/quickstart). Also, the beta
14+
platform needs to be enabled for your paid Slack workspace.
1015

1116
And then, you can use this GitHub repository as the template for your app.
1217

@@ -17,7 +22,9 @@ cd my-deepl-slack-app/
1722

1823
### 1. Enable reaction_added trigger
1924

20-
First off, open `triggers/reaction_added.ts` source file and modify it to have a valid list of channel IDs to enable this app. And then, run the following CLI command:
25+
First off, open `triggers/reaction_added.ts` source file and modify it to have a
26+
valid list of channel IDs to enable this app. And then, run the following CLI
27+
command:
2128

2229
```bash
2330
slack deploy
@@ -38,10 +45,11 @@ Add the deployed app to the channels you've listed in the step 1.
3845

3946
### 4. Add a reaction to a message in the channel
4047

41-
Add `:jp:` reaction to any message in the channel. If everything is fine, you will see the same content that is translated into Japanese in its thread.
48+
Add `:jp:` reaction to any message in the channel. If everything is fine, you
49+
will see the same content that is translated into Japanese in its thread.
4250

4351
<img width="300" src="https://user-images.githubusercontent.com/19658/192277306-b3a2f431-1b8b-44e0-9b6a-224ca09a4b6e.png">
4452

4553
## LICENSE
4654

47-
The MIT License
55+
The MIT License

functions/lang_selector.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { DefineFunction, Schema } from "deno-slack-sdk/mod.ts";
22
import { SlackFunctionHandler } from "deno-slack-sdk/types.ts";
33
import { reactionToLang } from "./languages.ts";
4+
import { getLogger } from "../utils/logger.ts";
5+
import { resolveFunctionSourceFile } from "../utils/source_file_resoluion.ts";
46

57
export const def = DefineFunction({
68
callback_id: "lang_selector",
79
title: "Language selector",
810
description: "A funtion to identify the language to translate into",
9-
source_file: "functions/lang_selector.ts",
11+
source_file: resolveFunctionSourceFile(import.meta.url),
1012
input_parameters: {
1113
properties: {
1214
reaction: {
@@ -27,8 +29,10 @@ export const def = DefineFunction({
2729

2830
const handler: SlackFunctionHandler<typeof def.definition> = async ({
2931
inputs,
32+
env,
3033
}) => {
31-
console.log(`lang_selector function called: ${JSON.stringify(inputs)}`);
34+
const logger = await getLogger(env.logLevel);
35+
logger.debug(`lang_selector inputs: ${JSON.stringify(inputs)}`);
3236
const reactionName = inputs.reaction;
3337
let lang = undefined;
3438
if (reactionName.match(/flag-/)) {

functions/translator.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { DefineFunction, Schema } from "deno-slack-sdk/mod.ts";
22
import { SlackFunctionHandler } from "deno-slack-sdk/types.ts";
33
import { SlackAPI } from "deno-slack-api/mod.ts";
44
import { SlackAPIClient } from "deno-slack-api/types.ts";
5+
import { getLogger } from "../utils/logger.ts";
6+
import { resolveFunctionSourceFile } from "../utils/source_file_resoluion.ts";
7+
import * as log from "https://deno.land/std@0.157.0/log/mod.ts";
58

69
export const def = DefineFunction({
710
callback_id: "translator",
811
title: "Translator",
912
description: "A funtion to translate a Slack message",
10-
source_file: "functions/translator.ts",
13+
source_file: resolveFunctionSourceFile(import.meta.url),
1114
input_parameters: {
1215
properties: {
1316
channelId: {
@@ -37,11 +40,12 @@ const handler: SlackFunctionHandler<typeof def.definition> = async ({
3740
token,
3841
env,
3942
}) => {
40-
console.log(`translator function called: ${JSON.stringify(inputs)}`);
43+
const logger = await getLogger(env.logLevel);
44+
logger.debug(`translator inputs: ${JSON.stringify(inputs)}`);
4145
const emptyOutputs = { outputs: {} };
4246
if (inputs.lang === undefined) {
4347
// no language specified by the reaction
44-
console.log(`Skipped as no lang detected`);
48+
logger.info(`Skipped as no lang detected`);
4549
return emptyOutputs;
4650
}
4751
const client: SlackAPIClient = SlackAPI(token);
@@ -53,13 +57,13 @@ const handler: SlackFunctionHandler<typeof def.definition> = async ({
5357
});
5458
if (translationTarget.error) {
5559
// If you see this log message, you need to invite this app to the channel
56-
console.log(
57-
`Failed to fetch the message due to ${translationTarget.error}`
60+
logger.info(
61+
`Failed to fetch the message due to ${translationTarget.error}`,
5862
);
5963
return emptyOutputs;
6064
}
6165
if (translationTarget.messages.length == 0) {
62-
console.log(`No message found`);
66+
logger.info("No message found");
6367
return emptyOutputs;
6468
}
6569
const authKey = env.DEEPL_AUTH_KEY;
@@ -78,26 +82,26 @@ const handler: SlackFunctionHandler<typeof def.definition> = async ({
7882
});
7983
const translationResult = await deeplResponse.json();
8084
if (!translationResult || translationResult.translations.length === 0) {
81-
console.log(`Translation failed for some reason: ${translationResult}`);
85+
logger.info(`Translation failed for some reason: ${translationResult}`);
8286
return emptyOutputs;
8387
}
8488
const translatedText = translationResult.translations[0].text;
8589
const replies = await client.conversations.replies({
8690
channel: inputs.channelId,
8791
ts: inputs.messageTs,
8892
});
89-
if (isAlreadyPosted(replies.messages, translatedText)) {
93+
if (isAlreadyPosted(logger, replies.messages, translatedText)) {
9094
// Skip posting the same one
91-
console.log(
92-
`Skipped this translation as it's already posted: ${translatedText}`
95+
logger.info(
96+
`Skipped this translation as it's already posted: ${translatedText}`,
9397
);
9498
return emptyOutputs;
9599
}
96100
const result = await sayInThread(
97101
client,
98102
inputs.channelId,
99103
inputs.messageTs,
100-
translatedText
104+
translatedText,
101105
);
102106
return await { outputs: { ts: result.ts } };
103107
};
@@ -106,11 +110,12 @@ export default handler;
106110
// internal functions
107111

108112
function isAlreadyPosted(
113+
logger: log.Logger,
109114
// deno-lint-ignore no-explicit-any
110115
replies: Record<string, any>[],
111-
translatedText: string
116+
translatedText: string,
112117
): boolean {
113-
console.log(replies);
118+
logger.debug(replies);
114119
if (!replies) {
115120
return false;
116121
}
@@ -126,7 +131,7 @@ async function sayInThread(
126131
client: SlackAPIClient,
127132
channelId: string,
128133
threadTs: string,
129-
text: string
134+
text: string,
130135
) {
131136
return await client.chat.postMessage({
132137
channel: channelId,

triggers/reaction_added.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const trigger: Trigger<typeof workflowDef.definition> = {
99
event: {
1010
event_type: "slack#/events/reaction_added",
1111
// TODO: Listing all the channels to enable here is required
12-
channel_ids: ["C013T0FTKU3"],
12+
channel_ids: ["CLT1F93TP"],
1313
},
1414
inputs: {
1515
channelId: {

utils/logger.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as log from "https://deno.land/std@0.157.0/log/mod.ts";
2+
3+
export async function getLogger(
4+
level?: string,
5+
): Promise<log.Logger> {
6+
const logLevel: log.LevelName = level === undefined
7+
? "DEBUG"
8+
: level as log.LevelName;
9+
await log.setup({
10+
handlers: {
11+
console: new log.handlers.ConsoleHandler(logLevel),
12+
},
13+
loggers: {
14+
default: {
15+
level: logLevel,
16+
handlers: ["console"],
17+
},
18+
},
19+
});
20+
return log.getLogger();
21+
}

utils/source_file_resoluion.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function resolveFunctionSourceFile(importMetaUrl: string): string {
2+
return new URL("", importMetaUrl).pathname.split("/").slice(-2).join(
3+
"/",
4+
);
5+
}

0 commit comments

Comments
 (0)