From b541042e79bd773484c9da0dceca71bed1ede00c Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 2 Jan 2016 21:51:45 +0100 Subject: [PATCH] done! --- bashbot.sh | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 88ac3f8..e6d8ec6 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -26,19 +26,35 @@ send_photo() { } readproc() { - coproc="coproc$1" + coproc="$1" + msg2send="${@/$1/}" + echo $msg2send >&${coproc["0"]} +} + +sendproc() { + coproc="$1" while true;do read msg <&${coproc["0"]}; [ "$?" != "0" ] && return || send_message "$TARGET" "$msg";done } + + process_client() { local MESSAGE=$1 local TARGET=$2 local msg="" - case $MESSAGE in - '/info') msg="This is bashbot, the Telegram bot written entirely in bash.";; - '/question') coproc "coproc$TARGET" { question; }; readproc $TARGET; return; - *) msg="$MESSAGE";; - esac - send_message "$TARGET" "$msg"& + local pid="coproc$TARGET" + [ "$pid" = "" ] { + case $MESSAGE in + '/info') msg="This is bashbot, the Telegram bot written entirely in bash.";; + '/question') coproc "$pid" { question; } &>&1; readproc $TARGET; return;; + *) msg="$MESSAGE";; + esac + send_message "$TARGET" "$msg"& + } || { + + case $MESSAGE in + '/cancel') kill ${$pid};; + *) sendproc "$pid" "$MESSAGE";; + esac } while true; do {