How to send Slack notifications through your APEX apps - Part 1

Playing with the incoming webhooks

ยท

4 min read

Introduction

Nowadays, all the apps we build need to communicate with their users using as many channels as possible. With the pandemic, the use of business communication tools such as Teams or Slack has increased a lot and now people tend to centralize notifications to these applications. This blog post will focus on how to send information from your Oracle APEX applications through Slack using an incoming webhook.

Slack set up

Creating a Slack app

Go to https://api.slack.com/apps and click on the "Create New App" button

Screenshot of the apps page showing the Create New App button

Next, Slack will ask you to choose a name for your app and a workspace. When you're done, click Create App Screenshot showing the Create New App form page

You will be redirected to the management page of your new application.

Screenshot showing your Slack application management page

Congratulations, you just got your first Slack apps!

Configuring the Slack app

Make our app unique

Under Basic Information, scroll down the page to the Display Information section. Then choose a logo and add a short description

Screenshot of the Display Information section

Now, when we go to Add apps on the Slack app, we should see something like this.

Screenshot of the Add apps page on Slack showing our app with the logo and the description we just add

Much better than the default one right?

Creating the webhook

Slack allows us to set incoming webhooks for an application and provides us with an https address to send the POST request. To set it up, just click on the Incoming Webhooks card and you will be redirected to the webhook page.

Sreenshot showing how to activate Incoming Webhooks for your Slack app

Next, you need to create a Webhook URL for your workspace by clicking on "Add New Webhook to Workspace".

Screenshot showing the section Webhook URLs

Slack will ask you to choose the channel for the webhook you create (which means that if you want to use multiple channels, you'll need to create a url for each).

Screenshot showing the authorization process for your Slack app

After your application is authorized for the selected channel, you will see a url. Copy it and store it carefully because everyone who knows it can send messages to your channel.

Sending your first message

Configuring the APEX application

To test it, I built a simple APEX page by adding :

  • A P2_URL page item (type Text), which will contain the URL of the webhook
  • A P2_TEXT page item (type Rich Text Editor - Markdown) that will allow the user to type the message
  • A SEND button used to submit the page
  • A "Send To Slack" page process that will actually send your message using this PL/SQL procedure
declare
    l_response clob;
    l_text varchar2(500);
begin
    -- Slack uses a different bold markup than CKEditor
    l_text := replace(:P2_TEXT, '**', '*');
    -- Slack doesn't support language in code block
    l_text := regexp_replace(l_text, '^```[a-z]{1,}', '```');
    -- convert new line line from chr(10) to \n
    l_text := replace(l_text, chr(10), '\n');
    -- convert carriage return from chr(13) to \r
    l_text := replace(l_text, chr(13), '\r');


    apex_web_service.clear_request_headers;

    apex_web_service.set_request_headers(
        p_name_01        => 'Content-Type',
        p_value_01       => 'application/json'
    );

    l_response := apex_web_service.make_rest_request(
        p_url => :P2_URL,
        p_http_method => 'POST',
        p_body => '{"text":"'|| l_text ||'", "mrkdwn":true}'
    );

end;

Test it ๐Ÿš€

Finally, the last thing you need to do is run your application, paste in your webhook URL, type in your message, and hit the send button! Pretty cool, right?

Animated screenshot showing an APEX app posting message to a Slack channel

Conclusion

Using incoming webhooks makes the integration very quick but has some drawbacks:

  • You must create an incoming webhook for each channel you want to be able to send notifications on.
  • The incoming webhook URL is open, which means that everyone who knows it is able to send messages to the channel.
  • You cannot send direct messages to a specific user.

In the next post in this series, we'll look at how to use Slack's API to get rid of these drawbacks and be able to notify who you want and how you want!

In the meantime, if you want to test it by yourself, you can find the export of the application in this Github repository.

ย