From 034409c4915af91177577641c6c2c185b7ef66ba Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 9 Jun 2016 09:40:39 -0400 Subject: [PATCH] Fix bugs and update docs --- README.md | 26 ++++++++++++++++---------- bashbot.sh | 12 ++++++++---- commands.sh | 6 +++--- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d7bbbbb..22de4d4 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,12 @@ You can read incoming data using the following variables: * ```${USER[FIRST_NAME]}```: User's first name * ```${USER[LAST_NAME]}```: User's last name * ```${USER[USERNAME]}```: Username +* ```$CHAT```: This array contains the First name, last name, username, title and user id of the chat of the current message. + * ```${CHAT[ID]}```: Chat id + * ```${CHAT[FIRST_NAME]}```: Chat's first name + * ```${CHAT[LAST_NAME]}```: Chat's last name + * ```${CHAT[USERNAME]}```: Username + * ```${CHAT[TITLE]}```: Title * ```$URLS```: This array contains documents, audio files, stickers, voice recordings and stickers stored in the form of URLs. * ```${URLS[AUDIO]}```: Audio files * ```${URLS[VIDEO]}```: Videos @@ -106,43 +112,43 @@ You can read incoming data using the following variables: ### Usage To send messages use the ```send_message``` function: ``` -send_message "${USER[ID]}" "lol" +send_message "${CHAT[ID]}" "lol" ``` To send html or markdown put the following strings before the text, depending on the parsing mode you want to enable: ``` -send_message "${USER[ID]}" "markdown_parse_mode lol *bold*" +send_message "${CHAT[ID]}" "markdown_parse_mode lol *bold*" ``` ``` -send_message "${USER[ID]}" "html_parse_mode lol bold" +send_message "${CHAT[ID]}" "html_parse_mode lol bold" ``` This function also allows a third parameter that disables additional function parsing (for safety use this when reprinting user input): ``` -send_message "${USER[ID]}" "lol" "safe" +send_message "${CHAT[ID]}" "lol" "safe" ``` To send images, videos, voice files, photos ecc use the ```send_photo``` function (remember to change the safety Regex @ line 14 of command.sh to allow sending files only from certain directories): ``` -send_file "${USER[ID]}" "/home/user/doge.jpg" "Lool" +send_file "${CHAT[ID]}" "/home/user/doge.jpg" "Lool" ``` To send custom keyboards use the ```send_keyboard``` function: ``` -send_keyboard "${USER[ID]}" "Text that will appear in chat?" "Yep" "No" +send_keyboard "${CHAT[ID]}" "Text that will appear in chat?" "Yep" "No" ``` To send locations use the ```send_location``` function: ``` -send_location "${USER[ID]}" "Latitude" "Longitude" +send_location "${CHAT[ID]}" "Latitude" "Longitude" ``` To send venues use the ```send_venue``` function: ``` -send_venue "${USER[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id" +send_venue "${CHAT[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id" ``` To forward messages use the ```forward``` function: ``` -forward "${USER[ID]}" "from_chat_id" "message_id" +forward "${CHAT[ID]}" "from_chat_id" "message_id" ``` To send a chat action use the ```send_action``` function. Allowed values: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for locations. ``` -send_action "${USER[ID]}" "action" +send_action "${CHAT[ID]}" "action" ``` To create interactive chats, write (or edit the question script) a normal bash (or C or python) script, chmod +x it and then change the argument of the startproc function to match the command you usually use to start the script. diff --git a/bashbot.sh b/bashbot.sh index a0f389d..4b3f3b9 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -52,7 +52,7 @@ FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/' UPD_URL=$URL'/getUpdates?offset=' GET_URL=$URL'/getFile' OFFSET=0 -declare -A USER MESSAGE URLS CONTACT LOCATION +declare -A USER MESSAGE URLS CONTACT LOCATION CHAT urlencode() { echo "$*" | sed 's:%:%25:g;s: :%20:g;s:<:%3C:g;s:>:%3E:g;s:#:%23:g;s:{:%7B:g;s:}:%7D:g;s:|:%7C:g;s:\\:%5C:g;s:\^:%5E:g;s:~:%7E:g;s:\[:%5B:g;s:\]:%5D:g;s:`:%60:g;s:;:%3B:g;s:/:%2F:g;s:?:%3F:g;s^:^%3A^g;s:@:%40:g;s:=:%3D:g;s:&:%26:g;s:\$:%24:g;s:\!:%21:g;s:\*:%2A:g' @@ -131,7 +131,7 @@ unban_chat_member() { } leave_chat() { - res=$(curl -s "$LEAVE_URL" -F "chat_id=$1") + res=$(curl -s "$LEAVE_URL" -F "chat_id=$1") } answer_inline_query() { @@ -310,7 +310,7 @@ inproc() { process_client() { # Message MESSAGE=$(echo "$res" | egrep '\["result",0,"message","text"\]' | cut -f 2 | cut -d '"' -f 2) - + MESSAGE_ID=$(echo "$res" | egrep '\["result",0,"message","message_id"\]' | cut -f 2 | cut -d '"' -f 2) # Chat CHAT[ID]=$(echo "$res" | egrep '\["result",0,"message","chat","id"\]' | cut -f 2) CHAT[FIRST_NAME]=$(echo "$res" | egrep '\["result",0,"message","chat","first_name"\]' | cut -f 2 | cut -d '"' -f 2) @@ -371,7 +371,11 @@ while [ "$1" == "startbot" ]; do { OFFSET=$((OFFSET+1)) if [ $OFFSET != 1 ]; then - process_client& + if [ "$2" == "test" ]; then + process_client + else + process_client& + fi fi }; done diff --git a/commands.sh b/commands.sh index 960b319..871ba59 100755 --- a/commands.sh +++ b/commands.sh @@ -72,7 +72,7 @@ Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ;; '/leavechat') - send_markdown_message "${CHAT[ID]}" "*CHAT LEAVED*" + send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" leave_chat "${CHAT[ID]}" ;; @@ -82,10 +82,10 @@ Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash) ;; '/cancel') - if tmux ls | grep -q $copname; then killproc && send_message "${USER[ID]}" "Command canceled.";else send_message "${USER[ID]}" "No command is currently running.";fi + if tmux ls | grep -q $copname; then killproc && send_message "${CHAT[ID]}" "Command canceled.";else send_message "${CHAT[ID]}" "No command is currently running.";fi ;; *) - if tmux ls | grep -v send | grep -q $copname;then inproc; else send_message "${USER[ID]}" "$MESSAGE" "safe";fi + if tmux ls | grep -v send | grep -q $copname;then inproc; else send_message "${CHAT[ID]}" "$MESSAGE" "safe";fi ;; esac fi