Re: Simple Telegram client to chat as bot

From NAT, 6 Years ago, written in Plain Text, viewed 644 times. This paste is a reply to Simple Telegram client to chat as bot from NAT - go back
URL https://code.nat.moe/view/f9845396/diff Embed
Viewing differences between Simple Telegram client to chat as bot and Re: Simple Telegram client to chat as bot
#!/bin/bash
TOKEN=""
URL='https://api.telegram.org/bot'$TOKEN
MSG_URL=$URL'/sendMessage'
ACTION_URL=$URL'/sendChatAction'
PHO_URL=$URL'/sendPhoto'
AUDIO_URL=$URL'/sendAudio'
DOCUMENT_URL=$URL'/sendDocument'
STICKER_URL=$URL'/sendSticker'
VIDEO_URL=$URL'/sendVideo'
VOICE_URL=$URL'/sendVoice'

alias s=send
alias f=file
alias a=act
alias p=prompt
alias c=chat
alias l=list
alias h=help
alias a=act
alias e=exit

trap 'exit' 2 3 15

_chat () {
        local cur
        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        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) )
        return 0
}

complete -F _chat -o filenames chat
complete -F _chat -o filenames c

help () {
        echo 'available commands: a(ct), c(hat), f(ile), h(elp), l(ist), p(rompt), s(end).'
        echo 'use e(xit) to exit.'
}

chat () {
        str=$(echo "$1" | awk -F"_" '{print $1}')
        
[[ -z $1 "$str" ]] && echo "chat: usage: chat <id/name>" <id/name> [-n|--not-group] [-f|--force]" && return
        [[ $* == *"-n"* || $* == *"--not-group"* ]] && {
                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}')
                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}')
        } || {
                
info=$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep $1 -- "$str" | head -n1 | awk -F": " '{print $2}')
        
$2}')
                
id=$(cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | grep $1 -- "$str" | head -n1 | awk -F": " '{print $1}')
$1}')
        }
        [[ $id == *"@"* ]] && id=$(echo "$id" | awk -F"@" '{print $2}')
        [[ $info == *"@"* ]] && info=$(echo "$info" | awk -F"@" '{print $2}' | tr -d ")")
        [[ -z $info ]] && {
                [[ $2 $* == "--force" *"--force"* || $* == *"-f"* ]] && {
                        echo "chat: warning: force to set current peer to '$1'.'$str'."
                        chat=$1
                        chat_str=$chat
                } || echo "chat: error: unknow peer '$1'. '$str'. use 'chat $1 $str --force' to set anyway"
        } || {
                chat=$id
                chat_str="$info|$chat"
        }
        PS1="$chat_str> "
}

list () {
        cat chat.log | awk -F'[' '{print $3}' | awk -F'>' '{print $1}' | tr -d '<>' | tr ']' ':' | sort | uniq | less
}

log () {
        less chat.log
}

send () {
        [[ -z $1 ]] && echo "send: usage: send <message>" && return
        [[ -z $chat ]] && echo "send: no current chat. Do 'chat <id>'" && return
        curl -s "$MSG_URL" -F "chat_id=$chat" -F "text=$*"  >> console.log
}

file () {
        [[ -z $1 ]] && echo "file: usage: file <file>" && return
        [[ -z $chat ]] && echo "file: no current chat. Do 'chat <id>'" && return
        local chat_id=$chat
        local file=$1
        local ext="${file##*.}"
        case $ext in
                "mp3")
                        CUR_URL=$AUDIO_URL
                        WHAT=audio
                        STATUS=upload_audio
                        ;;
                png|jpg|jpeg|gif)
                        CUR_URL=$PHO_URL
                        WHAT=photo
                        STATUS=upload_photo
                        ;;
                webp)
                        CUR_URL=$STICKER_URL
                        WHAT=sticker
                        STATUS=
                        ;;
                mp4)
                        CUR_URL=$VIDEO_URL
                        WHAT=video
                        STATUS=upload_video
                        ;;

                ogg)
                        CUR_URL=$VOICE_URL
                        WHAT=voice
                        STATUS=
                        ;;
                *)
                        CUR_URL=$DOCUMENT_URL
                        WHAT=document
                        STATUS=upload_document
                        ;;
        esac
        curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$3" >> console.log
}

prompt () {
        [[ -z $2 ]] && echo "prompt: usage: prompt <phase> <ans> [ans1] ... [ansN]" && return
        [[ -z $chat ]] && echo "prompt: no current chat. Do 'chat <id>'" && return
        local text="$1"
        shift 1
        local keyboard=init
        OLDIFS=$IFS
        IFS=$(echo -en "\"")
        for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done
        IFS=$OLDIFS
        local keyboard=${keyboard/init, /}
        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
}

act () {
        [[ -z $1 ]] && echo "act: usage: act <typing/upload_photo/record_video/upload_video/record_audio/upload_audio/upload_document/find_location>" && return
        [[ -z $chat ]] && echo "act: no current chat. Do 'chat <id>'" && return
        curl -s "$ACTION_URL" -F "chat_id=$chat" -F "action=$1" >> console.log
}

receive () {
        this=$(mktemp)
        echo 'IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwojIC0qLSBjb2Rpbmc6IHV0Zi04IC0qLQppbXBvcnQgc3lzCmZyb20gdGVsZWdyYW0uZXh0IGltcG9ydCBVcGRhdGVyCmltcG9ydCBsb2dnaW5nClRPS0VOPXN5cy5hcmd2WzFdCmxvZ2dpbmcuYmFzaWNDb25maWcoCiAgICAgICAgZm9ybWF0PSdbJShsZXZlbG5hbWUpc10gJShhc2N0aW1lKXMgLSAlKG1lc3NhZ2UpcycsCiAgICAgICAgbGV2ZWw9bG9nZ2luZy5JTkZPKQpsb2dnZXIgPSBsb2dnaW5nLmdldExvZ2dlcihfX25hbWVfXykKZGVmIGxvZ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JbWFpbigpCg==' | base64 --decode > $this
        python3 $this "$TOKEN" 2>> chat.log &
        echo $! > chat.pid
        tail -f chat.log
log 
}

resizer () 
{
        {
                while true
                do
                        lines=$(tput lines)
                        ysize=$(echo "$lines*0.4" | bc | awk -F"." '{print $1}')
                        tmux resize-pane -y $ysize
                        sleep 1
                done
        } &
        echo $! > resizer.pid
}

cli () {
        PS1="bot> " bash --init-file $0 #> console.log
}

console () {
        rm -fr console.log 
        touch console.log
        tail -f console.log
}

function exit {
        tmux kill-session
        kill $(cat resizer.pid)
        kill -9 $(cat chat.pid)
        rm resizer.pid
        rm chat.pid
}

function start {
        cols=$(tput cols)
        size=$(echo "$cols*0.8" | bc | awk -F"." '{print $1}')
        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 
}

$*

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

Here you can reply to the paste above

captcha