Sending OTP via WhatsApp in Oracle APEX Using UltraMsg

 

 


Step 1: Sign Up for UltraMsg

  1. Go to UltraMsg and create an account.

  2. Verify your email and login to the UltraMsg dashboard.

  3. Create an Instance:

    • Click “Create Instance”.

    • Name your instance (e.g., MyAPEXInstance).

    • Save your Instance ID (e.g., instance123456) — you’ll need this for API calls.

  4. Generate a Token:

    • Navigate to “API Token” in the instance settings.

    • Copy the token (e.g., 2jhbj8sf1fygygh).

    • This token will be used in API requests.

Step 2: Prepare Oracle APEX

  1. Create a Page Item for Mobile Number

    • Item Name: P2_MOBILE_NUMBER

    • Type: Text Field

  2. Optional: Create a Page Item for OTP Verification

    • Item Name: P2_ENTERED_OTP

    • Type: Text Field

  3. Create a Button to Send OTP

    • Label: Send OTP

    • Action: Submit Page or Execute PL/SQL Code

Step 3: PL/SQL Code to Send OTP via WhatsApp

Paste the following in “Execute PL/SQL Code” of your button or process:

CODE:

DECLARE

    l_otp           VARCHAR2(6);

    l_url           VARCHAR2(4000);

    l_response_b    BLOB;

    l_response_c    VARCHAR2(32767);

    l_mobile_number VARCHAR2(20);

    l_message       VARCHAR2(500);

BEGIN

    -- 1️⃣ Get mobile number from page item

    l_mobile_number := TRIM(:P2_MOBILE_NUMBER);

    IF l_mobile_number IS NULL THEN

        RAISE_APPLICATION_ERROR(-20001, 'Mobile number is required.');

    END IF;


    -- 2️⃣ Add country code if missing

    IF SUBSTR(l_mobile_number,1,3) != '+91' THEN

        l_mobile_number := '+91' || l_mobile_number;

    END IF;


   -- 3️⃣ Generate 6-digit OTP

    l_otp := TO_CHAR(TRUNC(DBMS_RANDOM.VALUE(100000, 999999)));


    l_message  := 'Your OTP is ' || l_otp || ' and is valid for 5 minutes';


    -- 4️⃣ Set UltraMsg API URL with token

     l_url :=  'https://api.ultramsg.com/[Your Instance Id]/messages/chat?token=[Your Token]&to='||l_mobile_number||'&body='||l_message||'&priority=10';



    -- 6️⃣ Call UltraMsg API

    l_response_b := APEX_WEB_SERVICE.MAKE_REST_REQUEST_B(

                        p_url         => l_url,

                        p_http_method => 'POST'

                    );


    -- 7️⃣ Convert BLOB to text

    l_response_c := UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(l_response_b,32767,1));


    -- 8️⃣ Store OTP

    APEX_UTIL.SET_SESSION_STATE('P2_OTP', l_otp);


    -- 9️⃣ Success message

    APEX_APPLICATION.G_PRINT_SUCCESS_MESSAGE :=

        ' OTP sent successfully '||

        '. WhatsApp response: ' || l_response_c;


EXCEPTION

    WHEN OTHERS THEN

        APEX_APPLICATION.G_PRINT_SUCCESS_MESSAGE :=

            ' Error sending OTP: ' || SQLERRM;

END;

Tips & Best Practices

  • OTP Expiry: You can store a timestamp in session or table and check expiry.

  • Message Template: Customize your message text for better branding.

  • Multiple Users: If your app has multiple users, store OTP in a table instead of session.

  • Security: Do not log OTPs in plain text in production.

Conclusion

Integrating WhatsApp OTP in Oracle APEX using UltraMsg API is straightforward:

  1. Create UltraMsg account and token.

  2. Generate OTP in PL/SQL.

  3. Send OTP using APEX_WEB_SERVICE.MAKE_REST_REQUEST_B.

  4. Verify OTP in APEX page.

This approach enhances user experience and adds a secure authentication layer to your applications.



Comments