Telegram Image Fetch Bot

From nat, 5 Years ago, written in Bash, viewed 333 times.
URL https://code.nat.moe/view/f9d33e2f Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. TOKEN=''
  3. URL='https://api.telegram.org/bot'$TOKEN
  4. FILE='https://api.telegram.org/file/bot'$TOKEN
  5. POLL_TIME=30
  6. IMG_DIR='./tg_imgs'
  7.  
  8. while true; do {
  9.   objs="$(
  10.    curl -s "$URL/getUpdates" \
  11.      -d "offset=${offset:-0}" \
  12.      -d "timeout=${POLL_TIME}" \
  13.      -d 'allowed_updates=["message"]' \
  14.      -m ${POLL_TIME} | jq '.result []'
  15.  )"
  16.   offset="$(($(jq '.update_id' <<< "$objs" | tail -n1) + 1))"
  17.   jq -cr '.message | .photo, .from' <<< "$objs"
  18. }; done | while read photo; do {
  19.   read user
  20.   [ "$photo" = null ] && continue
  21.   jq -cr '.[] | .file_id, .width, .height, .file_size' <<< "$photo" | while read fid; do {
  22.     read w; read h; read file_size;
  23.     jq -cr '.id, .first_name, .last_name, .username' <<< "$user" | {
  24.       read uid; read fname; read lname; read uname
  25.       curl -s "$URL/getFile" -d "file_id=$fid" | jq -cr '.result .file_path' | {
  26.         read path
  27.         echo "[$(date)] Got image from $fname $lname (@$uname, id $uid), ID: $fid, ${w}x${h} $file_size bytes."
  28.         ext="$(rev <<< "$path" | cut -d. -f1 | rev)"
  29.         curl -s $FILE/$path > $IMG_DIR/$uid-$fid.$ext &
  30.       }
  31.     }
  32.   }; done
  33. }; done
  34.  

Reply to "Telegram Image Fetch Bot"

Here you can reply to the paste above

captcha