1
0
mirror of https://github.com/danog/id3bot.git synced 2024-11-30 04:19:02 +01:00

Merge pull request #8 from danog/patch-2

Fixed bugs and added option to source functions from other script
This commit is contained in:
Drew 2016-03-22 09:00:55 -04:00
commit 652be0893f
3 changed files with 26 additions and 24 deletions

View File

@ -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). 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: 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: Same goes for files:
``` ```
@ -142,7 +142,7 @@ echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45"
``` ```
You can combine them: 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```. 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```. 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! That's it!
If you feel that there's something missing or if you found a bug, feel free to submit a pull request! If you feel that there's something missing or if you found a bug, feel free to submit a pull request!

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# bashbot, the Telegram bot written in 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 # https://github.com/topkecleon/telegram-bot-bash
# Depends on ./JSON.sh (http://github.com/dominictarr/./JSON.sh), # Depends on ./JSON.sh (http://github.com/dominictarr/./JSON.sh),
@ -51,7 +51,7 @@ send_message() {
local sent=y local sent=y
fi fi
if [ "$file" != "" ]; then if [ "$file" != "" ]; then
send_file "$chat" "$file" send_file "$chat" "$file" "$text"
local sent=y local sent=y
fi fi
if [ "$lat" != "" -a "$long" != "" ]; then if [ "$lat" != "" -a "$long" != "" ]; then
@ -74,12 +74,14 @@ send_keyboard() {
local text="$2" local text="$2"
shift 2 shift 2
local keyboard=init 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, /} 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}") 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() { 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) [ "$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") res=$(curl -s "$FORWARD_URL" -F "chat_id=$1" -F "from_chat_id=$2" -F "message_id=$3")
} }
startproc() { startproc() {
mkdir -p "$copname" rm -r $copname
mkfifo $copname/out mkfifo $copname
tmux new-session -d -s $copname "./question 2>&1>$copname/out" tmux kill-session -t $copname
local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g') tmux new-session -d -s $copname "./question &>$copname"
echo $pid>$copname/pid while tmux ls | grep -q $copname;do
while ps aux | grep -v grep | grep -q $pid;do
read -t 10 line read -t 10 line
[ "$line" != "" ] && send_message "${USER[ID]}" "$line" [ "$line" != "" ] && send_message "${USER[ID]}" "$line"
line= line=
done <$copname/out done <$copname
rm -r $copname
} }
inproc() { inproc() {
tmux send-keys -t $copname "$MESSAGE ${URLS[*]} tmux send-keys -t $copname "$MESSAGE ${URLS[*]}
" "
ps aux | grep -v grep | grep -q "$copid" || { rm -r $copname; };
} }
process_client() { process_client() {
@ -195,20 +195,18 @@ process_client() {
# Location # Location
LOCATION[LONGITUDE]=$(echo "$res" | egrep '\["result",0,"message","location","longitude"\]' | cut -f 2 | cut -d '"' -f 2) 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) 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 # Tmux
copname="CO${USER[ID]}" copname="CO${USER[ID]}"
copidname="$copname/pid"
copid="$(cat $copidname 2>/dev/null)"
if [ "$copid" = "" ]; then if ! tmux ls | grep -q $copname; then
[ "${URLS[*]}" != "" ] && { [ ! -z ${URLS[*]} ] && {
curl -s ${URLS[*]} -o $NAME curl -s ${URLS[*]} -o $NAME
send_file "${USER[ID]}" "$NAME" "$CAPTION" send_file "${USER[ID]}" "$NAME" "$CAPTION"
rm "$NAME" rm "$NAME"
} }
[ "${LOCATION[*]}" != "" ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}" [ ! -z ${LOCATION[*]} ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}"
case $MESSAGE in case $MESSAGE in
'/question') '/question')
startproc& startproc&
@ -238,7 +236,7 @@ Contribute to the project: https://github.com/topkecleon/telegram-bot-bash
else else
case $MESSAGE in case $MESSAGE in
'/cancel') '/cancel')
kill $copid tmux kill-session -t $copname
rm -r $copname rm -r $copname
send_message "${USER[ID]}" "Command canceled." send_message "${USER[ID]}" "Command canceled."
;; ;;
@ -247,7 +245,8 @@ Contribute to the project: https://github.com/topkecleon/telegram-bot-bash
fi 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) res=$(curl -s $UPD_URL$OFFSET | ./JSON.sh -s)

View File

@ -5,7 +5,7 @@ Would you like some tea (y/n)?"
read answer 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." [[ $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 until [ "$SUCCESS" = "y" ] ;do
echo "Do you like Music? mykeyboardstartshere Yass! No" echo 'Do you like Music? mykeyboardstartshere "Yass!" "No"'
read answer read answer
case $answer in case $answer in
'Yass!') echo "Goody!";SUCCESS=y;; 'Yass!') echo "Goody!";SUCCESS=y;;