Last active
April 20, 2024 16:46
-
-
Save sibelius/ab3104054b981084b85a14185873057b to your computer and use it in GitHub Desktop.
basic script to tag people that forget to answer daily thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { slackApp } from '../src/slackApp'; | |
import { generalChannelId } from '../src/slackChannels'; | |
import { config } from '../src/config'; | |
import { | |
filterBots, | |
replaceMembers, | |
} from '../src/slack/membersMapping'; | |
import { removeRepliedUsers } from '../src/commandMessage/dailyThreadCommand'; | |
import { unique } from '../src/core/unique'; | |
import { slackAllMembers } from '../src/slack/slackAllMembers'; | |
import { App, LogLevel } from '@slack/bolt'; | |
import { config } from './config'; | |
export const slackApp = new App({ | |
token: config.TOKEN, | |
appToken: config.APP_TOKEN, | |
signingSecret: config.SIGNING_SECRET_TOKEN, | |
socketMode: true, | |
port: config.PORT || 3000, | |
logLevel: config.LOG_LEVEL, | |
}); | |
const isDailyThread = (text: string) => { | |
return text.includes('Daily Thread'); | |
}; | |
export const getReminderDailyThreadMessage = (messages) => { | |
for (const message of messages) { | |
if (isDailyThread(message.text)) { | |
return message; | |
} | |
} | |
return null; | |
}; | |
/** | |
from command | |
{ | |
message: { | |
client_msg_id: '9d0b74fb-b1b3-4b49-809a-575f09f91f77', | |
type: 'message', | |
text: 'Reminder: Daily thread.', | |
user: 'U0XHU2KGF', | |
ts: '1677757362.298299', | |
blocks: [ [Object] ], | |
team: 'T04J5Q6SJ', | |
channel: 'C3SL4GMTK', | |
event_ts: '1677757362.298299', | |
channel_type: 'channel' | |
} | |
} | |
from history | |
{ | |
dailyThreadMessage: { | |
bot_id: 'B01', | |
type: 'message', | |
text: 'Reminder: Daily thread.', | |
user: 'USLACKBOT', | |
ts: '1677754823.838879', | |
blocks: [ [Object] ], | |
team: 'T04J5Q6SJ' | |
} | |
} | |
**/ | |
// yarn es ./scripts/daily.ts | |
const run = async () => { | |
const result = await slackApp.client.conversations.history({ | |
channel: generalChannelId, | |
}); | |
const dailyThreadMessage = getReminderDailyThreadMessage(result.messages); | |
const replies = await slackApp.client.conversations.replies({ | |
channel: generalChannelId, | |
ts: dailyThreadMessage.ts, | |
}); | |
const usersReplied = unique(filterBots(replies.messages.map((m) => m.user))); | |
if (usersReplied.length === slackAllMembers.length) { | |
// eslint-disable-next-line | |
console.log('all members replied'); | |
return; | |
} | |
const messageToSend = removeRepliedUsers( | |
replaceMembers(slackAllMembers.join(' ')), | |
usersReplied, | |
).trim(); | |
if (!messageToSend) { | |
// eslint-disable-next-line | |
console.log('all members replied'); | |
return; | |
} | |
if (!dailyThreadMessage) { | |
// eslint-disable-next-line | |
console.log('no daily thread message'); | |
return; | |
} | |
// eslint-disable-next-line | |
console.log({ | |
messageToSend, | |
}); | |
await slackApp.client.chat.postMessage({ | |
token: config.TOKEN, | |
channel: generalChannelId, | |
text: messageToSend, | |
thread_ts: dailyThreadMessage.ts, | |
}); | |
}; | |
(async () => { | |
try { | |
await run(); | |
} catch (err) { | |
// eslint-disable-next-line | |
console.log({ | |
err, | |
}); | |
process.exit(1); | |
} | |
process.exit(0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment