Simple Telegram client to chat as bot

From NAT, 6 Years ago, written in Plain Text, viewed 848 times.
URL https://code.nat.moe/view/4e4a3da1 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. help () {
  26.         echo 'available commands: a(ct), c(hat), f(ile), h(elp), l(ist), p(rompt), s(end).'
  27.         echo 'use e(xit) to exit.'
  28. }
  29.  
  30. chat () {
  31.         [[ -z $1 ]] && echo "chat: usage: chat <id/name>" && return
  32.         info=$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep $1 | head -n1 | awk -F": " '{print $2}')
  33.         id=$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep $1 | head -n1 | awk -F": " '{print $1}')
  34.         [[ $id == *"@"* ]] && id=$(echo "$id" | awk -F"@" '{print $2}')
  35.         [[ $info == *"@"* ]] && info=$(echo "$info" | awk -F"@" '{print $2}' | tr -d ")")
  36.         [[ -z $info ]] && {
  37.                 [[ $2 == "--force" ]] && {
  38.                         echo "chat: warning: force to set current peer to '$1'."
  39.                         chat=$1
  40.                         chat_str=$chat
  41.                 } || echo "chat: error: unknow peer '$1'. use 'chat $1 --force' to set anyway"
  42.         } || {
  43.                 chat=$id
  44.                 chat_str="$info|$chat"
  45.         }
  46.         PS1="$chat_str> "
  47. }
  48.  
  49. list () {
  50.         cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | less
  51. }
  52.  
  53. log () {
  54.         less chat.log
  55. }
  56.  
  57. send () {
  58.         [[ -z $1 ]] && echo "send: usage: send <message>" && return
  59.         [[ -z $chat ]] && echo "send: no current chat. Do 'chat <id>'" && return
  60.         curl -s "$MSG_URL" -F "chat_id=$chat" -F "text=$*"  >> console.log
  61. }
  62.  
  63. file () {
  64.         [[ -z $1 ]] && echo "file: usage: file <file>" && return
  65.         [[ -z $chat ]] && echo "file: no current chat. Do 'chat <id>'" && return
  66.         local chat_id=$chat
  67.         local file=$1
  68.         local ext="${file##*.}"
  69.         case $ext in
  70.                 "mp3")
  71.                         CUR_URL=$AUDIO_URL
  72.                         WHAT=audio
  73.                         STATUS=upload_audio
  74.                         ;;
  75.                 png|jpg|jpeg|gif)
  76.                         CUR_URL=$PHO_URL
  77.                         WHAT=photo
  78.                         STATUS=upload_photo
  79.                         ;;
  80.                 webp)
  81.                         CUR_URL=$STICKER_URL
  82.                         WHAT=sticker
  83.                         STATUS=
  84.                         ;;
  85.                 mp4)
  86.                         CUR_URL=$VIDEO_URL
  87.                         WHAT=video
  88.                         STATUS=upload_video
  89.                         ;;
  90.  
  91.                 ogg)
  92.                         CUR_URL=$VOICE_URL
  93.                         WHAT=voice
  94.                         STATUS=
  95.                         ;;
  96.                 *)
  97.                         CUR_URL=$DOCUMENT_URL
  98.                         WHAT=document
  99.                         STATUS=upload_document
  100.                         ;;
  101.         esac
  102.         curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$3" >> console.log
  103. }
  104.  
  105. prompt () {
  106.         [[ -z $2 ]] && echo "prompt: usage: prompt <phase> <ans> [ans1] ... [ansN]" && return
  107.         [[ -z $chat ]] && echo "prompt: no current chat. Do 'chat <id>'" && return
  108.         local text="$1"
  109.         shift 1
  110.         local keyboard=init
  111.         OLDIFS=$IFS
  112.         IFS=$(echo -en "\"")
  113.         for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done
  114.         IFS=$OLDIFS
  115.         local keyboard=${keyboard/init, /}
  116.         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
  117. }
  118.  
  119. act () {
  120.         [[ -z $1 ]] && echo "act: usage: act <typing/upload_photo/record_video/upload_video/record_audio/upload_audio/upload_document/find_location>" && return
  121.         [[ -z $chat ]] && echo "act: no current chat. Do 'chat <id>'" && return
  122.         curl -s "$ACTION_URL" -F "chat_id=$chat" -F "action=$1" >> console.log
  123. }
  124.  
  125. receive () {
  126.         this=$(mktemp)
  127.         echo 'IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwojIC0qLSBjb2Rpbmc6IHV0Zi04IC0qLQppbXBvcnQgc3lzCmZyb20gdGVsZWdyYW0uZXh0IGltcG9ydCBVcGRhdGVyCmltcG9ydCBsb2dnaW5nClRPS0VOPXN5cy5hcmd2WzFdCmxvZ2dpbmcuYmFzaWNDb25maWcoCiAgICAgICAgZm9ybWF0PSdbJShsZXZlbG5hbWUpc10gJShhc2N0aW1lKXMgLSAlKG1lc3NhZ2UpcycsCiAgICAgICAgbGV2ZWw9bG9nZ2luZy5JTkZPKQpsb2dnZXIgPSBsb2dnaW5nLmdldExvZ2dlcihfX25hbWVfXykKZGVmIGxvZyhib3QsIHVwZGF0ZSk6CglpZiAicHJpdmF0ZSIgbm90IGluIHVwZGF0ZS5tZXNzYWdlLmNoYXQudHlwZToKCQlsb2dnZXIuaW5mbygnWyVzQCVzXSA8JXMgJXMgKCVzQCVzKT4gJXMnICUgKHVwZGF0ZS5tZXNzYWdlLmZyb21fdXNlci5pZCAsIHVwZGF0ZS5tZXNzYWdlLmNoYXRfaWQsIHVwZGF0ZS5tZXNzYWdlLmZyb21fdXNlci5maXJzdF9uYW1lLCB1cGRhdGUubWVzc2FnZS5mcm9tX3VzZXIubGFzdF9uYW1lLCB1cGRhdGUubWVzc2FnZS5mcm9tX3VzZXIudXNlcm5hbWUsIHVwZGF0ZS5tZXNzYWdlLmNoYXQudGl0bGUsIHVwZGF0ZS5tZXNzYWdlLnRleHQpKQoJZWxzZToKCQlsb2dnZXIuaW5mbygnWyVzXSA8JXMgJXMgKCVzKT4gJXMnICUgKHVwZGF0ZS5tZXNzYWdlLmNoYXRfaWQsIHVwZGF0ZS5tZXNzYWdlLmNoYXQuZmlyc3RfbmFtZSwgdXBkYXRlLm1lc3NhZ2UuY2hhdC5sYXN0X25hbWUsIHVwZGF0ZS5tZXNzYWdlLmNoYXQudXNlcm5hbWUsIHVwZGF0ZS5tZXNzYWdlLnRleHQpKQpkZWYgZXJyb3IoYm90LCB1cGRhdGUsIGVycm9yKToKCWxvZ2dlci53YXJuKCdVcGRhdGUgIiVzIiBjYXVzZWQgZXJyb3IgIiVzIicgJSAodXBkYXRlLCBlcnJvcikpCmRlZiBtYWluKCk6Cgl1cGRhdGVyID0gVXBkYXRlcihUT0tFTikKCWRwID0gdXBkYXRlci5kaXNwYXRjaGVyCglkcC5hZGRUZWxlZ3JhbU1lc3NhZ2VIYW5kbGVyKGxvZykKCWRwLmFkZEVycm9ySGFuZGxlcihlcnJvcikKCXVwZGF0ZXIuc3RhcnRfcG9sbGluZygpCgl1cGRhdGVyLmlkbGUoKQppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgoJbWFpbigpCg==' | base64 --decode > $this
  128.         python3 $this "$TOKEN" 2>> chat.log &
  129.         echo $! > chat.pid
  130.         tail -f chat.log
  131. }
  132.  
  133. resizer ()
  134. {
  135.         {
  136.                 while true
  137.                 do
  138.                         lines=$(tput lines)
  139.                         ysize=$(echo "$lines*0.4" | bc | awk -F"." '{print $1}')
  140.                         tmux resize-pane -y $ysize
  141.                         sleep 1
  142.                 done
  143.         } &
  144.         echo $! > resizer.pid
  145. }
  146.  
  147. cli () {
  148.         PS1="bot> " bash --init-file $0 #> console.log
  149. }
  150.  
  151. console () {
  152.         rm -fr console.log
  153.         touch console.log
  154.         tail -f console.log
  155. }
  156.  
  157. function exit {
  158.         tmux kill-session
  159.         kill $(cat resizer.pid)
  160.         kill -9 $(cat chat.pid)
  161.         rm resizer.pid
  162.         rm chat.pid
  163. }
  164.  
  165. function start {
  166.         cols=$(tput cols)
  167.         size=$(echo "$cols*0.8" | bc | awk -F"." '{print $1}')
  168.         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
  169. }
  170.  
  171. $*

Replies to Simple Telegram client to chat as bot rss

Title Name Language When
Re: Simple Telegram client to chat as bot NAT text 6 Years ago.

Reply to "Simple Telegram client to chat as bot"

Here you can reply to the paste above

captcha