1. How Message Sending Works

SQLTelebot provides a feature that allows you to send Telegram messages directly from your database through an interface table.

You can create an interface table in your database and insert the messages you want to send into this table.

SQLTelebot periodically reads the table and delivers the messages via your configured Telegram bot to the specified users or channels.

SQLTelebot supports the following database systems:

Microsoft SQL Server (2005 or higher)

Oracle Database (11g or higher)

MySQL (5.7.26 or higher)

PostgreSQL (9.0 or higher)

2. Setting Up the Interface Table and Database Monitoring

Step 1: Download the SQL script for your database from the links below, and then execute the script in your database to create the interface tables.

SQL Script for Oracle 11g or higher

SQL Script for MySQL 5.7.26 or higher

SQL Script for PostgreSQL 9.0 or higher

SQL Script for SQLServer 2005 or higher

After executing the script, the following tables will be created in your database:

telebot_send_message

The message interface table, where you can insert the messages you want to send.

telebot_send_message_h

Stores processed historical messages. After SQLTelebot processes a message, it moves the data from telebot_send_message to this table for future querying and management.

For Oracle databases, the script also creates a sequence named seq_telebot_send_message_id. This sequence is used to generate message_id values in telebot_send_message.

For more details about the interface tables, please refer to the Interface Table Field Descriptions.

Step 2: In SQLTelebot, click "Send Messages from Database," and then click "Add Database."

If you have already added this database to SQLMessenger’s data source list, you can skip Step 3 and 4.

Step 3: In the "Data Sources Monitoring Configuration" dialog, click "Create a New Data Source."

Step 4: In the "Create New Data Source" dialog, enter your database information and click "OK." This will add your database to SQLMessenger’s data source list. For detailed instructions on configuring data sources, click here.

Step 5: In the "Data Sources Monitoring Configuration" dialog, select the database you want to monitor under "Data Source," and set the monitoring interval in "Polling Interval."

In this dialog box:

Data Source

Select the database you want to monitor. SQLTelebot will periodically read the interface table in this database and process requests for new messages.

Polling Interval

Set the interval for reading the interface table. By default, SQLTelebot reads the table every 30 seconds. The minimum value cannot be less than 5 seconds.

After configuring the options, click "OK" to save. SQLTelebot will then read the interface table at regular intervals (as specified by the "Polling Interval") and process new message sending requests.

3. Send Messages Using SQL Inserts

After completing the above settings, you can easily send messages by inserting records with SQL in your database.

Note that the SQL syntax for Oracle differs from that used in other databases.

Here is an example SQL statement for sending a text message:

Oracle database:

INSERT INTO telebot_send_message
            (message_id,
             bot_id,
             chat_id,
             message_text)
VALUES      ( seq_telebot_send_message_id.NEXTVAL,
             --The ID of the bot that will send the message. You can find it in "Bot Configuration".
             7898095120,
             --The ID of the chat that will receive this message. It can be a private chat or a group. The chat_id can be found in "Chat List Manager".
             --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',
             --The message text to be sent.
             'This is a test message from Oracle database' ); 

Other database:

INSERT INTO telebot_send_message
            (bot_id,
             chat_id,
             message_text)
VALUES      (
--The ID of the bot used to send the message. This can be found in "Bot Configuration".
7898095120,
--The ID of the chat that will receive this message. It can be a private chat or a group. The chat_id can be found in "Chat List Manager".
--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',
--The message text to be sent.
'This is a test message from Oracle database' ); 

Here is an example SQL statement for sending a photo message:

Note: The file to be sent must be copied in advance to the "userfiles" folder under the SQLMessenger installation directory.

Oracle databases:

INSERT INTO telebot_send_message
    (message_id,
     bot_id,
     chat_id,
     message_type,
     message_text,
     attach_file_name)
VALUES ( seq_telebot_send_message_id.nextval,
     --The ID of the bot used to send the message. This can be found in "Bot Configuration".
     7898095120,
    --The ID of the chat that will receive this message. It can be a private chat or a group. The chat_id can be found in "Chat List Manager".
    --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: "document" indicates sending a file, "photo" indicates sending a photo.
     'document',
     -- Write a caption for the file. Optional. In Telegram, this caption will appear below the photo or file.
     'Report file(01/May/2025)',
     -- The full path of the file to be sent.
     'C:\SQLMessenger\userfiles\report\myreport(202504).xlsx' 
); 

