Re: Simple Telegram client to chat as bot

From NAT, 6 Years ago, written in Plain Text, viewed 634 times. This paste is a reply to Simple Telegram client to chat as bot from NAT - view diff
URL https://code.nat.moe/view/f9845396 Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. TOKEN=""
  3. URL='https://api.telegram.org/bot'$TOKEN
  4. MSG_URL=$URL'/sendMessage'
  5. ACTION_URL=$URL'/sendChatAction'
  6. PHO_URL=$URL'/sendPhoto'
  7. AUDIO_URL=$URL'/sendAudio'
  8. DOCUMENT_URL=$URL'/sendDocument'
  9. STICKER_URL=$URL'/sendSticker'
  10. VIDEO_URL=$URL'/sendVideo'
  11. VOICE_URL=$URL'/sendVoice'
  12.  
  13. alias s=send
  14. alias f=file
  15. alias a=act
  16. alias p=prompt
  17. alias c=chat
  18. alias l=list
  19. alias h=help
  20. alias a=act
  21. alias e=exit
  22.  
  23. trap 'exit' 2 3 15
  24.  
  25. _chat () {
  26.         local cur
  27.         COMPREPLY=()
  28.         cur=${COMP_WORDS[COMP_CWORD]}
  29.         COMPREPLY=( $(compgen -W "$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | sed -e 's/^.*@-/-/g; s/:.*@/: (/g;' | tr ": " "__" | sed -e 's/__/_/g;')" -- $cur) )
  30.         return 0
  31. }
  32.  
  33. complete -F _chat -o filenames chat
  34. complete -F _chat -o filenames c
  35.  
  36. help () {
  37.         echo 'available commands: a(ct), c(hat), f(ile), h(elp), l(ist), p(rompt), s(end).'
  38.         echo 'use e(xit) to exit.'
  39. }
  40.  
  41. chat () {
  42.         str=$(echo "$1" | awk -F"_" '{print $1}')
  43.         [[ -z "$str" ]] && echo "chat: usage: chat <id/name> [-n|--not-group] [-f|--force]" && return
  44.         [[ $* == *"-n"* || $* == *"--not-group"* ]] && {
  45.                 info=$(cat chat.log | grep -v "@" | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep -- "$str" | head -n1 | awk -F": " '{print $2}')
  46.                 id=$(cat chat.log | grep -v "@" | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep -- "$str" | head -n1 | awk -F": " '{print $1}')
  47.         } || {
  48.                 info=$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep -- "$str" | head -n1 | awk -F": " '{print $2}')
  49.                 id=$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep -- "$str" | head -n1 | awk -F": " '{print $1}')
  50.         }
  51.         [[ $id == *"@"* ]] && id=$(echo "$id" | awk -F"@" '{print $2}')
  52.         [[ $info == *"@"* ]] && info=$(echo "$info" | awk -F"@" '{print $2}' | tr -d ")")
  53.         [[ -z $info ]] && {
  54.                 [[ $* == *"--force"* || $* == *"-f"* ]] && {
  55.                         echo "chat: warning: force to set current peer to '$str'."
  56.                         chat=$1
  57.                         chat_str=$chat
  58.                 } || echo "chat: error: unknow peer '$str'. use 'chat $str --force' to set anyway"
  59.         } || {
  60.                 chat=$id
  61.                 chat_str="$info|$chat"
  62.         }
  63.         PS1="$chat_str> "
  64. }
  65.  
  66. list () {
  67.         cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | less
  68. }
  69.  
  70. log () {
  71.         less chat.log
  72. }
  73.  
  74. send () {
  75.         [[ -z $1 ]] && echo "send: usage: send <message>" && return
  76.         [[ -z $chat ]] && echo "send: no current chat. Do 'chat <id>'" && return
  77.         curl -s "$MSG_URL" -F "chat_id=$chat" -F "text=$*"  >> console.log
  78. }
  79.  
  80. file () {
  81.         [[ -z $1 ]] && echo "file: usage: file <file>" && return
  82.         [[ -z $chat ]] && echo "file: no current chat. Do 'chat <id>'" && return
  83.         local chat_id=$chat
  84.         local file=$1
  85.         local ext="${file##*.}"
  86.         case $ext in
  87.                 "mp3")
  88.                         CUR_URL=$AUDIO_URL
  89.                         WHAT=audio
  90.                         STATUS=upload_audio
  91.                         ;;
  92.                 png|jpg|jpeg|gif)
  93.                         CUR_URL=$PHO_URL
  94.                         WHAT=photo
  95.                         STATUS=upload_photo
  96.                         ;;
  97.                 webp)
  98.                         CUR_URL=$STICKER_URL
  99.                         WHAT=sticker
  100.                         STATUS=
  101.                         ;;
  102.                 mp4)
  103.                         CUR_URL=$VIDEO_URL
  104.                         WHAT=video
  105.                         STATUS=upload_video
  106.                         ;;
  107.  
  108.                 ogg)
  109.                         CUR_URL=$VOICE_URL
  110.                         WHAT=voice
  111.                         STATUS=
  112.                         ;;
  113.                 *)
  114.                         CUR_URL=$DOCUMENT_URL
  115.                         WHAT=document
  116.                         STATUS=upload_document
  117.                         ;;
  118.         esac
  119.         curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$3" >> console.log
  120. }
  121.  
  122. prompt () {
  123.         [[ -z $2 ]] && echo "prompt: usage: prompt <phase> <ans> [ans1] ... [ansN]" && return
  124.         [[ -z $chat ]] && echo "prompt: no current chat. Do 'chat <id>'" && return
  125.         local text="$1"
  126.         shift 1
  127.         local keyboard=init
  128.         OLDIFS=$IFS
  129.         IFS=$(echo -en "\"")
  130.         for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done
  131.         IFS=$OLDIFS
  132.         local keyboard=${keyboard/init, /}
  133.         curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}" >> console.log
  134. }
  135.  
  136. act () {
  137.         [[ -z $1 ]] && echo "act: usage: act <typing/upload_photo/record_video/upload_video/record_audio/upload_audio/upload_document/find_location>" && return
  138.         [[ -z $chat ]] && echo "act: no current chat. Do 'chat <id>'" && return
  139.         curl -s "$ACTION_URL" -F "chat_id=$chat" -F "action=$1" >> console.log
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IHVwZGF0ZS5tZXNzYWdlLnRleHQpKQpkZWYgZXJyb3IoYm90LCB1cGRhdGUsIGVycm9yKToKCWxvZ2dlci53YXJuKCdVcGRhdGUgIiVzIiBjYXVzZWQgZXJyb3IgIiVzIicgJSAodXBkYXRlLCBlcnJvcikpCmRlZiBtYWluKCk6Cgl1cGRhdGVyID0gVXBkYXRlcihUT0tFTikKCWRwID0gdXBkYXRlci5kaXNwYXRjaGVyCglkcC5hZGRUZWxlZ3JhbU1lc3NhZ2VIYW5kbGVyKGxvZykKCWRwLmFkZEVycm9ySGFuZGxlcihlcnJvcikKCXVwZGF0ZXIuc3RhcnRfcG9sbGluZygpCgl1cGRhdGVyLmlkbGUoKQppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgoJbWFpbigpCg==' | base64 --decode > $this
  145.         python3 $this "$TOKEN" 2>> chat.log &
  146.         echo $! > chat.pid
  147.         tail -f chat.log
  148. }
  149.  
  150. resizer ()
  151. {
  152.         {
  153.                 while true
  154.                 do
  155.                         lines=$(tput lines)
  156.                         ysize=$(echo "$lines*0.4" | bc | awk -F"." '{print $1}')
  157.                         tmux resize-pane -y $ysize
  158.                         sleep 1
  159.                 done
  160.         } &
  161.         echo $! > resizer.pid
  162. }
  163.  
  164. cli () {
  165.         PS1="bot> " bash --init-file $0 #> console.log
  166. }
  167.  
  168. console () {
  169.         rm -fr console.log
  170.         touch console.log
  171.         tail -f console.log
  172. }
  173.  
  174. function exit {
  175.         tmux kill-session
  176.         kill $(cat resizer.pid)
  177.         kill -9 $(cat chat.pid)
  178.         rm resizer.pid
  179.         rm chat.pid
  180. }
  181.  
  182. function start {
  183.         cols=$(tput cols)
  184.         size=$(echo "$cols*0.8" | bc | awk -F"." '{print $1}')
  185.         tmux new-session -d "$0 receive" && tmux split-window -v "$0 console" && tmux split-window -h "$0 cli" && tmux resize-pane -x $size && resizer && tmux a
  186. }
  187.  
  188. $*

Reply to "Re: Simple Telegram client to chat as bot"

Here you can reply to the paste above

captcha