Home Docs Changelog Download Pricing Service Center Contact

How to Send Telegram Bot Messages from a Database Using SQL

Overview

This guide explains how to send Telegram Bot messages directly from your database using SQL statements. Whether you use PostgreSQL, SQL Server, MySQL, or Oracle, you can trigger Telegram notifications, alerts, images, and file messages automatically from database records.

The solution is implemented using SQLMessenger and its SQLTelebot plugin. SQLMessenger monitors a database interface table and acts as the background service, while SQLTelebot sends the message through the Telegram Bot API whenever a new record is inserted.

Instead of writing custom API integration code inside your application, you simply execute a SQL INSERT statement. The background service processes the record and delivers the message to a Telegram user, group, or channel.

This approach provides a reliable database-to-Telegram integration architecture for automated Telegram notifications triggered by SQL.

Supported Databases

SQLTelebot supports sending Telegram messages from the following databases:

  • SQL Server (2005 or higher)

  • Oracle (11g or higher)

  • MySQL (version 5.7.26 or higher)

  • PostgreSQL (version 9.0 or higher)

SQLTelebot supports sending text messages, images, and files.

Common Use Cases for Database-to-Telegram Automation

  • Sending Telegram alerts from SQL Server
  • Triggering Telegram notifications from PostgreSQL
  • Sending automated reports to Telegram groups
  • Database-driven Telegram Bot automation

This document demonstrates how to send Telegram messages from a PostgreSQL database.

Note: Before getting started with SQLTelebot, ensure you have completed the following:

1. Creating Interface Tables in your Database

First, download the script for your database type, then run the script in your database to create the interface tables (telebot_send_message and telebot_send_message_h).

Download link: https://www.sqlmessenger.com/manual/plugin-2-interface-table-config.htm#part2

Create the interface tables in your database

Create the interface tables in your database

Click here to view detailed explanation of telebot_send_message and telebot_send_message_h tables.

2. Configuring Database Monitoring in SQLTelebot

After the interface tables have been created, add your database to the SQLTelebot monitoring list.

Add the database to the SQLTelebot monitoring list

Once configured, SQLTelebot will periodically read the telebot_send_message table based on the Polling Interval setting. If new records are found, SQLTelebot will add them to the Outgoing Messages list and send them to the specified Telegram contacts.

3. Sending Telegram Messages Using SQL Statements

After SQLTelebot is properly configured, you can insert messages into the telebot_send_message table using SQL statements. SQLTelebot will send the inserted records to the corresponding Telegram recipients.

3.1 Sending Text Messages

The following SQL statement sends a text message to a Telegram group.

INSERT INTO
    telebot_send_message (bot_id, chat_id, message_text)
VALUES
    (
        7898095120,        -- Your Telegram Bot ID
        '-1002546050440',  -- The group Chat ID. To send messages to a channel, you can use the channel username, e.g., "@mychannel"
        'Alert: Server 1 disk space critical - requires immediate maintenance.' -- The message content to be sent
    );

Below is the message received in Telegram.

Telegram message sent from PostgreSQL using SQL statement

3.2 Sending Images

Note: For security reasons, any image file you plan to send must be copied in advance to the userfiles folder (or any of its subfolders) under the SQLMessenger installation directory.

The files that you plan to send must be copied in advance to the userfiles folder

You can view the SQLMessenger installation directory in the “About” dialog.

Open the “About” dialog

Viewing the SQLMessenger installation directory

Viewing the SQLMessenger installation directory

The following SQL statement sends an image to a Telegram group. Note that the message_type field must be set to "photo", and attach_file_name must contain the full file path of the image.

INSERT INTO telebot_send_message
            (bot_id,
             chat_id,
             message_type,
             message_text,
             attach_file_name)
VALUES      (
--Bot ID for sending the message. Found in "Bot Configuration".
7898095120,
--Target chat ID (private or group). Found in "Chat List". For channels, use the username (e.g., "@testchannel9012001").
--To send a message to a channel, you can enter the channel's username (including the "@" symbol) in the chat_id field, e.g., "@testchannel9012001".
'5352535507',
--Message type: Use 'document' for a file or 'photo' for an image.
'photo',
-- Optional caption displayed with the photo or file in Telegram.
'This is a photo I took during my trip in Canada',
-- The full path of the file to be sent.
'C:\SQLMessenger\userfiles\myphoto\StockSnap_QMXZFZZ6WM.jpeg' ); 

The photo received in Telegram

The photo received in Telegram

3.3 Sending Files

The following SQL statement sends a file to a Telegram group. Note that the message_type field must be set to "document", and attach_file_name must contain the full file path of the file. (As with images, the file must first be copied to the userfiles folder under the SQLMessenger installation directory or one of its subfolders.)

INSERT INTO telebot_send_message
            (bot_id,
             chat_id,
             message_type,
             message_text,
             attach_file_name)
