import axios from 'axios';
interface SlackActivityCreateParams {
text: string;
sourceURL: string;
}
function createActivityMessage(params: SlackActivityCreateParams) {
return {
type: 'activity',
data: {
text: params.text,
sourceURL: params.sourceURL,
},
};
}
async function getMessage(accessToken: string, channel: string, ts: string) {
const result = await axios.get('https://slack.com/api/conversations.history', {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
params: {
channel,
latest: ts,
inclusive: true,
limit: 1,
},
});
return result.data.messages?.[0];
}
async function getConversationInfo(accessToken: string, channel: string) {
const result = await axios.get('https://slack.com/api/conversations.info', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
params: { channel },
});
return result.data.channel;
}
async function getPermalink(accessToken: string, channel: string, ts: string) {
return await axios.get(
`https://slack.com/api/chat.getPermalink?channel=${channel}&message_ts=${ts}`,
{
headers: { Authorization: `Bearer ${accessToken}` },
}
);
}
export const createActivityEvent = async (eventData: any, config: any) {
const event = eventData.event;
if (!config) {
throw new Error('Integration configuration not found');
}
const accessToken = config.access_token;
// Handle reaction_added events
if (event.type === 'reaction_added' && event.reaction === 'eyes') {
const channel = event.item.channel;
const ts = event.item.ts;
// Fetch message and context
const [eventMessage, conversationInfo] = await Promise.all([
getMessage(accessToken, channel, ts),
getConversationInfo(accessToken, channel)
]);
// Build activity text
const text = `User reacted with eyes emoji in channel ${conversationInfo.name}. ` +
`Content: '${eventMessage.text}'`;
// Get permalink
const permalinkResponse = await getPermalink(accessToken, channel, ts);
return [createActivityMessage({
text,
sourceURL: permalinkResponse.data.permalink
})];
}
// Handle message events
if (event.type === 'message') {
const text = `Message in channel: '${event.text}'`;
const permalinkResponse = await getPermalink(accessToken, event.channel, event.ts);
return [createActivityMessage({
text,
sourceURL: permalinkResponse.data.permalink
})];
}
// Return empty array for unhandled events
return [];
};