From 2d7e5bdcd48607b621a9cac303c2b6c9bda54fc8 Mon Sep 17 00:00:00 2001 From: Alan Date: Fri, 6 Oct 2023 16:06:57 -0300 Subject: [PATCH] Add temporary console logs for debugging --- src/commands/tarea/tarea.ts | 43 +++++++ src/events/message/messageNew.ts | 30 +++++ .../reaction/handleRobotFaceReaction.ts | 115 ++++++++++++++++++ 3 files changed, 188 insertions(+) diff --git a/src/commands/tarea/tarea.ts b/src/commands/tarea/tarea.ts index b509e96..b8b6bb4 100644 --- a/src/commands/tarea/tarea.ts +++ b/src/commands/tarea/tarea.ts @@ -46,18 +46,28 @@ export const tareaCommandFunction = async ({ respond, client, }: Command): Promise => { + console.log('HERE START TAREA COMMAND FUNCTION'); + console.log('command: ', command); + console.log('client: ', client); + await ack(); try { + console.log('HERE ENTERS IN TRY BLOCK'); const { user } = (await client.users.info({ user: command.user_id, })) as IUserClient; + console.log('user: ', user); + console.log('client.users.info property "user": ', command.user_id); + if (!user) { throw new Error('Slack-api Error: User not found'); } const classNumber = validateChannelName(command.channel_name); + console.log('classNumber: ', classNumber); + if (!classNumber) { await respond(unknownCommandBlock()); return; @@ -65,6 +75,8 @@ export const tareaCommandFunction = async ({ const submissionHasValidFormat = validateSubmissionFormat(command.text); + console.log('submissionHasValidFormat: ', submissionHasValidFormat); + if (!submissionHasValidFormat) { await respond(wrongFormatSubmission()); return; @@ -74,6 +86,8 @@ export const tareaCommandFunction = async ({ command.text ); + console.log('submissionHasSingleFormat: ', submissionHasSingleFormat); + if (!submissionHasSingleFormat) { await respond(multipleSubmissionFormatsDetected()); return; @@ -83,6 +97,11 @@ export const tareaCommandFunction = async ({ command.text ); + console.log( + 'validateIsNotMultipleSubmissions: ', + validateIsNotMultipleSubmissions + ); + if (!validateIsNotMultipleSubmissions && command.text.includes('```')) { await respond(multipleBlocksOfCodeDetected()); return; @@ -101,6 +120,8 @@ export const tareaCommandFunction = async ({ delivery: command.text, }); + console.log('validSubmissionFormat: ', validSubmissionFormat); + if (!validSubmissionFormat) { await respond(wrongFormatBlock()); return; @@ -108,6 +129,8 @@ export const tareaCommandFunction = async ({ const onlySubmissionContent = extractOnlySubmission(command.text); + console.log('onlySubmissionContent: ', onlySubmissionContent); + if (command.text && validSubmissionFormat) { const tarea = await uploadTarea({ classNumber, @@ -118,12 +141,25 @@ export const tareaCommandFunction = async ({ email: user.profile.email as string, }); + console.log('tarea: ', tarea); + console.log('uploadTarea property "classNumber": ', classNumber); + console.log('uploadTarea property "slackId": ', user.id); + console.log('uploadTarea property "delivery": ', onlySubmissionContent); + console.log( + 'uploadTarea property "firstName": ', + user.profile.first_name + ); + console.log('uploadTarea property "lastName": ', user.profile.last_name); + console.log('uploadTarea property "email": ', user.profile.email); + const messageResponse = await say( `Tarea subida con éxito <@${ user.id }>!\n\nTarea:\n${command.text.trim()}\n\n*Para agregar correcciones responder en este hilo.*` ); + console.log('messageResponse: ', messageResponse); + const thread: ICreateThreadDto = { authorId: process.env.BOT_ID!, studentId: tarea.studentId, @@ -132,6 +168,13 @@ export const tareaCommandFunction = async ({ taskId: tarea.taskId, }; + console.log('thread: ', thread); + console.log('thread property "authorId": ', process.env.BOT_ID!); + console.log('thread property "studentId": ', tarea.studentId); + console.log('thread property "text": ', messageResponse.message?.text); + console.log('thread property "timestamp": ', messageResponse.ts); + console.log('thread property "taskId": ', tarea.taskId); + await threadApi.create(thread); } } catch (error) { diff --git a/src/events/message/messageNew.ts b/src/events/message/messageNew.ts index 30e2661..532cc4d 100644 --- a/src/events/message/messageNew.ts +++ b/src/events/message/messageNew.ts @@ -9,12 +9,15 @@ export const handleSubmissionReplyNew: Middleware< SlackEventMiddlewareArgs<'message'>, StringIndexed > = async ({ client, message, logger }) => { + console.log('HERE START HANDLE SUBMISSION REPLY NEW'); if ( message.subtype === undefined && message.thread_ts && message.parent_user_id === env.BOT_ID ) { + console.log('Conditional data: ', message); try { + console.log('HERE ENTERS IN TRY BLOCK'); const { messages: messagesFromChannel } = await client.conversations.history({ latest: message.thread_ts, @@ -23,10 +26,14 @@ export const handleSubmissionReplyNew: Middleware< inclusive: true, }); + console.log('messages from channel: ', messagesFromChannel); + const isSubmissionThread = isTaskSubmission( messagesFromChannel![0].text! ); + console.log('isSubmissionThread: ', isSubmissionThread); + if (!isSubmissionThread) { return; } @@ -35,6 +42,9 @@ export const handleSubmissionReplyNew: Middleware< user: message.user, }); + console.log('slackUser: ', slackUser); + console.log('client.users.info property "user": ', message.user); + const createReplyDto: ICreateReplyDto = { authorId: slackUser!.id!, text: message.text!, @@ -45,14 +55,34 @@ export const handleSubmissionReplyNew: Middleware< (slackUser!.profile!.real_name as string), }; + console.log('createReplyDto: ', createReplyDto); + console.log('createReplyDto property "authorId": ', slackUser!.id); + console.log('createReplyDto property "text": ', message.text); + console.log('createReplyDto property "threadTS": ', message.thread_ts); + console.log('createReplyDto property "timestamp": ', message.ts); + console.log( + 'createReplyDto property "username" (first option, display_name): ', + slackUser!.profile!.display_name + ); + console.log( + 'createReplyDto property "username" (second option, real_name): ', + slackUser!.profile!.real_name + ); + const reply = await replyApi.create(createReplyDto); + console.log('reply: ', reply); + await client.reactions.add({ channel: message.channel, name: 'white_check_mark', timestamp: message.ts, }); + console.log('client.reactions.add property "channel": ', message.channel); + console.log('client.reactions.add property "name": ', 'white_check_mark'); + console.log('client.reactions.add property "timestamp": ', message.ts); + logger.info( `Reply with ID "${reply.id}" from user "${ slackUser!.id diff --git a/src/events/reaction/handleRobotFaceReaction.ts b/src/events/reaction/handleRobotFaceReaction.ts index b5804bb..4ee0e43 100644 --- a/src/events/reaction/handleRobotFaceReaction.ts +++ b/src/events/reaction/handleRobotFaceReaction.ts @@ -19,12 +19,22 @@ export const handleRobotFaceReaction: Middleware< SlackEventMiddlewareArgs<'reaction_added'>, StringIndexed > = async ({ client, event, logger }): Promise => { + console.log('HERE START HANDLE ROBOT FACE REACTION'); + console.log('client: ', client); + console.log('event: ', event); + if ( event.type === 'reaction_added' && event.reaction === 'robot_face' && event.item.type === 'message' ) { + console.log('Event data: ', event); + console.log('Event type: ', event.type); + console.log('Event reaction: ', event.reaction); + console.log('Event item type: ', event.item.type); + try { + console.log('HERE ENTERS IN TRY BLOCK'); const { messages: messagesFromChannel } = await client.conversations.history({ latest: event.item.ts, @@ -33,6 +43,16 @@ export const handleRobotFaceReaction: Middleware< inclusive: true, }); + console.log('messages from channel: ', messagesFromChannel); + console.log( + 'client.conversations.history property "latest": ', + event.item.ts + ); + console.log( + 'client.conversations.history property "channel": ', + event.item.channel + ); + if (!messagesFromChannel) { throw new Error( `Message with ID ${event.item.ts} not found in channel ${event.item.channel}.` @@ -41,6 +61,8 @@ export const handleRobotFaceReaction: Middleware< const reactedMessage = messagesFromChannel[0]; + console.log('reactedMessage: ', reactedMessage); + if (!reactedMessage.reactions) { throw new Error( `Message with ID ${event.item.ts} in channel ${event.item.channel} has no reactions.` @@ -51,6 +73,8 @@ export const handleRobotFaceReaction: Middleware< reactedMessage.reactions ); + console.log('botAlreadyReacted: ', botAlreadyReacted); + if (botAlreadyReacted) { logger.info( `Robotina already processed message with ID ${event.item.ts} in channel ${event.item.channel}, exiting...` @@ -91,12 +115,17 @@ export const handleRobotFaceReaction: Middleware< return; } + console.log('reactorIsValid: ', reactorIsValid); + logger.info('Processing submission...'); const { user: slackUser } = await client.users.info({ user: event.item_user, }); + console.log('slackUser: ', slackUser); + console.log('client.users.info property "user": ', event.item_user); + if (!slackUser) { throw new Error(`User with ID ${event.item_user} not found.`); } @@ -105,12 +134,21 @@ export const handleRobotFaceReaction: Middleware< channel: event.item.channel, }); + console.log('channel: ', channel); + console.log( + 'client.conversations.info property "channel": ', + event.item.channel + ); + if (!channel) { throw new Error(`Channel ${event.item.channel} not found.`); } const lessonId = validateChannelName(channel.name!); + console.log('lessonId: ', lessonId); + console.log('validateChannelName property "name": ', channel.name); + if (!lessonId) { throw new Error( 'Channel name must be in the format "clase-" or "clase-react-".' @@ -121,6 +159,8 @@ export const handleRobotFaceReaction: Middleware< reactedMessage.text! ); + console.log('submissionHasValidFormat: ', submissionHasValidFormat); + if (!submissionHasValidFormat) { await client.chat.postMessage({ channel: event.item.channel, @@ -134,6 +174,8 @@ export const handleRobotFaceReaction: Middleware< reactedMessage.text! ); + console.log('submissionHasSingleFormat: ', submissionHasSingleFormat); + if (!submissionHasSingleFormat) { await client.chat.postMessage({ channel: event.item.channel, @@ -147,6 +189,11 @@ export const handleRobotFaceReaction: Middleware< reactedMessage.text! ); + console.log( + 'validateIsNotMultipleSubmissions: ', + validateIsNotMultipleSubmissions + ); + if ( !validateIsNotMultipleSubmissions && reactedMessage.text!.includes('```') @@ -176,6 +223,8 @@ export const handleRobotFaceReaction: Middleware< delivery: reactedMessage.text!, }); + console.log('validSubmissionFormat: ', validSubmissionFormat); + if (!validSubmissionFormat) { await client.chat.postMessage({ channel: event.item.channel, @@ -187,6 +236,8 @@ export const handleRobotFaceReaction: Middleware< const onlySubmissionContent = extractOnlySubmission(reactedMessage.text!); + console.log('onlySubmissionContent: ', onlySubmissionContent); + const tarea = await uploadTarea({ classNumber: lessonId, slackId: slackUser.id!, @@ -196,22 +247,69 @@ export const handleRobotFaceReaction: Middleware< email: slackUser.profile!.email!, }); + console.log('tarea: ', tarea); + console.log('uploadTarea property "classNumber": ', lessonId); + console.log('uploadTarea property "slackId": ', slackUser.id); + console.log('uploadTarea property "delivery": ', onlySubmissionContent); + console.log( + 'uploadTarea property "firstName": ', + slackUser.profile!.first_name + ); + console.log( + 'uploadTarea property "lastName": ', + slackUser.profile!.last_name + ); + console.log('uploadTarea property "email": ', slackUser.profile!.email); + const { permalink } = await client.chat.getPermalink({ channel: event.item.channel, message_ts: event.item.ts, }); + console.log('permalink: ', permalink); + console.log( + 'client.chat.getPermalink property "channel": ', + event.item.channel + ); + console.log( + 'client.chat.getPermalink property "message_ts": ', + event.item.ts + ); + const botMessage = await client.chat.postMessage({ channel: event.item.channel, text: `Tarea subida con éxito <@${slackUser.id}>! \n\nAcá está el <${permalink}|Link> al mensaje original.\n\n*Para agregar correcciones responder en este hilo (no en el mensaje original).*`, }); + console.log('botMessage: ', botMessage); + console.log( + 'client.chat.postMessage property "channel": ', + event.item.channel + ); + console.log( + 'client.chat.postMessage property "text": ', + `Tarea subida con éxito <@${slackUser.id}>! \n\nAcá está el <${permalink}|Link> al mensaje original.\n\n*Para agregar correcciones responder en este hilo (no en el mensaje original).*` + ); + await client.chat.postMessage({ channel: event.item.channel, text: 'Si querés agregar una corrección a esta tarea hacelo como una respuesta al mensaje que mandó el bot.', thread_ts: event.item.ts, }); + console.log( + 'client.chat.postMessage property "channel": ', + event.item.channel + ); + console.log( + 'client.chat.postMessage property "text": ', + 'Si querés agregar una corrección a esta tarea hacelo como una respuesta al mensaje que mandó el bot.' + ); + console.log( + 'client.chat.postMessage property "thread_ts": ', + event.item.ts + ); + const createThreadDto: ICreateThreadDto = { authorId: env.BOT_ID!, studentId: tarea.studentId, @@ -220,6 +318,16 @@ export const handleRobotFaceReaction: Middleware< taskId: tarea.taskId, }; + console.log('createThreadDto: ', createThreadDto); + console.log('createThreadDto property "authorId": ', env.BOT_ID); + console.log('createThreadDto property "studentId": ', tarea.studentId); + console.log( + 'createThreadDto property "text": ', + botMessage.message!.text! + ); + console.log('createThreadDto property "timestamp": ', botMessage.ts); + console.log('createThreadDto property "taskId": ', tarea.taskId); + await threadApi.create(createThreadDto); await client.reactions.add({ @@ -228,6 +336,13 @@ export const handleRobotFaceReaction: Middleware< timestamp: event.item.ts, }); + console.log( + 'client.reactions.add property "channel": ', + event.item.channel + ); + console.log('client.reactions.add property "name": ', 'white_check_mark'); + console.log('client.reactions.add property "timestamp": ', event.item.ts); + logger.info('Submission processed successfully!'); } catch (error) { logger.error(