18

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Jeff Gilbert
  • 183
  • 1
  • 6
  • Don't think you can automatically convert it into a link at the moment, but you can have a [bot](https://api.slack.com/bot-users) listen to all messages and whenever a ticket is posted let the bot post a message with the deep link and relevant metadata. – Wilhelm Klopp Apr 03 '17 at 20:09

2 Answers2

14

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.
Community
  • 1
  • 1
Jesse
  • 305
  • 2
  • 7
  • The approach of collecting tokens from all users and then using them with `chat.update` to modify their individual message does not require legacy tokens. It will work with normal tokens too. – Erik Kalkoken Nov 02 '18 at 13:35
  • 1
    It will work with normal tokens, yes, but not in the same implementation. I'm in the process of migrating from the legacy system to using OAuth which will allow me to do this in the recommended and supported way, which I discovered after I answered the question. – Jesse Nov 05 '18 at 16:43
2

Its not possible to edit the content of the message from another user. So you can not directly replace the Ticket # with a link.

But what you can do as a workaround is to let your bot "reply" to a message and add the direct link that way using the threading system.

So the basic approach would be:

  1. Create a Slack app with a bot user

  2. Subscribe to message events for the bot user

  3. Bot user joins all channels that need to be monitored

  4. Slack app received all messages of that channel, parses them and replies to messages that have a ticket #

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • You're right in that you cannot edit the content of the message from another user, but implementing the bot method is a workaround (one that may, admittedly, be a simpler implementation as your users won't have to authorize the apps). The behavior he requested is entirely possible using a Slack App with OAuth tokens for each user. You replied to my answer when I posted previously but please see the updated and proper method of achieving the exact behavior requested. – Jesse Dec 09 '18 at 04:49