Skip to content

Commit 7eeb1cb

Browse files
committed
fix: slack urgent fix for posting
1 parent 5bfb97e commit 7eeb1cb

File tree

1 file changed

+122
-35
lines changed

1 file changed

+122
-35
lines changed

libraries/nestjs-libraries/src/integrations/social/slack.provider.ts

Lines changed: 122 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -137,55 +137,142 @@ export class SlackProvider extends SocialAbstract implements SocialProvider {
137137
postDetails: PostDetails[],
138138
integration: Integration
139139
): Promise<PostResponse[]> {
140+
const [firstPost] = postDetails;
141+
const channel = firstPost.settings.channel;
142+
143+
// Join the channel first
140144
await fetch(`https://slack.com/api/conversations.join`, {
141145
method: 'POST',
142146
headers: {
143147
Authorization: `Bearer ${accessToken}`,
144148
'Content-Type': 'application/json',
145149
},
146150
body: JSON.stringify({
147-
channel: postDetails[0].settings.channel,
151+
channel,
148152
}),
149153
});
150154

151-
let lastId = '';
152-
for (const post of postDetails) {
153-
const { ts } = await (
154-
await fetch(`https://slack.com/api/chat.postMessage`, {
155-
method: 'POST',
155+
// Post the main message
156+
const { ts, channel: responseChannel } = await (
157+
await fetch(`https://slack.com/api/chat.postMessage`, {
158+
method: 'POST',
159+
headers: {
160+
Authorization: `Bearer ${accessToken}`,
161+
'Content-Type': 'application/json',
162+
},
163+
body: JSON.stringify({
164+
channel,
165+
username: integration.name,
166+
icon_url: integration.picture,
167+
blocks: [
168+
{
169+
type: 'section',
170+
text: {
171+
type: 'mrkdwn',
172+
text: firstPost.message,
173+
},
174+
},
175+
...(firstPost.media?.length
176+
? firstPost.media.map((m) => ({
177+
type: 'image',
178+
image_url: m.path,
179+
alt_text: '',
180+
}))
181+
: []),
182+
],
183+
}),
184+
})
185+
).json();
186+
187+
// Get permalink for the message
188+
const { permalink } = await (
189+
await fetch(
190+
`https://slack.com/api/chat.getPermalink?channel=${responseChannel}&message_ts=${ts}`,
191+
{
192+
method: 'GET',
156193
headers: {
157194
Authorization: `Bearer ${accessToken}`,
158-
'Content-Type': 'application/json',
159195
},
160-
body: JSON.stringify({
161-
channel: postDetails[0].settings.channel,
162-
username: integration.name,
163-
icon_url: integration.picture,
164-
...(lastId ? { thread_ts: lastId } : {}),
165-
blocks: [
166-
{
167-
type: 'section',
168-
text: {
169-
type: 'mrkdwn',
170-
text: post.message,
171-
},
196+
}
197+
)
198+
).json();
199+
200+
return [
201+
{
202+
id: firstPost.id,
203+
postId: ts,
204+
releaseURL: permalink || '',
205+
status: 'posted',
206+
},
207+
];
208+
}
209+
210+
async comment(
211+
id: string,
212+
postId: string,
213+
lastCommentId: string | undefined,
214+
accessToken: string,
215+
postDetails: PostDetails[],
216+
integration: Integration
217+
): Promise<PostResponse[]> {
218+
const [commentPost] = postDetails;
219+
const channel = commentPost.settings.channel;
220+
const threadTs = lastCommentId || postId;
221+
222+
// Post the threaded reply
223+
const { ts, channel: responseChannel } = await (
224+
await fetch(`https://slack.com/api/chat.postMessage`, {
225+
method: 'POST',
226+
headers: {
227+
Authorization: `Bearer ${accessToken}`,
228+
'Content-Type': 'application/json',
229+
},
230+
body: JSON.stringify({
231+
channel,
232+
username: integration.name,
233+
icon_url: integration.picture,
234+
thread_ts: threadTs,
235+
blocks: [
236+
{
237+
type: 'section',
238+
text: {
239+
type: 'mrkdwn',
240+
text: commentPost.message,
172241
},
173-
...(post.media?.length
174-
? post.media.map((m) => ({
175-
type: 'image',
176-
image_url: m.path,
177-
alt_text: '',
178-
}))
179-
: []),
180-
],
181-
}),
182-
})
183-
).json();
184-
185-
lastId = ts;
186-
}
187-
188-
return [];
242+
},
243+
...(commentPost.media?.length
244+
? commentPost.media.map((m) => ({
245+
type: 'image',
246+
image_url: m.path,
247+
alt_text: '',
248+
}))
249+
: []),
250+
],
251+
}),
252+
})
253+
).json();
254+
255+
// Get permalink for the comment
256+
const { permalink } = await (
257+
await fetch(
258+
`https://slack.com/api/chat.getPermalink?channel=${responseChannel}&message_ts=${ts}`,
259+
{
260+
method: 'GET',
261+
headers: {
262+
Authorization: `Bearer ${accessToken}`,
263+
},
264+
}
265+
)
266+
).json();
267+
268+
return [
269+
{
270+
id: commentPost.id,
271+
postId: ts,
272+
releaseURL: permalink || '',
273+
status: 'posted',
274+
},
275+
];
189276
}
190277

191278
async changeProfilePicture(id: string, accessToken: string, url: string) {

0 commit comments

Comments
 (0)