From 6eca34758fb9fc36e524add8ce1853227102e378 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Jan 2016 16:06:41 +0100 Subject: [PATCH 01/10] fixed some more bugs --- bashbot.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index a4b8d1e..b0c473d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -144,7 +144,7 @@ forward() { startproc() { mkdir -p "$copname" mkfifo $copname/out - tmux new-session -d -s $copname "./question 2>&1>$copname/out" + tmux new-session -d -s $copname "./question &>$copname/out" local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g') echo $pid>$copname/pid while ps aux | grep -v grep | grep -q $pid;do @@ -191,7 +191,7 @@ process_client() { # Location LOCATION[LONGITUDE]=$(echo "$res" | egrep '\["result",0,"message","location","longitude"\]' | cut -f 2 | cut -d '"' -f 2) LOCATION[LATITUDE]=$(echo "$res" | egrep '\["result",0,"message","location","latitude"\]' | cut -f 2 | cut -d '"' -f 2) - NAME="$(basename ${URLS[*]})" + NAME="$(basename ${URLS[*]} &>/dev/null)" # Tmux copname="CO${USER[ID]}" @@ -199,12 +199,12 @@ process_client() { copid="$(cat $copidname 2>/dev/null)" if [ "$copid" = "" ]; then - [ "${URLS[*]}" != "" ] && { + [ ! -z "${URLS[*]}" ] && { curl -s ${URLS[*]} -o $NAME send_file "${USER[ID]}" "$NAME" "$CAPTION" rm "$NAME" } - [ "${LOCATION[*]}" != "" ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" + [ ! -z "${LOCATION[*]}" ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" case $MESSAGE in '/question') startproc& From 43c468e33691665d16d234f3d74ba6291f21c7c2 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Jan 2016 16:47:58 +0100 Subject: [PATCH 02/10] Fix loop --- bashbot.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index b0c473d..7d32d7b 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -70,12 +70,14 @@ send_keyboard() { local text="$2" shift 2 local keyboard=init - for f in $*;do local keyboard="$keyboard, [\"$f\"]";done + OLDIFS=$IFS + IFS=$(echo -en "\"") + for f in $*;do [ ! -z "$f" ] && local keyboard="$keyboard, [\"$f\"]";done + IFS=$OLDIFS local keyboard=${keyboard/init, /} res=$(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}") } - get_file() { [ "$1" != "" ] && echo $FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh -s | egrep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2) From 905d68a9f37e8b653904cfe01f4fbe1df17ea22a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Jan 2016 18:52:37 +0100 Subject: [PATCH 03/10] FIxing bugs --- bashbot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashbot.sh b/bashbot.sh index 7d32d7b..066d8f0 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -72,7 +72,7 @@ send_keyboard() { local keyboard=init OLDIFS=$IFS IFS=$(echo -en "\"") - for f in $*;do [ ! -z "$f" ] && local keyboard="$keyboard, [\"$f\"]";done + for f in $*;do [ "$f" != " " ] && local keyboard="$keyboard, [\"$f\"]";done IFS=$OLDIFS local keyboard=${keyboard/init, /} res=$(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}") From 471819f9346beb63d090afcd43951831c939868f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Jan 2016 18:54:08 +0100 Subject: [PATCH 04/10] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58660a5..7cb555d 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ To create interactive chats, write (or edit the question script) a normal bash ( The text that the script will output will be sent in real time to the user, and all user input will be sent to the script (as long as it's running or until the user kills it with /cancel). To open up a keyboard in an interactive script, print out the keyboard layout in the following way: ``` -echo "Text that will appear in chat? mykeyboardstartshere Yep No" +echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\"" ``` Same goes for files: ``` @@ -142,7 +142,7 @@ echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45" ``` You can combine them: ``` -echo "Text that will appear in chat? mykeyboardstartshere Yep No myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" +echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\" myfilelocationstartshere /home/user/doge.jpg mylatstartshere 45 mylongstartshere 45" ``` From c1456a3119dedc988168994244c88848c5979f15 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Jan 2016 19:01:24 +0100 Subject: [PATCH 05/10] Update bashbot.sh --- bashbot.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bashbot.sh b/bashbot.sh index 066d8f0..0873e01 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -146,6 +146,7 @@ forward() { startproc() { mkdir -p "$copname" mkfifo $copname/out + tmux kill -s $copname tmux new-session -d -s $copname "./question &>$copname/out" local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g') echo $pid>$copname/pid From 931cf51a7cf484714be86de2edef99f2d68042eb Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 22 Jan 2016 19:03:12 +0100 Subject: [PATCH 06/10] Update bashbot.sh --- bashbot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashbot.sh b/bashbot.sh index 0873e01..f881871 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -146,7 +146,7 @@ forward() { startproc() { mkdir -p "$copname" mkfifo $copname/out - tmux kill -s $copname + tmux kill-session -t $copname tmux new-session -d -s $copname "./question &>$copname/out" local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g') echo $pid>$copname/pid From 6e3e156f42cbeef08dda00803563b8c3700fc259 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 25 Jan 2016 13:37:05 +0100 Subject: [PATCH 07/10] Fixed other bugs --- bashbot.sh | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f881871..2400fa3 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -142,25 +142,22 @@ forward() { res=$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3") } - startproc() { - mkdir -p "$copname" - mkfifo $copname/out + rm -r $copname + mkfifo $copname tmux kill-session -t $copname - tmux new-session -d -s $copname "./question &>$copname/out" - local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g') - echo $pid>$copname/pid - while ps aux | grep -v grep | grep -q $pid;do + tmux new-session -d -s $copname "./question &>$copname" + while tmux ls | grep -q $copname;do read -t 10 line [ "$line" != "" ] && send_message "${USER[ID]}" "$line" line= - done <$copname/out + done <$copname + rm -r $copname } inproc() { tmux send-keys -t $copname "$MESSAGE ${URLS[*]} " - ps aux | grep -v grep | grep -q "$copid" || { rm -r $copname; }; } process_client() { @@ -198,16 +195,14 @@ process_client() { # Tmux copname="CO${USER[ID]}" - copidname="$copname/pid" - copid="$(cat $copidname 2>/dev/null)" - if [ "$copid" = "" ]; then - [ ! -z "${URLS[*]}" ] && { + if ! tmux ls | grep -q $copname; then + [ ! -z ${URLS[*]} ] && { curl -s ${URLS[*]} -o $NAME send_file "${USER[ID]}" "$NAME" "$CAPTION" rm "$NAME" } - [ ! -z "${LOCATION[*]}" ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" + [ ! -z ${LOCATION[*]} ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" case $MESSAGE in '/question') startproc& From 79a7a7ee4ac534ac14a5c22690a31c184efd76b3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 6 Mar 2016 21:30:25 +0100 Subject: [PATCH 08/10] Update question --- question | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/question b/question index b5dcc9e..98721ef 100755 --- a/question +++ b/question @@ -5,7 +5,7 @@ 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 - echo "Do you like Music? mykeyboardstartshere Yass! No" + echo 'Do you like Music? mykeyboardstartshere "Yass!" "No"' read answer case $answer in 'Yass!') echo "Goody!";SUCCESS=y;; From a7801823ce73f9d0715e2a7e5e9a6062d5ea8125 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 20 Mar 2016 22:34:55 +0100 Subject: [PATCH 09/10] Fixed last bugs --- bashbot.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 2400fa3..1020ac2 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -1,6 +1,6 @@ #!/bin/bash # bashbot, the Telegram bot written in bash. -# Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (danog) +# Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (@danog) # https://github.com/topkecleon/telegram-bot-bash # Depends on ./JSON.sh (http://github.com/dominictarr/./JSON.sh), @@ -51,7 +51,7 @@ send_message() { local sent=y fi if [ "$file" != "" ]; then - send_file "$chat" "$file" + send_file "$chat" "$file" "$text" local sent=y fi if [ "$lat" != "" -a "$long" != "" ]; then @@ -232,7 +232,7 @@ Contribute to the project: https://github.com/topkecleon/telegram-bot-bash else case $MESSAGE in '/cancel') - kill $copid + tmux kill-session -t $copname rm -r $copname send_message "${USER[ID]}" "Command canceled." ;; @@ -241,7 +241,8 @@ Contribute to the project: https://github.com/topkecleon/telegram-bot-bash fi } -while true; do { +# source the script with source as param to use functions in other scripts +while [ "$1" != "source" ]; do { res=$(curl -s $UPD_URL$OFFSET | ./JSON.sh -s) From 994bfa3100858eb01af04b27c6723a08b9b91e2d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 20 Mar 2016 23:40:00 +0100 Subject: [PATCH 10/10] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 7cb555d..eda4012 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,9 @@ Once you're done editing start the bot with ```tmux new-session -d -s bashbot ". To stop the bot run ```tmux kill-session -t bashbot```. If some thing doesn't work as it should, debug with ```bash -x bashbot.sh```. +To use the functions provided in this script in other scripts source bashbot.sh: ```source bashbot.sh source``` + + That's it! If you feel that there's something missing or if you found a bug, feel free to submit a pull request!