Skip to content

Instantly share code, notes, and snippets.

@basu021
Last active April 21, 2024 22:37
Show Gist options
  • Save basu021/2079886d3d71291f426ea73bbf52784b to your computer and use it in GitHub Desktop.
Save basu021/2079886d3d71291f426ea73bbf52784b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Telegram Bot Token
TOKEN="6830599987:AAHWoCdShN_potBq7xP6BxR52nuDyf10pT8"
# Telegram Bot API URL
API_URL="https://api.telegram.org/bot$TOKEN"
# Function to send photo to Telegram
send_photo() {
curl -s -X POST "$API_URL/sendPhoto" -F chat_id="$1" -F photo=@"$2"
}
# Function to delete file
delete_file() {
rm -f "$1"
}
# Main function
main() {
offset=0
while true; do
# Get updates from Telegram
updates=$(curl -s "$API_URL/getUpdates?offset=$offset")
# Check if updates are empty
if [[ "$updates" == '{"ok":true,"result":[]}' ]]; then
continue
fi
# Extract messages
messages=$(echo "$updates" | sed -n 's/^.*"message":{\([^}]*\)}.*/\1/p')
# Process each message
while IFS= read -r message; do
chat_id=$(echo "$message" | sed -n 's/^.*"chat":{"id":\([^,}]*\).*/\1/p')
# Log incoming message
echo "Received message"
# Download image
echo "Downloading image..."
curl -s http://192.168.137.248:8080/photo.jpg -o photo.jpg
# Send photo
echo "Sending photo..."
send_photo "$chat_id" "photo.jpg"
# Delete image file
echo "Deleting image..."
delete_file "photo.jpg"
done <<< "$messages"
# Update offset
offset=$(echo "$updates" | sed -n 's/^.*"update_id":\([^,}]*\).*/\1/p' | tail -n 1)
((offset++))
done
}
# Start the script
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment