Send Messages to Telegram Contacts in Your SQL Query

1 About Interface Table Feature

2 Configure Interface Table Feature

3 Send Messages via Interface Table

4 Interface Table Format Description

1 About Interface Table Feature

Telebot provides the ability to send messages through interface table.

You can create an interface table in your database and insert the messages you want to send into this table. Telebot can read this table and send the messages through the Telegram Bots to the specified person or group.

Telebot supports sending messages from four types of databases: SQLServer (2005 or higher), Oracle (11g or higher), MySQL (version 5.7.26 or higher), and PostgreSQL (version 9.0 or higher).

2 Configure Interface Table Feature

Here is the method for configuring the interface table feature, using PostgreSQL as an example.

Step1: Create the interface table in your database.

Download the SQL script to create the interface table based on your database type.

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 downloading, run the SQL script in your database to create the interface table.

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

telebot_send_message

The message interface table. You can insert the messages you want to send into this table.

telebot_send_message_h

The table for storing processed historical messages. After Telebot processes a message, the processed data will be moved from telebot_send_message to this table for future querying and management.

For Oracle databases, the script will also create the seq_telebot_send_message_id sequence in the database to generate the message_id in telebot_send_message.

For more details about the interface table, please refer to the Interface Table Format Description.

Step2: Add your database to the data source list in SQLMessenger. Click here for detailed instructions on configuring data sources.

Step3: Add the data source to the monitoring list in Telebot:

Click the "Interface Table Feature Configuration" button in the "Telebot Manager".

Click the "Add Data Source Monitoring" button in the list.

Data Sources Monitoring Configuration dialog

In this dialog box:

DataSource

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

Polling Interval

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

Click "OK" to save. At this point, Telebot will read the interface table at regular intervals (the interval is determined by the "Polling Interval" parameter) and process new message sending requests.

3 Send Messages via Interface Table

Once the above settings are completed, you can use SQL queries in your database to send messages. The SQL syntax for Oracle databases differs from that of other databases.

To send 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 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'
); 

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'
); 

To send a file message or 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 Format Description

telebot_send_message: The message interface table. You can insert the messages you want to send into this table.

telebot_send_message_h: The table for storing processed historical messages. After Telebot processes a message, the processed data will be moved from telebot_send_message to this table.

Field descriptions of the interface table:

message_id

BIGINT

The unique ID of the message.

In Oracle database, the message_id is generated using "seq_telebot_send_message_id.nextval".

For other databases, it is generated automatically by the system and does not need to be filled in.

bot_id

BIGINT

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

chat_id

VARCHAR(1000)

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

The chat_id can be found in the "Chat List Manager".

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. The Telegram API requires that the length of this field cannot exceed 4096 characters.

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

For "text" messages, this field is mandatory. For "document" and "photo" type messages, this field is optional and will be sent 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 the 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 Telebot 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, Telebot 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 Telebot. 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. Automatically filled by the system. It can have the following values:

W: Message is waiting to be sent.

T: Message has been retrieved by Telebot 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.