| name | telecli |
| description | This skill should be used when the user asks to "send a Telegram message", "poll for Telegram updates", "check Telegram bot status", "respond to Telegram messages", "react to messages", "manage Telegram bot", or mentions Telegram Bot API operations. Provides comprehensive guidance for using the tg CLI to interact with Telegram bots. |
Telecli - Telegram Bot CLI
Control Telegram bots via the tg command-line interface. All commands output JSON for easy parsing and automation.
Setup
Configure the bot token (one-time setup):
tg config token <your_bot_token>
tg config token <your_bot_token> --local
export TELEGRAM_BOT_TOKEN="your_bot_token"
Get a bot token from @BotFather on Telegram.
Core Commands
Bot Info
tg me
tg config token
tg config path
Polling for Updates
tg updates poll
tg updates poll --offset 729538157
tg updates poll --timeout 5
Polling loop pattern:
- Call
tg updates poll (blocks until updates arrive)
- Process returned messages
- Calculate next offset:
max(update_id) + 1
- Repeat with
--offset <next_offset>
Sending Messages
tg message send <chat_id> "Hello!"
tg message send <chat_id> "Thanks!" --reply-to <message_id>
tg message send <chat_id> "<b>Bold</b> text" --parse-mode HTML
tg message forward <to_chat_id> <from_chat_id> <message_id>
tg message edit <chat_id> <message_id> "Updated text"
tg message delete <chat_id> <message_id>
Reactions
tg reaction set <chat_id> <message_id> "👍"
tg reaction set <chat_id> <message_id> "😂"
Media
tg photo send <chat_id> /path/to/image.jpg
tg photo send <chat_id> /path/to/image.jpg --caption "Nice photo!"
tg document send <chat_id> /path/to/file.pdf
tg voice send <chat_id> /path/to/audio.ogg
tg file download <file_id> /path/to/save
Chat Management
tg chat get <chat_id>
tg chat members <chat_id>
tg chat admins <chat_id>
tg chat leave <chat_id>
Update Processing
Updates from tg updates poll return JSON with this structure:
{
"ok": true,
"result": [
{
"update_id": 729538157,
"message": {
"message_id": 123,
"from": {
"id": 12345678,
"first_name": "User",
"username": "username"
},
"chat": {
"id": -123456789,
"title": "Group Name",
"type": "group"
},
"date": 1704067200,
"text"
Key fields:
update_id: Use max + 1 as next offset
message.chat.id: Target for replies
message.message_id: Use for replies/reactions
message.from: Sender information
message.text: Message content
Update types:
message: New message
edited_message: Edited message
callback_query: Inline button press
inline_query: Inline mode query
Common Patterns
Reply to Messages
chat_id=$(echo "$update" | jq -r '.message.chat.id')
message_id=$(echo "$update" | jq -r '.message.message_id')
text=$(echo "$update" | jq -r '.message.text')
tg message send "$chat_id" "You said: $text" --reply-to "$message_id"
Continuous Polling Loop
offset=""
while true; do
result=$(tg updates poll $offset)
echo "$result" | jq -c '.result[]' | while read update; do
chat_id=$(echo "$update" | jq -r '.message.chat.id')
text=$(echo "$update" | jq -r '.message.text')
if [ -n "$text" ]; then
tg message send "$chat_id" "Received: $text"
fi
done
new_offset=$(echo "$result" | jq '[.result[].update_id] | max + 1 // empty')
if [ -n "$new_offset" ]; then
offset="--offset $new_offset"
fi
done
Check for Mentions
if echo "$text" | grep -qi "@BotUsername"; then
tg message send "$chat_id" "You called?"
fi
Error Handling
All commands return JSON with ok field:
{"ok": true, "result": {...}} # Success
{"ok": false, "error": "..."} # Error
Check ok field before processing results:
result=$(tg message send 123 "Hello")
if echo "$result" | jq -e '.ok' > /dev/null; then
echo "Message sent!"
else
echo "Error: $(echo "$result" | jq -r '.error')"
fi
Additional Resources
Reference Files
For complete command reference with all options:
references/commands.md - Full command reference with all flags and options
Chat ID Types
- Positive numbers: Private chats (user IDs)
- Negative numbers: Groups and supergroups
- @username: Public channels/groups with usernames