LinuxCommandLibrary

tgsend

Send Telegram messages and files

TLDR

Send a message to a chat

$ tgsend [[-n|--name]] [session_name] [[-u|--username]] [chat_id] "[message]"
copy

Send a message to chat with silent notification
$ tgsend [[-n|--name]] [session_name] [[-u|--username]] [chat_id] --silent "[message]"
copy

SYNOPSIS

tgsend [OPTIONS] <message_text>
tgsend --chatid <chat_id> --token <bot_token> <message_text>
tgsend --file <file_path> --chatid <chat_id> --token <bot_token> [--caption <caption_text>]

PARAMETERS

<message_text>
    The actual text content to be sent to the Telegram chat. This is often the primary argument when sending a text message.

--token <bot_token>
    Specifies the unique API token for your Telegram bot. This token is crucial for authenticating your requests with the Telegram Bot API.

--chatid <chat_id>
    Defines the target identifier for the message. This can be a user's ID, a group chat ID, or a channel username (e.g., @mychannel).

--file <file_path>
    Sends a local file (e.g., photo, document, video) instead of a plain text message. The file will be uploaded to Telegram.

--caption <caption_text>
    Provides a text caption for a file sent using the --file option.

--parse-mode <mode>
    Sets the parse mode for the message text. Common modes include MarkdownV2 or HTML, allowing for rich text formatting.

--disable-notification
    Sends the message silently without triggering a sound notification on the recipient's device.

--disable-web-page-preview
    Disables link previews for URLs included in the message text.

--help
    Displays a brief usage message and available options for the tgsend command.

DESCRIPTION

tgsend is a command-line utility, most commonly implemented as a shell script, designed to send messages to Telegram chats or channels. It leverages the Telegram Bot API to deliver text messages, and often files, directly from a Linux terminal. This utility is particularly useful for integrating Telegram notifications into automated scripts, cron jobs, or system monitoring alerts, allowing for real-time communication of system events or data without requiring a graphical user interface. It provides a simple, programmatic way to interact with Telegram, enabling seamless integration into various system automation workflows.

CAVEATS

The tgsend command is not a standard, built-in Linux utility. Its implementation varies widely, typically being a custom shell script leveraging curl and the Telegram Bot API. Users must obtain a bot token from Telegram's BotFather and know the target chat ID. Bot tokens should be handled securely, preferably not hardcoded in publicly accessible scripts. It requires internet connectivity to communicate with Telegram's servers.

SETUP REQUIREMENTS

To use tgsend, you need to:
1. Create a new bot via Telegram's BotFather to obtain your unique <bot_token>.
2. Get the <chat_id> of the user, group, or channel you want to send messages to. For a user, simply send a message to your bot and then access the Telegram Bot API endpoint https://api.telegram.org/bot<bot_token>/getUpdates (replace <bot_token> with yours) to find the chat_id.

EXAMPLE USAGE

Send a simple text message:
tgsend --token "YOUR_BOT_TOKEN" --chatid "YOUR_CHAT_ID" "Hello from my Linux server!"

Send a message with Markdown formatting:
tgsend --token "YOUR_BOT_TOKEN" --chatid "YOUR_CHAT_ID" --parse-mode MarkdownV2 "*Important*: Server alert! CPU usage is at _90%_."

Send a file with a caption:
tgsend --token "YOUR_BOT_TOKEN" --chatid "YOUR_CHAT_ID" --file /var/log/syslog --caption "Latest syslog file"

HISTORY

The tgsend command, in its various forms, emerged as a popular, community-driven solution following the introduction of the Telegram Bot API. As system administrators and developers sought ways to integrate real-time notifications into their automation pipelines, custom shell scripts mimicking a command-line interface for Telegram became prevalent. Its development is decentralized, with numerous custom implementations evolving to meet specific needs for sending messages, files, and other bot API interactions directly from the terminal or automated scripts.

SEE ALSO

curl(1)

Copied to clipboard