Telegram Integration in your APEX application

Telegram is one of the widely used messenger platform. It has lots of features like bots, channels and groups. In-short, it is more then a messenger platform.

Purpose of this blog is to see a basic example of how we can use Telegram platform to integrate with Oracle APEX application. Following is my use case.

When a new Order get’s created, I want to notified specific admin users with order details. The benefit is that, those who are part of that group will receive a new message as a notification in Telegram mobile application.

Yes, you can! And it’s FREE !!!

Time needed: 30 minutes.

Now, let’s try to see how we can achieve this.

  1. Install Telegram and Register yourself.

  2. Create New Channel

    https://www.logaster.com/blog/how-create-telegram-channel
    Based on your required, you can decide to make it private/public channel

  3. Embed Bot in this channel

    https://www.logaster.com/blog/how-create-telegram-channel/#par5
    We are going to use BotFather (option 2) 

  4. Add bot user into channel

  5. Test Integration

    Let’s try to call a simple REST webservice using getMe method.

    Please note that, you will require to add their SSL Certificate into oracle wallet. Click here to find details about how you can setup add certificate into wallet.

  6. Find chat_id of your channel

    chat_id always starts with – (minus sign)

  7. Send Message into Channel

    Now, let’s try to send a message and consume sendMessage web service.

  8. Result

    Telegram Integration in Oracle APEX


  • Step 2 – Following steps to perform in Telegram Mobile App
    • /start
    • /newbot
    • Bot Name = <BOT_NAME>
    • Username = <BOT_USERNAME>
    • You will receive HTTP API token here, please save it. This will be useful in accessing the REST API
  • Step 3 – Following steps to perform in Telegram Mobile App to add bot user into channel
    • Open Channel
    • Click on Manage Channel
    • Click Administrators
    • Click Add Administrator
    • Search “<BOT_USERNAME>”
    • Click on the bot you found and Add
  • Step 6 – Following steps to find your channel chat_id
    • chat_id always starts with – (minus sign)
    • to find your chat_id, use following method
      • Send one dummy/test message into your channel via Telegram App
      • Open following URL in your browser window.
        • https://api.telegram.org/bot<bot_token>/getUpdates
      • Result
        • {“ok”:true,”result”:[{“update_id”:369100877,”channel_post”:{“message_id”:2,”chat”:{“id”:<here_is_chat_id>,”title”:”<title>”,”type”:”channel”},”date”:1602245959,”text”:”Welcome”}}]}

Test Telegram Integration from PL/SQL

SET DEFINE OFF;
SET SERVEROUTPUT ON;

DECLARE
  l_result clob;
BEGIN
  l_result := APEX_WEB_SERVICE.MAKE_REST_REQUEST(
    p_url => 'https://api.telegram.org/bot<bot_token>/getMe'
   ,p_http_method => 'GET'
   ,p_wallet_path => '<wallet_path>'
   ,p_wallet_pwd => '<wallet_pwd>'
  );
  DBMS_OUTPUT.PUT_LINE(l_result);
END;

Result:
{"ok":true,"result":{"id":<bot-id>,"is_bot":true,"first_name":"<bot-name>","username":"<bot-username>","can_join_groups":true,"can_read_all_group_messages":false,"supports_inline_queries":false}}

Send Message into Telegram Channel using PL/SQL

SET DEFINE OFF;
SET SERVEROUTPUT ON;

DECLARE
  l_result clob;
BEGIN
  apex_web_service.g_request_headers.delete();
  apex_web_service.g_request_headers(1).name := 'Content-Type';
  apex_web_service.g_request_headers(1).value := 'application/json';
  
  l_result := APEX_WEB_SERVICE.MAKE_REST_REQUEST(
    p_url => 'https://api.telegram.org/bot<bot_token>/sendMessage'
   ,p_http_method => 'POST'
   ,p_wallet_path => '<wallet_path>'
   ,p_wallet_pwd => '<wallet_pwd>'
   ,p_body => '{"chat_id":-<chat_id>,"text":"Welcome to https://apex18.blogspot.com. Introducing https://apexpert.in."}'
  );
  
  DBMS_OUTPUT.PUT_LINE(l_result);
END;
That’s it.
 
Click here to see working demo.
 
Hope that helps !!!
 
Regards,
Jaydip Bosamiya
jbosamiya@gmail.com

3 thoughts on “Telegram Integration in your APEX application

  1. Pingback: Launching apexpert.in - Integrated solutions using Oracle APEX and Native Mobile App - APEXPERT - Oracle APEX and Mobile App Consultants

  2. W.S. Reply

    Great article Jaydip Bosamiya! Congrats!

    Just one comment, I commented the lines related to Walled User/PWD and it worked well too.

    Just a question, is it possible in any way to send direct messages to users instead of Channels or Groups (I tried with groups and it worked well too).?

    Thanks!

    Answering Rodrigo Santos question:

    @Rodrigo, to see those commands working you can try to add them into Script Editor. You just copy/paste and replace with your bot/chat details. In my case here I removed lines related to Wallet User/PWD and it worked pretty well.

    Hope that this help you.

    Regards!

Leave a Reply

Your email address will not be published. Required fields are marked *