diff --git a/README.md b/README.md index 58660a5..eda4012 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" ``` @@ -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! diff --git a/bashbot.sh b/bashbot.sh index 9b900fa..51ce1e3 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 @@ -74,12 +74,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 [ "$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) @@ -144,24 +146,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 - tmux new-session -d -s $copname "./question 2>&1>$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 + rm -r $copname + mkfifo $copname + tmux kill-session -t $copname + 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() { @@ -195,20 +195,18 @@ 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]}" - copidname="$copname/pid" - copid="$(cat $copidname 2>/dev/null)" - if [ "$copid" = "" ]; then - [ "${URLS[*]}" != "" ] && { + if ! tmux ls | grep -q $copname; then + [ ! -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& @@ -238,7 +236,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." ;; @@ -247,7 +245,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) 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;;