You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过外部脚本向telegram-cli发送命令并管理其输出?

How to Send Commands to telegram-cli & Automate Message Workflows

Looks like you've already got the output capture part down, but the missing piece is two-way communication with telegram-cli. Right now your setup only redirects stdout to a pipe, but stdin is still tied to the terminal—so you can't send commands from your script. Let's fix that and build out the message save/delete workflow you need.

Step 1: Set Up Bidirectional Communication with Named Pipes

Instead of a single pipe, we'll use two named pipes (FIFOs) to handle input (sending commands to telegram-cli) and output (capturing its responses) separately.

First, create the pipes in your tg directory:

mkfifo tg_input tg_output

Now start telegram-cli with both stdin and stdout tied to these pipes (don't forget the server public key file, usually tg-server.pub):

./telegram-cli -k tg-server.pub < tg_input > tg_output 2>&1

The 2>&1 redirects stderr to stdout so you can capture error messages too.

Step 2: Updated Script to Send Commands & Process Output

Here's an improved version of your read_and_execute.sh that can send commands, capture messages, save them to a file, and delete them from Telegram. I'll add comments to explain each part:

#!/bin/bash

# Configure paths (update these to match your setup)
TG_INPUT="/home/me/Download/tg/bin/tg_input"
TG_OUTPUT="/home/me/Download/tg/bin/tg_output"
SAVED_MESSAGES="/home/me/tg_saved_messages.txt"
TARGET_CONTACT="@your_contact_username"  # Replace with actual username/ID
MESSAGE_COUNT=50  # Number of recent messages to fetch

# Function to send commands to telegram-cli
send_command() {
    echo "$1" > "$TG_INPUT"
    echo "Sent command: $1"
}

# Function to wait for a specific keyword in output (avoids using arbitrary sleeps)
wait_for_completion() {
    local keyword="$1"
    echo "Waiting for: $keyword"
    while ! grep -q "$keyword" "$TG_OUTPUT"; do
        sleep 1
    done
}

# Clear previous output (optional, keeps things clean)
> "$TG_OUTPUT"

# Start background process to monitor and log output
tail -f "$TG_OUTPUT" | while read -r line; do
    echo "[TG Output] $line"
    # You can add more logic here to parse specific message content if needed
done &
TAIL_PID=$!

# 1. Fetch recent messages from the target contact
send_command "history $TARGET_CONTACT $MESSAGE_COUNT"
wait_for_completion "End of history"  # Wait until history fetch is done

# 2. Extract and save messages to file
# Adjust the grep/awk pattern to match your telegram-cli's output format
# Example format: [2024-05-20 14:30:00] @your_contact_username: Hello there!
echo "Saving messages to $SAVED_MESSAGES..."
grep -A 1 "$TARGET_CONTACT:" "$TG_OUTPUT" >> "$SAVED_MESSAGES"

# 3. Delete the fetched messages from Telegram
# Note: Verify the delete command syntax with `help delete` in telegram-cli first
# Some versions use `delete_messages` instead of `delete`
send_command "delete $TARGET_CONTACT all"
wait_for_completion "Messages deleted"  # Adjust keyword to match your CLI's confirmation

# 4. Exit telegram-cli cleanly
send_command "exit"
wait_for_completion "Exiting..."

# Clean up background tail process
kill $TAIL_PID
wait $TAIL_PID 2>/dev/null

echo "Workflow completed successfully!"

Key Notes & Troubleshooting

  • Command Syntax: Always test commands directly in telegram-cli first (like history, delete) to confirm the syntax—versions can vary. Run help in the CLI to get exact command details.
  • Message Parsing: The grep command in the script is a basic example. If you need more precise parsing, use awk to extract timestamps, usernames, and message content separately.
  • Permissions: Make sure your script has read/write access to the named pipes and output files.
  • Login State: Ensure telegram-cli is already logged in (you'll need to scan the QR code the first time you run it).

内容的提问来源于stack exchange,提问作者Camilla8

火山引擎 最新活动