Slack API Integrate with Oracle Apex

Desire OutPut

 


1)  First Create Your Slack   WorkSpace 

https://slack.com/intl/en-om/get-started#/

2) Ones Workspace created login to your selected workspace


3) create channel ( I have created here #erp) 


Register your application 

with Slack to obtain credentials for use with our OAuth 2.0implementation, which allows you to negotiate tokens on behalf of users and workspaces.

4) Now Login to https://api.slack.com/apps for create the Bot Token

Click on Create new App , give App Name:- Any name , and selected the Slack Workspace

Click on Create App Button 

Click on Bot :-  Allow User to interact with your app through channels and conversation.

Bots Will redirect you App Home --> Click on Review Scopes to Add


Go to Scopes option and add scopes



Add the Scopes according to your API method required 

User Token Scopes


Ones u add the tokens  system will enable Install App To Workspace button click on it 

will generate the Basic security key 


will generate the App Credentials  

Now go to Oauth & Permission , you will find your both Token is ready.

OAuth Token start with xoxp

Bot User  Tokent Start with xoxb


Now Slack API key Is Ready do the blow test  go to 

https://api.slack.com/methods/conversations.list/test


Documentation :- Which will guide you method of URL and what is type of token key and scope required to run the api 

Make sure all required scoped highlighted in above image with red frame should be added in Bot token and User token Scope list as mentioned below  ( Go to Oauth & Permission Tab and check Bot Token scope and User Token Scope)

https://api.slack.com/apps/A0196830KPU/oauth?



Now go to https://api.slack.com/methods/conversations.list/test

will perform test  copy your Bot token access key 

Click on Test Method button to perform the Json output 


If you are able to perform test successfully  then perform below method similar way 


1) Conversations.list method  =  here required xoxb (bot token)

https://slack.com/api/conversations.list?token= xoxb-1301204045396-1324912557537-xxxxxxxx


2) Conversation.history  

    --> here required xoxp (user token) ,

    --> here you have to pass the channel  (C0191JXF38R i have created #erp channel)

    

https://slack.com/api/conversations.info?token= xoxb-1301204045396-1324912557537-xxxxxxx&channel=C0191JXF38R

3) Users.list 

to perform User.list its required  users:read scope should be added in both token

https://slack.com/api/users.list?token=xoxb-1301204045396-1324912557537-xxxxxxx


Now turn to Oracle and create the view if required or you can use API straight away in websource in Apex


====================

Slack conversations.list

=====================

API:- https://slack.com/api/conversations.list?token= xoxb-1301204045396-1324912557537-xxxxxxxx

Request Scop:- channels:read  groups:read  im:read  mpim:read 


create or replace view SLACK_CIS_CHANNEL as

select j.channel_id,j.channel_name,j.channel_topic,j.channel_topic_creator,j.channel_topic_lastset,j.channel_user,j.shared_team_ids,j.purpose_title,j.purpose_creator,j.purpose_lastset

from JSON_TABLE( apex_web_service.make_rest_request(

                  p_url => 'https://slack.com/api/conversations.list?token= xoxb-1301204045396-1324912557537-XXXX'

                , p_http_method => 'GET'

               

            ),'$.channels[*]'

                        COLUMNS

                        (

                         channel_id varchar2(50) path '$.id'

                        ,channel_name varchar2(250) path '$.name'

                        ,channel_topic varchar2(250) path '$.topic.value'

                        ,channel_topic_creator varchar2(250) path '$.topic.creator'   

                        ,channel_topic_lastset varchar2(250) path '$.topic.last_set'   

                        ,channel_user varchar2(2000) path '$.creator'

                        ,shared_team_ids varchar2(250) path '$.shared_team_ids'

                        ,purpose_title varchar2(250) path '$.purpose.value'

                        ,purpose_creator varchar2(250) path '$.purpose.creator'

                        ,purpose_lastset varchar2(250) path '$.purpose.last_set'

                        

                        )

                ) j

=========================

conversations.history

=========================

API:- https://slack.com/api/conversations.history?token=xoxp-1301204045396-1293646031205-1324912543569-XXXXX&channel=C0191JXF38R

Request scope: channels:history  groups:history  im:history  mpim:history 


CREATE OR REPLACE FORCE VIEW "SLACK_CIS_CONVERSATION_HISTORY" ("CONVERSATION_MSGID", "CONVERSATION_TYPE", "CONVERSATION_TEXT", "CONVERSATION_USER", "CONVERSATION_TS", "CONVERSATION_TEAM", "TAG_USER", "TAG_MSG") AS 

  select j.conversation_msgid,j.conversation_type,j.conversation_text,j.conversation_user,j.conversation_ts,j.conversation_team,j.tag_user,j.tag_msg

