Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a string and replace with a URL in a user-generated message

New at Slack programming and, admittedly, it's been a while since I've developed much of anything.

I'm looking for direction on how to automatically convert any ticket # posted by anyone in any channel and convert it to a deep link into our ticketing system.

So, perhaps the original message was something like:

"Who is working on Ticket #212373?"

I'd like '#212373' to be automatically be converted to a link into our ticketing system. Bonus points if it will then expand with other details/attributes from the ticket in the target system, such as customer name, issue description, current status, etc.

Can anyone give me some direction, perhaps in showing me which API to target, whether it's possible, recommended approach, etc?

like image 637
Jeff Gilbert Avatar asked Mar 31 '17 21:03

Jeff Gilbert


1 Answers

Update 2018-12-08: Using OAuth

I finally got around to implementing this properly via OAuth, and it's pretty simple given the complexity of what's happening under the hood. To have exactly the behavior you described above, here's what you do:

  1. Register a Slack App
  2. Add the message.channels event subscription to your App and set your Request URL.
  3. Setup an Oauth flow according to Slack's documentation. This is the process by which your users' tokens will be generated.
  4. Direct your users to authorize your Slack App using the "Add to Slack" button specifying (at a minimum) the chat:write:user scope. This is the critical component which allows your App to actually update the user's message.
  5. At your Request URL specified in step 2, handle incoming message events from Slack. In your (and my) situation, you'll want to parse the text attribute of the event, which is the message posted by the user.
  6. Finally, use the appropriate user's access token that was generated in step 4 to send chat.update back to Slack with the parsed (and linked) text.
  7. Optionally, configure a bot user to send chat.postEphemeral when your App parses a message that it cannot update due to lack of permission. You can send the ephemeral message to the user to let him know that he can authorize your app to link things on his behalf.

If you want to get message.channels events from private groups and/or direct messages, you will need to specify the groups:history and im:history, respectively.

Take note that you will need to additionally handle message_changed events if you want to maintain links in the edited message, because Slack strips the link tags when the user edits their message directly. message_changed events are structured a bit differently with the the original message contained in a new message parameter and the previous message in a new previous_message parameter but can otherwise be parsed and updated the same as simple messages.

Furthermore, you can supply a properly formatted attachments parameter to your chat.update method to add the additional details you mentioned in your question such as customer name and ticket info.

Original 2018-10-27: Legacy method, not recommended

This is possible, but not encouraged by the Slack Team. The method is deprecated and may be removed in the future.

  • Generate a Legacy Token here: https://api.slack.com/custom-integrations/legacy-tokens
  • Configure an outgoing webhook for the channel(s) you want to monitor.
  • Post a message to the channel(s) where an outgoing webhook is configured.
  • Parse the message on your endpoint.
  • If the message contained something to be linked, modify the message then send an incoming webhook to the chat.update endpoint authenticating as the token generated above.

I have implemented the exact behavior that you requested and it has worked flawlessly for several years. However, be aware of the following downsides:

  • All users who wish to have their messages parsed must generate a Legacy Token and that token must be stored in a retrievable fashion on your endpoint, which is both not recommended by Slack and potentially insecure.
  • The Legacy Token has powerful scopes assigned to it and can do some real damage in the wrong hands.
  • This is deprecated and may be removed at any time in the future.
  • Each outgoing webhook counts against your integration limit on the free plan, so you may not be able to cover every channel.
  • This does not work for direct messages -- only public channels.
like image 197
Jesse Avatar answered Oct 08 '22 20:10

Jesse