Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 

Overview


In this guide, you will learn how to integrate between SAP and Slack in order to send out a SAP Fiori Notification to a Slack user. For example, this integration will enable you to send a leave request notification to a specific user on Slack such as your manager.

To achieve this goal, you need to first create a Slack Application which will serve as a bridge to communicate between the SAP Fiori and Slack platforms. Once the OAuth 2.0 Authentication Framework is configured in your NetWeaver system, it will perform the user mapping and allow the Fiori Notification to be sent to a Slack user.

 

Main Steps



  1. (Slack) Create a Slack app

  2. (Slack) Install the created Slack app in your workspace

  3. (ABAP) Configure OAuth 2.0 Client

  4. (ABAP) Create Slack Outbound Adapter for Notification Channel


 

Step by Step Guide


Preparation: If you are new to Slack, please see the link below for instructions of how to create a Slack workspace.

https://get.slack.help/hc/en-us/articles/206845317-Create-a-Slack-workspace

Prerequisite: SAP NetWeaver Software Component “SAP_GWFND” Release 751 or above.

 

Create a Slack app


Sign in to your Slack workspace via Slack web page and click Browse apps



 

Choose Manage apps



 

Click on Build to start building your Slack app



 

Put a useful app name i.e. Fiori Notifications, select the development Slack workspace where you want to install the app and create the app



 

Configure Permissions

These permissions will allow your application to use Slack API such as passing the redirect URL in an OAuth request and configure the scopes. The scopes will define the methods this application is allowed to call, and what information and capabilities are available on a workspace.



 

Select the incoming-webhook, chat:write:user, chat:write:bot Permission Scopes and save your changes



 

Install the created Slack app in your workspace


 



 

Configure OAuth 2.0 Client


Please see Access Google APIs using the OAuth 2.0 Client API as an example for the configuration.

By following all the steps from the above Google API example, you should have the following steps completed:

  • Define a Service Provider Type

  • Create a BADI implementation for the Service Provider Type


Replace the method implementation “IF_OA2C_SPECIFICS~GET_ENDPOINT_SETTINGS” with the following code:

*********************************************************************************
  e_changeable                  = abap_false.
e_authorization_endpoint_path = 'https://slack.com/api/oauth.access'.
e_token_endpoint_path = 'https://slack.com/api/oauth.token'.
e_revocation_endpoint_path = 'https://slack.com/api/auth.revoke'.

*********************************************************************************

  • Implement the Enhancement Spot for the Service Provider Type

  • Create a BADI implementation for the Configure Extension

  • Implement the Enhancement Spot for the Configure Extension

  • Create an OAuth 2.0 Client Profile

  • Copy and paste the scopes from your Slack App (in OAuth & Permissions section) to the client profile

  • Create an OAuth 2.0 Client Configuration

  • Copy and paste your OAuth 2.0 Client Redirection URL to your Slack app


End User Tasks

  • Execute transaction “OA2C_GRANT” and click on “Request OAuth 2.0 Tokens” button

  • Once the OAuth 2.0 Token is requested, you will be redirected to the Slack authorization endpoint.

  • After signing in with your SAP ID, you will then get redirected back to the grant application


 

Create Slack Outbound Adapter for Notification Channel


You need to create a class that is responsible for pushing out the notifications to the Slack.

The class should follow the naming convention such as Z_SLACK_OUTBOUND_CHANNEL and use the interface “/IWNGW/IF_HUB_OUT_PUSH_CHANNEL”. Once the class is created, you need to maintain your outbound adapter class along with push channel ID in customizing “Activate and Maintain Push channels”.

Create and implement the interface method “/iwngw/if_hub_out_push_channel~notifications_created” with the code below.

Additionally, we will be using the transaction “/n/iwngw/bep_demo” to create the testing notification in this guide.

  • Use CL_HTTP_CLIENT with OAuth configuration to perform API call.


*********************************************************************************
CALL METHOD cl_http_client=>create_by_url
EXPORTING
* The target destination web-hook can be obtained from the Incoming
* Webhooks feature in your Slack Application
* url = 'Your target destination web-hook'
IMPORTING
client = lr_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.

* it_notification is an imported parameter which contains the
* notifications
LOOP AT it_notification INTO lv_notification.

lr_request = lr_http_client->request.
lr_request->set_method( if_http_request=>co_request_method_post ).

CALL METHOD lr_request->if_http_entity~set_header_field
EXPORTING
name = 'Content-type'
value = 'application/json'.

* Example template
* lv_body = '{' &&
* '"attachments": [' &&
* '{' &&
* '"fallback": "' && lv_notification-text_sensitive &&
* '",' && '"pretext": "You have a new ' &&
* lv_notification-description &&
* ' notification from Fiori",' &&
* '"color": "#36a64f",' &&
* '"author_name": "XXX",' &&
* '"title": "' && lv_notification-text_sensitive && '",' &&
* '"text": "' && lv_notification-subtitle && '",' &&
* '"footer": "Fiori Notification Channel - FUB",' &&
* '}' &&
* ']' &&
* '}'.
* lv_body = 'Your text template in JSON format'.

CALL METHOD lr_request->if_http_entity~set_cdata
EXPORTING
data = lv_body.

CALL METHOD lr_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.

CALL METHOD lr_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.

IF sy-subrc = 0.
lr_http_client->response
->get_status( IMPORTING code = lv_http_rc ).
IF lv_http_rc <> 200.
ELSE. "status 200 ->>OK
CLEAR: lv_response.
lv_response = lr_http_client->response->get_cdata( ).
ENDIF.
ENDIF.
ENDLOOP.

lr_http_client->close( ).

*********************************************************************************

Once you have completed your outbound adapter class, you will be able to perform the API call to the target destination web-hook you specified and send the SAP Fiori Notification in your customized text template to a specific Slack user.

 

Test the integration



  1. Execute the transaction code “/n/iwngw/bep_demo”

  2. Select Leave Request Notification Type and enter your User ID

  3. Click the execute (F8) button to create the Leave Request Notification


  4. Check if the notification is sent to a specific user on Slack



 

Conclusion


Overall, It’s not difficult to complete this tutorial if you have followed all the steps from the "Access Google APIs" example and configured the OAuth 2.0 Client properly.

Also, you should be able to create your own outbound adapter class to push out the notifications with the provided sample code and web-hook URL obtained from the Slack application.

Please enjoy!

Jack