VALUES      (
--Bot ID for sending the message. Found in "Bot Configuration".
7898095120,
--Target chat ID (private or group). Found in "Chat List". For channels, use the username (e.g., "@testchannel9012001").
--To send a message to a channel, you can enter the channel's username (including the "@" symbol) in the chat_id field, e.g., "@testchannel9012001".
'5352535507',
--Message type: Use 'document' for a file or 'photo' for an image.
'document',
-- Optional caption displayed with the photo or file in Telegram.
'This is the sales performance report for Emma for November 2025.',
-- The full path of the file to be sent.
'C:\SQLMessenger\userfiles\report202511(Emma).xlsx' ); 

The file must be placed in the userfiles folder under the SQLMessenger installation directory

The file received in Telegram

The file received in Telegram

3.4 Tracking Message Delivery Status

After SQLTelebot sends a message, it updates the state field in table telebot_send_message , and then moves the message record from telebot_send_message to telebot_send_message_h.

You can check the delivery result by querying the state and status_desc fields in the telebot_send_message_h table. For example:

SELECT state,
       status_desc
FROM   telebot_send_message_h
WHERE  message_id = YourMessageID; 

The state field indicates the message delivery status. Possible values include:

W

Waiting to be processed. SQLTelebot has not yet read this record.

T

SQLTelebot has read this record and is currently processing it.

F

Failed to send. The status_desc field contains the reason for the failure.

For detailed information about telebot_send_message and telebot_send_message_h, please visit: https://www.sqlmessenger.com/manual/plugin-2-interface-table-config.htm#part4

FAQ

Can I send Telegram messages directly from SQL Server without writing API code?

Yes. By using SQLMessenger together with the SQLTelebot plugin, you can send Telegram messages using standard SQL INSERT statements into an interface table. There is no need to manually implement Telegram Bot API calls in your application code.

This approach simplifies Telegram integration for database administrators and SQL developers.

Does this solution work with PostgreSQL, MySQL, and Oracle?

Yes. SQLMessenger supports multiple relational database systems including:

  • SQL Server
  • PostgreSQL
  • MySQL
  • Oracle

The Telegram message sending mechanism works the same way across all supported databases: insert a record into the monitoring table, and SQLTelebot delivers the message automatically.

How does the database-to-Telegram integration actually work?

The architecture is based on a monitoring mechanism:

  1. A database table acts as a message queue.
  2. SQLMessenger monitors the table in real time.
  3. When a new record is inserted, SQLTelebot sends the message through the Telegram Bot API.
  4. Delivery status is written back to the database.

This creates a clean separation between database logic and external messaging services.

Can I send images or files to Telegram from SQL?

Yes. SQLTelebot supports:

  • Text messages
  • Images
  • Files (such as PDF reports, Excel documents, or log files)

You can store the file path or reference inside the interface table, and the plugin will handle file transmission automatically.

Is it possible to send automated Telegram alerts based on database triggers?

Yes. You can configure:

  • SQL triggers
  • Scheduled jobs
  • Stored procedures
  • Monitoring scripts

Whenever a specific event occurs (for example, an error log entry or threshold breach), the system can automatically insert a message into the interface table and trigger a Telegram notification.

Is this solution secure?

Yes. The Telegram Bot Token is configured within the SQLTelebot plugin settings and is not exposed in SQL scripts. Database access control mechanisms remain fully enforced.

For additional security, you can:

  • Restrict table access permissions
  • Use encrypted database connections
  • Limit bot access to specific chat IDs

Can I track whether the Telegram message was successfully delivered?

Yes. After sending the message, SQLTelebot updates the status column in the interface table. You can track:

  • Sent
  • Failed
  • Error message
  • Telegram response ID

This allows you to build reliable audit logging and monitoring systems.

What are common real-world use cases for sending Telegram messages from a database?

Typical scenarios include:

  • System error alerts
  • Monitoring notifications
  • Financial transaction confirmations
  • Daily report distribution
  • Industrial automation alerts
  • DevOps monitoring notifications

This makes the solution suitable for IT operations, enterprise systems, and data-driven automation environments.

Why use SQLMessenger instead of directly calling the Telegram Bot API?

Direct API integration requires custom development, background processing logic, retry handling, and error management.

Using SQLMessenger and SQLTelebot provides a ready-to-use integration layer that separates database logic from external API communication, reducing development effort and improving maintainability.

Technical Support

If you run into any issues while using SQLTelebot, feel free to contact us for technical support.

Last updated on February 25, 2026


Cookie Settings

We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies in accordance with our Cookie Policy.

You can change your preferences at any time by clicking on "Cookie Settings" in the website footer.

Cookie Preferences

Manage your cookie preferences. You can enable or disable different types of cookies below. Necessary cookies cannot be disabled as they are required for the website to function properly.

Necessary Cookies

Always active

These cookies are essential for the website to function and cannot be switched off. They are usually only set in response to actions made by you such as setting your privacy preferences, logging in or filling in forms.

Preferences Cookies

Makes the site easier to use

These cookies allow the website to remember choices you make and provide enhanced, more personal features.

Analytics Cookies

Helps us improve our website

These cookies help us understand how visitors interact with our website, which helps us improve our products and services.

Marketing Cookies

Used to track visitors across websites

These cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging.