-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupsertSlackMessage.ts
More file actions
227 lines (209 loc) · 7.07 KB
/
upsertSlackMessage.ts
File metadata and controls
227 lines (209 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env bun --hot
import { getSlack, isSlackAvailable } from "@/lib/slack";
import { getSlackChannel } from "@/lib/slack/channels";
import KeyvSqlite from "@keyv/sqlite";
import DIE from "@snomiao/die";
import chalk from "chalk";
import Keyv from "keyv";
import { slackMessageUrlParse, slackMessageUrlStringify } from "../gh-design/slackMessageUrlParse";
import { COMFY_PR_CACHE_DIR } from "./COMFY_PR_CACHE_DIR";
import * as prettier from "prettier";
import { slack } from "@/lib";
// Detect test environment - use in-memory cache to avoid SQLite issues
const isTestEnv = process.env.NODE_ENV === "test" || process.env.CI === "true" || !!process.env.CI;
const SlackChannelIdsCache = isTestEnv
? new Keyv<string>()
: new Keyv<string>(new KeyvSqlite("sqlite://" + COMFY_PR_CACHE_DIR + "/slackChannelIdCache.sqlite"));
const _SlackUserIdsCache = isTestEnv
? new Keyv<string>()
: new Keyv<string>(new KeyvSqlite("sqlite://" + COMFY_PR_CACHE_DIR + "/slackUserIdCache.sqlite"));
/**
* Slack message length limits
* - Text in messages: 40,000 characters
* - Text in markdown blocks: 12,000 characters cumulative
* - We use conservative limits to be safe
*/
const _SLACK_TEXT_LIMIT = 35000; // Conservative limit for text parameter
const _SLACK_MARKDOWN_BLOCK_LIMIT = 11000; // Conservative limit for markdown blocks
/**
* Truncate text from the middle, preserving start and end
*/
function _truncateFromMiddle(text: string, maxLength: number): string {
if (text.length <= maxLength) {
return text;
}
const truncationMarker = "\n\n...TRUNCATED...\n\n";
const markerLength = truncationMarker.length;
const halfLength = Math.floor((maxLength - markerLength) / 2);
return text.slice(0, halfLength) + truncationMarker + text.slice(-halfLength);
}
/**
* Create or update existing slack message
*
* Note: the returned value 'channel' is a channel id, not name
*
* To tag a user, use <@UserId>, e.g. <@snomiao>
*/
export async function upsertSlackMessage({
text,
channel,
channelName,
url,
replyUrl,
reply_broadcast,
}: {
text: string;
/** channelId */
channel?: string;
channelName?: string;
url?: string;
replyUrl?: string;
reply_broadcast?: boolean;
}) {
const slack = getSlack();
if (channelName) {
channel ||=
(await SlackChannelIdsCache.get(channelName)) ||
(await (async () => {
const ch = await getSlackChannel(channelName);
const id =
ch.id ||
DIE(`Got slack channel from ${channelName} but no id ` + JSON.stringify(channel));
await SlackChannelIdsCache.set(channelName, id);
return id;
})());
}
if (!channel) DIE(`No slack channel specified`);
if (!url) {
if (process.env.DRY_RUN) {
console.error("DRY RUN MODE");
console.error("sending text:", text);
throw new Error(chalk.red("Sending slack message to: " + JSON.stringify({ channel })));
}
const thread_ts = !replyUrl ? undefined : slackMessageUrlParse(replyUrl).ts;
const msg = !thread_ts
? await slack.chat.postMessage({ text, channel })
: await slack.chat.postMessage({
text,
channel,
thread_ts,
reply_broadcast: reply_broadcast ?? false,
});
const url = slackMessageUrlStringify({ channel, ts: msg.ts! });
return { ...msg, url, text, channel };
}
if (process.env.DRY_RUN) {
console.error("DRY RUN MODE");
console.error("sending text:", text);
throw new Error(chalk.red("Updating slack message to: " + JSON.stringify({ channel, url })));
}
const ts = slackMessageUrlParse(url).ts;
const msg = await slack.chat.update({ text, channel, ts });
return { ...msg, url, text, channel };
}
/**
* Create or update existing slack message
*
* Note: the returned value 'channel' is a channel id, not name
*
* Note: will always update if url is provided, even if the content is the same, so deduplication should be handled by caller if needed
*/
export async function upsertSlackMarkdownMessage({
markdown,
channel,
channelName,
url,
replyUrl,
reply_broadcast,
}: {
markdown: string;
/** channelId */
channel?: string;
channelName?: string;
url?: string;
replyUrl?: string;
reply_broadcast?: boolean;
}) {
// format the md before send
markdown = await mdFmt(markdown);
if (channelName) {
channel ||=
(await SlackChannelIdsCache.get(channelName)) ||
(await (async () => {
const ch = await getSlackChannel(channelName);
const id =
ch.id ||
DIE(`Got slack channel from ${channelName} but no id ` + JSON.stringify(channel));
await SlackChannelIdsCache.set(channelName, id);
return id;
})());
}
if (!channel) DIE(`No slack channel specified`);
if (!url) {
if (process.env.DRY_RUN) {
console.error("DRY RUN MODE");
console.error("sending markdown:", markdown);
throw new Error(chalk.red("Sending slack message to: " + JSON.stringify({ channel })));
}
const thread_ts = !replyUrl ? undefined : slackMessageUrlParse(replyUrl).ts;
const msg = !thread_ts
? await slack.chat.postMessage({
text: markdown,
channel,
blocks: [{ type: "markdown", text: markdown }],
})
: await slack.chat.postMessage({
text: markdown,
blocks: [{ type: "markdown", text: markdown }],
channel,
thread_ts,
reply_broadcast: reply_broadcast ?? false,
});
const url = slackMessageUrlStringify({ channel, ts: msg.ts! });
return { ...msg, url, text: markdown, channel };
}
if (process.env.DRY_RUN) {
console.error("DRY RUN MODE");
console.error("sending markdown:", markdown);
throw new Error(chalk.red("Updating slack message to: " + JSON.stringify({ channel, url })));
}
const ts = slackMessageUrlParse(url).ts;
const msg = await slack.chat.update({
text: markdown,
channel,
ts,
blocks: [{ type: "markdown", text: markdown }],
});
return { ...msg, url, text: markdown, channel };
}
if (import.meta.main) {
if (!isSlackAvailable()) {
console.log("Slack token not configured, skipping upsertSlackMessage test");
} else {
// const myMsg = slackMessageUrlParse('https://comfy-organization.slack.com/archives/C095SJWUYMR/p1755243753632819')
// post new message
const msg = await upsertSlackMessage({
channelName: "sno-test-channel",
text: "Hello @sno @snomiao <@sno> <@snomiao>, this is a test message from upsertSlackMessage function.",
});
// console.log(msg)
console.log(msg.url);
// // edit that by providing msg url
const msgEdited = await upsertSlackMessage({
...msg,
text: msg.text + "\nThis is a msg edit",
});
console.log(msgEdited);
// // reply that msg by providing reply url
// const msgReplied = await upsertSlackMessage({
// channel: msgEdited.channel,
// text: "Hello @snomiao, this is a reply to last message.",
// replyUrl: msgEdited.url,
// reply_broadcast: true,
// });
// console.log(msgReplied)
}
}
export async function mdFmt(md: string) {
return await prettier.format(md, { parser: "markdown" });
}