diff --git a/bashbot.sh b/bashbot.sh index e6d8ec6..4bfe80d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -18,22 +18,45 @@ UPD_URL=$URL'/getUpdates?offset=' OFFSET=0 send_message() { - res=$(curl "$MSG_URL" -F "chat_id=$1" -F "text=$2") + [ "$2" != "" ] && res=$(curl "$MSG_URL" -F "chat_id=$1" -F "text=$2") +} +send_keyboard() { + for f in ${@/1\|2/};do keyboard="$keyboard, [\"$f\"]";done + keyboard=${keyboard/^, /} + res=$(curl "$MSG_URL" -F "chat_id=$1" -F "text=$2" -F "reply_markup={\"keyboard\": $keyboard, \"one_time_keyboard\": true}") } send_photo() { res=$(curl "$PHO_URL" -F "chat_id=$1" -F "photo=@$2") } -readproc() { - coproc="$1" - msg2send="${@/$1/}" - echo $msg2send >&${coproc["0"]} +question() { + TARGET="$1' + echo "Why hello there. +Would you like some tea (y/n)?" + read answer + [[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." + until [ "$SUCCESS" = "y" ] ;do + send_keyboard "$TARGET" "Do you like Music?" "Yass!" "No" + read answer + case $answer in + 'Yass!') echo "Goody!";SUCCESS=y;; + 'No') echo "Well that's weird";SUCCESS=y;; + *) SUCCESS=n;; + esac + done } -sendproc() { - coproc="$1" - while true;do read msg <&${coproc["0"]}; [ "$?" != "0" ] && return || send_message "$TARGET" "$msg";done +inproc() { + copname="$1" + msg="${@/1/}" + echo "$msg" >&${$1["0"]} +} + +outproc() { + copname="$1" + TARGET="$2" + while true; do read msg <&${$copname["0"]}; [ "$?" != "0" ] && return || send_message "$TARGET" "$msg";done } @@ -41,20 +64,22 @@ process_client() { local MESSAGE=$1 local TARGET=$2 local msg="" - local pid="coproc$TARGET" - [ "$pid" = "" ] { + local copname="coproc$TARGET" + local copidname="$copname"_PID + local copid="${$copid}" + [ "$copid" = "" ] { 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";; + '/info') send_message "$TARGET" "This is bashbot, the Telegram bot written entirely in bash.";; + '/question') coproc "$copname" { question "$TARGET"; } &>&1; outproc "$copname" "$TARGET"; return;; + *) send_message "$TARGET" "$MESSAGE";; esac - send_message "$TARGET" "$msg"& } || { case $MESSAGE in - '/cancel') kill ${$pid};; - *) sendproc "$pid" "$MESSAGE";; + '/cancel') kill $copid;; + *) inproc "$copname" "$MESSAGE";; esac + } } while true; do { @@ -69,7 +94,6 @@ while true; do { if [ $OFFSET != 1 ]; then process_client "$MESSAGE" "$TARGET"& - fi } &>/dev/null; done