ServiceNowDevProgram / SlackerBot

The official application repository for the bot @Slacker on the sndevs.com workspace.
https://github.com/ServiceNowDevProgram/Hacktoberfest
9 stars 73 forks source link

Bot messages don't appear as Chats #144

Open chelming opened 1 year ago

chelming commented 1 year ago

Problem

If we want to search for a bot message we have to use CONTAINS queries on the Payloads table because they never get parsed into fields on the Chats table.

Reason it's not currently working

Bots aren't users! Well, obviously, right?

πŸ±β€πŸ‘€ NINJA EDIT πŸ±β€πŸ‘€ There's actually two issues and I think it comes down to messages with images "unfurling." By default, the SRAPI is dropping messages from bots, I believe to prevent the bots from triggering each other. It does this by checking for event.bot_id. The problem is that the event we are getting in the Payloads table doesn't have an event.bot_id, there's instead event.message.bot_id and event.previous_message.bot_id. That "previous" is what's making me think it's due to unfurling being an event for a message "changing." (double ninja πŸ±β€πŸ‘€πŸ±β€πŸ‘€: event.subtype is message_changed which solidifies my theory)

back to our regularly scheduled programming... But here's where things are (edit: also 😬) going wrong... In the Convert to chat business rule we get the user using var userSlackID = message.event.user;. The problem is that bot information isn't stored there. We need another check specifically for bots for message.event.message.bot_id and then pass that into establish_user().

Why do we care?

Well it's not awful but currently if we want to check if a bot message has already "triggered" we have to do all the contains queries. If we had the Chat records we could query the fields for the specific values that have already been parsed.

chelming commented 1 year ago

aiight, I'm doing some solutioning here.

I think we can "make things better" through ~two~ three things:

  1. Save all payloads, regardless
  2. Update x_snc_slackerbot_user table to have a boolean for is_bot and enable that via establish_user
  3. Update the Parse Chat BR's to have a condition for user.is_bot == false

Number one

Remove the following two lines from the SRAPI SlackerBot Event Handler/Event

if (request.body.data.event.bot_id) return;
//gs.info('slackoff: ' + (request.body.dataString));

The first line is the only one that matters, but let's get rid of that comment anyway.

Number two

Update the User table

(From App Studio) Tables > User > Insert a new row... Field Value
Label Is Bot
Type True/False
Default Value False

Update the Slacker establish_user function

Slacker script include > establish_user Honestly, I think the easiest thing is to add a new param is_bot on the function and set it from there since all we pass in via payload_user is a string with the user_id.

Number three

We made it! Set a condition on the Parse chat business rule for user (show related fields) Is bot > is > True.

Number is-john-dahl-done-converting-pointsparty-from-a-br-to-a-parser-yet?

Points Party is currently a BR that scrapes the payload. This would likely have an adverse effect while it's in that state.

Other notes!

The event.message.subtype for bot messages with URLs is message_changed not bot_message so I don't think bot_message is the best place to look? Not sure, maybe it'd be good enough? We'd run in to some weird stuff if we stored that on the chats table and some bot messages had bot_message and others didn't. It feels a little wrong.