Other databases:

INSERT INTO telebot_send_message
    (
     bot_id,
     chat_id,
     message_type,
     message_text,
     attach_file_name)
VALUES (
     --The ID of the bot used to send the message. This can be found in "Bot Configuration".
     7898095120,
    --The ID of the chat that will receive this message. It can be a private chat or a group. The chat_id can be found in "Chat List Manager".
    --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: "document" indicates sending a file, "photo" indicates sending a photo.
     'document',
     -- Write a caption for the file. Optional. In Telegram, this caption will appear below the photo or file.
     'Report file(01/May/2025)',
     -- The full path of the file to be sent.
     'C:\SQLMessenger\userfiles\report\myreport(202504).xlsx' 
); 

4. Interface Table Field Descriptions

telebot_send_message: The message interface table, used for inserting the messages you want to send.

telebot_send_message_h: This table stores processed historical messages. After SQLTelebot processes a message, the data is moved from telebot_send_message to this table.

Field descriptions of the interface table:

message_id

BIGINT

The unique ID of the message.

For Oracle database, message_id is generated using "seq_telebot_send_message_id.nextval".

For other databases, this field is automatically generated by the system and does not require manual input.

bot_id

BIGINT

Required. Specifies which Telegram bot sent this message. The bot_id can be found in "Bot Configuration" interface.

chat_id

VARCHAR(1000)

Required. Specifies which chat the message is sent to. It can be a one-on-one chat, a group chat, or a channel.

The chat_id can be found in "Chat List".

To send a message to a channel, the chat_id can be filled with the channel's username (including the "@" symbol), for example, "@testchannel9012001".

message_type

VARCHAR(200)

Required. Specifies the type of message to be sent.

text: Send a text message.

document: Send a file.

photo: Send a photo.

message_text

VARCHAR

Specifies the text content of the message to be sent. According to the Telegram API, this field cannot exceed 4096 characters.

Note: For Oracle databases, due to field length limitations, this field cannot exceed 4000 characters.

For messages of type "text", this field is required. For messages of type "document" or "photo", this field is optional and will be used as the caption for the file.

parse_mode

VARCHAR(200)

Specifies how Telegram should parse the text format in message_text.

MarkdownV2: Displays the content of message_text in Markdown format.

HTML: Displays the content of message_text in HTML format.

If this field is not set, the content of message_text will be displayed as plain text.

attach_file_name

VARCHAR(5000)

If message_type is "document" or "photo," you need to specify the file name to be sent here.

Note: The file to be sent must be copied in advance to the "userfiles" folder under the SQLMessenger installation directory.

When filling out this field, please provide the full path of the file, such as "C:\SQLMessenger\userfiles\report2025\20250501.xlsx."

max_retries

INTEGER

Optional. Specify the maximum number of retries if an error occurs while sending the message. The default is a maximum of 5 retries.

When SQLTelebot attempts to send a message and encounters an error, it will automatically retry. After reaching the maximum retry limit, the message will no longer be sent.

expiration_time

DATETIME

Optional. The expiration time of the message. If the message is not processed within this time, SQLTelebot will give up sending it. By default, the message expires 24 hours after it is created.

Note: If your database and the computer where SQLMessenger is installed are in different time zones, you will need to handle the time difference between the two systems yourself.

retry_count

INTEGER

Filled by SQLTelebot. Records the number of times this message has been retried for sending.

create_time

DATETIME

The creation time of the message. Automatically filled by the system.

Note: If your database and the computer where SQLMessenger is installed are in different time zones, you will need to handle the time difference between the two systems yourself.

state

CHAR(1)

The status of the message. This field is automatically filled by the system and can have the following values:

W: Message is waiting to be sent.

T: Message has been retrieved by SQLTelebot and added into the sending queue.

C: Message sent successfully.

F: Message sending failed.

P: Message has expired and was not sent.

state_time

DATETIME

The status time of the message. Automatically filled by the system.

status_desc

VARCHAR(5000)

The status description of the message. Automatically filled by the system.