from JSON_TABLE( apex_web_service.make_rest_request(

                  p_url => 'https://slack.com/api/conversations.history?token=xoxp-1301204045396-1293646031205-1324912543569-XXXXX&channel=C0191JXF38R'

                , p_http_method => 'GET'

               

            ),'$.messages[*]'

                        COLUMNS

                        (

                         conversation_msgid varchar2(150) path '$.client_msg_id'

                        ,conversation_type varchar2(250) path '$.type'

                        ,conversation_text varchar2(2500) path '$.text'

                        ,conversation_user varchar2(250) path '$.user'   

                        ,conversation_ts varchar2(250) path '$.ts'   

                        ,conversation_team varchar2(2000) path '$.team'

                        ,tag_user varchar2(250) path '$.blocks.elements.elements.user_id'

                        ,tag_msg varchar2(2500) path '$.blocks.elements.elements.text'

                        )

                ) j

/

===============
 Users.list
===============

same way create for user.list

API:- https://slack.com/api/users.list?token=xoxb-1301204045396-1324912557537-XXXXX
Request Scop :-     users:read 

create or replace view SLACK_CIS_USER_LIST as
select j.user_id,j.team_id,j.user_name,j.real_name,j.first_name,j.last_name,j.title,j.phone,j.skype,j.status_emoji
from JSON_TABLE( apex_web_service.make_rest_request(
                  p_url => 'https://slack.com/api/users.list?token=xoxp-1301204045396-1293646031205-1324912543569-XXXXXXX'
                , p_http_method => 'GET'
               
            ),'$.members[*]'
                        COLUMNS
                        (
                         user_id varchar2(150) path '$.id'
                        ,team_id varchar2(250) path '$.team_id'
                        ,user_name varchar2(2500) path '$.name'
                        ,real_name varchar2(250) path '$.real_name'   
                        ,first_name varchar2(250) path '$.first_name'   
                        ,last_name varchar2(2000) path '$.last_name'
                        ,title varchar2(250) path '$.profile.title'
                        ,phone varchar2(2500) path '$.profile.phone'
                        ,skype varchar2(2500) path '$.profile.skype'
                        ,status_emoji clob path    '$.profile.status_emoji'
                                                
                        )
                ) j

========================
go to Apex now create web source 

=============================






Create  Page  in Apex with any report type and select web source which u have created 



if web source not able to add blcok elements as column then add manual as mentioned below 

if you want to display the name of User based on User id then do following 

1) Add New Column 

Add Column name as User_name and Change Column Type  A Derived , and applied SQL Expression by define where clause condition here USER_ is column name of API 


=====================================

Now post the message chat.postMessage

=====================================

Request Scope Bot:-chat:write  , Scope User:-chat:write  chat:write:user  chat:write:bot 

OAuth Access Token:- xoxp-1301204045396-1293646031205-1324912543569-5996892af780c2f1e4ab242ca8c277d4

Channel:- C0191JXF38R

text :- Hi message from me

SQL:-

create or replace procedure SLACK_CIS_POST_MESSAGE3(i_channel in varchar2,i_text in varchar2,i_msg out varchar2) is

    l_resp_clob        clob;

    l_parm_names  apex_application_global.vc_arr2;  

    l_parm_values apex_application_global.vc_arr2; 

    i_url varchar2(1500);

    c_url varchar2(1500);

BEGIN

    apex_web_service.g_request_headers(1).name := 'Content-Type';  

    apex_web_service.g_request_headers(1).value := 'application/x-www-form-urlencoded';  

    l_parm_names(1) := 'channel';  

    l_parm_values(1) := i_channel;  

    l_parm_names(2) := 'text';  

    l_parm_values(2) := UTL_URL.UNESCAPE(i_text);  

    

    l_resp_clob := apex_web_service.make_rest_request(

    p_url =>'https://slack.com/api/chat.postMessage?token=xoxp-1301204045396-1293646031205-1324912543569-XXXXXX'

    , p_http_method => 'POST'

    ,p_parm_name => l_parm_names,  

    p_parm_value => l_parm_values  

    );  

  if l_resp_clob is null then 

        i_msg:='Error while processing the Post';

  end if;

 end;


Go to Apex and create two page item and and one button to call the above procedure.

2) Create Process to call the procedure.


3) Run the page and post the message 


4) Go to Slack 


Comments

Post a Comment

Popular posts from this blog

Telegram Integration with Oracle

The username or password for the connection pool named |apex||, are invalid, expired, or the account is locked

Enabled SQL Developer Web (SDW) & ORDS Database API ORDS 19.4