digihub

Create the APEX Plugin

  1. In Oracle APEX, navigate to Shared Components > Plugins.
  2. Click on "Create" to create a new plugin.
  3. Fill in the necessary information for your plugin, such as name, description, category, etc.

Define Plugin Attributes:

Add attributes to your plugin for the API URL, instance ID, and access token. These attributes will be used to configure the plugin for each use case.

Create Plugin Source Code:

Here's a sample code for your plugin:

PL/SQL Code (Server-side Processing):


DECLARE
  l_url VARCHAR2(4000);
  l_instance_id VARCHAR2(100);
  l_access_token VARCHAR2(100);
  l_number NUMBER;
  l_message VARCHAR2(4000);
  l_otp NUMBER;
BEGIN
  -- Get the values of attributes
  l_url := apex_plugin.get_plugin_attribute(p_plugin_id => :PLUGIN_ID,
                                            p_name      => 'API_URL');
  l_instance_id := apex_plugin.get_plugin_attribute(p_plugin_id => :PLUGIN_ID,
                                                     p_name      => 'INSTANCE_ID');
  l_access_token := apex_plugin.get_plugin_attribute(p_plugin_id => :PLUGIN_ID,
                                                     p_name      => 'ACCESS_TOKEN');
  l_number := :P1_NUMBER; -- Replace P1_NUMBER with your page item for the number
  l_message := :P1_MESSAGE; -- Replace P1_MESSAGE with your page item for the message

  -- Generate OTP (One-Time Password)
  l_otp := FLOOR(DBMS_RANDOM.value(1000, 9999)); -- Generate a 4-digit OTP

  -- Construct the JSON payload for OTP
  apex_web_service.g_request_headers(1).name := 'Content-Type';
  apex_web_service.g_request_headers(1).value := 'application/json';

  apex_web_service.g_request_headers(2).name := 'Authorization';
  apex_web_service.g_request_headers(2).value := 'Bearer ' || l_access_token;

  apex_web_service.g_request_headers(3).name := 'Instance-ID';
  apex_web_service.g_request_headers(3).value := l_instance_id;

  apex_web_service.g_request_headers(4).name := 'Accept';
  apex_web_service.g_request_headers(4).value := 'application/json';

  apex_web_service.g_request_headers(5).name := 'Content-Length';
  apex_web_service.g_request_headers(5).value := LENGTH(apex_util.string_to_raw(l_message));

  l_url := l_url || '/api/send';

  apex_web_service.g_request_body := '{
    "number": ' || l_number || ',
    "type": "text",
    "message": "' || l_message || ' OTP: ' || l_otp || '",
    "instance_id": "' || l_instance_id || '",
    "access_token": "' || l_access_token || '"
  }';

  -- Send the request for OTP
  apex_web_service.request(
    p_url => l_url,
    p_http_method => 'POST'
  );

  -- Handle the response here and store the OTP in a session state or display it on the page.
  :P1_OTP := l_otp; -- Store OTP in a page item for display
END;

            
        

JavaScript Code (Client-side Processing):

function sendNotification(number, message, instance_id, access_token) {
  var apiUrl = 'https://wosocial.com/api/send';
  var otp = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000; // Generate a 4-digit OTP
  var requestData = {
    number: number,
    type: 'text',
    message: message + ' OTP: ' + otp,
    instance_id: instance_id,
    access_token: access_token
  };

  // Use AJAX or fetch to send the request to the API
  // Example using fetch:
  fetch(apiUrl, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + access_token,
      'Instance-ID': instance_id,
      'Accept': 'application/json'
    },
    body: JSON.stringify(requestData)
  })
  .then(function(response) {
    // Handle the response from the API here
    if (response.status === 200) {
      // Request was successful
      alert('Notification sent successfully');
    } else {
      // Handle errors
      alert('Error sending notification');
    }
  })
  .catch(function(error) {
    // Handle network errors here
    alert('Network error');
  });

  // Store the OTP in a page item for display
  $s('P1_OTP', otp); // Replace P1_OTP with your page item for OTP display
}

   
        

Define the Plugin Attributes:

Create attributes for API URL, Instance ID, and Access Token in the plugin attributes section. These attributes will allow users to configure the plugin by providing the necessary information.

Create a Page:

Create a new APEX page where you want to use this plugin.

Add the necessary page items for the number and message input fields.

Add the Plugin to the Page:

Edit your page and go to the "Page Designer" tab.

Drag and drop your custom plugin onto the page.

Configure the plugin to use the attributes for API URL, Instance ID, and Access Token.

Create Buttons or Processes to Trigger OTP Generation and Notification Sending:

Add buttons or create processes that call the PL/SQL and JavaScript functions when clicked. Pass the values from the page items as arguments to the functions.

Test your Page:

Run your APEX page and test the functionality to generate OTPs and send notifications using the provided API.

Note:

Please note that this is a simplified example, and you should enhance the error handling and security aspects as needed for your specific application. Additionally, consider implementing proper error handling and security measures, such as input validation and authentication, to ensure the security and reliability of your application.