mirror of
https://github.com/danog/id3bot.git
synced 2024-11-26 11:54:43 +01:00
Added a license for the unfortunate souls whose governments don't allow public domain.
Removed JSON.sh and added the project as a submodule. Modified code referencing it accordingly. Added "attach" argument to attach to the tmux session and a default message if no argument is given. Other minor changes.
This commit is contained in:
parent
55bf7e35a9
commit
62c3d5eb84
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "JSON.sh"]
|
||||||
|
path = JSON.sh
|
||||||
|
url = http://github.com/dominictarr/JSON.sh
|
204
JSON.sh
204
JSON.sh
@ -1,204 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
throw () {
|
|
||||||
echo "$*" >&2
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
BRIEF=0
|
|
||||||
LEAFONLY=0
|
|
||||||
PRUNE=0
|
|
||||||
NO_HEAD=0
|
|
||||||
NORMALIZE_SOLIDUS=0
|
|
||||||
|
|
||||||
usage() {
|
|
||||||
echo
|
|
||||||
echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
|
|
||||||
echo
|
|
||||||
echo "-p - Prune empty. Exclude fields with empty values."
|
|
||||||
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
|
|
||||||
echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
|
|
||||||
echo "-n - No-head. Do not show nodes that have no path (lines that start with [])."
|
|
||||||
echo "-s - Remove escaping of the solidus symbol (stright slash)."
|
|
||||||
echo "-h - This help text."
|
|
||||||
echo
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_options() {
|
|
||||||
set -- "$@"
|
|
||||||
local ARGN=$#
|
|
||||||
while [ "$ARGN" -ne 0 ]
|
|
||||||
do
|
|
||||||
case $1 in
|
|
||||||
-h) usage
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
-b) BRIEF=1
|
|
||||||
LEAFONLY=1
|
|
||||||
PRUNE=1
|
|
||||||
;;
|
|
||||||
-l) LEAFONLY=1
|
|
||||||
;;
|
|
||||||
-p) PRUNE=1
|
|
||||||
;;
|
|
||||||
-n) NO_HEAD=1
|
|
||||||
;;
|
|
||||||
-s) NORMALIZE_SOLIDUS=1
|
|
||||||
;;
|
|
||||||
?*) echo "ERROR: Unknown option."
|
|
||||||
usage
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
shift 1
|
|
||||||
ARGN=$((ARGN-1))
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
awk_egrep () {
|
|
||||||
local pattern_string=$1
|
|
||||||
|
|
||||||
gawk '{
|
|
||||||
while ($0) {
|
|
||||||
start=match($0, pattern);
|
|
||||||
token=substr($0, start, RLENGTH);
|
|
||||||
print token;
|
|
||||||
$0=substr($0, start+RLENGTH);
|
|
||||||
}
|
|
||||||
}' pattern="$pattern_string"
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenize () {
|
|
||||||
local GREP
|
|
||||||
local ESCAPE
|
|
||||||
local CHAR
|
|
||||||
|
|
||||||
if echo "test string" | egrep -ao --color=never "test" &>/dev/null
|
|
||||||
then
|
|
||||||
GREP='egrep -ao --color=never'
|
|
||||||
else
|
|
||||||
GREP='egrep -ao'
|
|
||||||
fi
|
|
||||||
|
|
||||||
if echo "test string" | egrep -o "test" &>/dev/null
|
|
||||||
then
|
|
||||||
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
|
|
||||||
CHAR='[^[:cntrl:]"\\]'
|
|
||||||
else
|
|
||||||
GREP=awk_egrep
|
|
||||||
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
|
|
||||||
CHAR='[^[:cntrl:]"\\\\]'
|
|
||||||
fi
|
|
||||||
|
|
||||||
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
|
|
||||||
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
|
|
||||||
local KEYWORD='null|false|true'
|
|
||||||
local SPACE='[[:space:]]+'
|
|
||||||
|
|
||||||
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_array () {
|
|
||||||
local index=0
|
|
||||||
local ary=''
|
|
||||||
read -r token
|
|
||||||
case "$token" in
|
|
||||||
']') ;;
|
|
||||||
*)
|
|
||||||
while :
|
|
||||||
do
|
|
||||||
parse_value "$1" "$index"
|
|
||||||
index=$((index+1))
|
|
||||||
ary="$ary""$value"
|
|
||||||
read -r token
|
|
||||||
case "$token" in
|
|
||||||
']') break ;;
|
|
||||||
',') ary="$ary," ;;
|
|
||||||
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
|
|
||||||
esac
|
|
||||||
read -r token
|
|
||||||
done
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
|
|
||||||
:
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_object () {
|
|
||||||
local key
|
|
||||||
local obj=''
|
|
||||||
read -r token
|
|
||||||
case "$token" in
|
|
||||||
'}') ;;
|
|
||||||
*)
|
|
||||||
while :
|
|
||||||
do
|
|
||||||
case "$token" in
|
|
||||||
'"'*'"') key=$token ;;
|
|
||||||
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
|
|
||||||
esac
|
|
||||||
read -r token
|
|
||||||
case "$token" in
|
|
||||||
':') ;;
|
|
||||||
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
|
|
||||||
esac
|
|
||||||
read -r token
|
|
||||||
parse_value "$1" "$key"
|
|
||||||
obj="$obj$key:$value"
|
|
||||||
read -r token
|
|
||||||
case "$token" in
|
|
||||||
'}') break ;;
|
|
||||||
',') obj="$obj," ;;
|
|
||||||
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
|
|
||||||
esac
|
|
||||||
read -r token
|
|
||||||
done
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
|
|
||||||
:
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_value () {
|
|
||||||
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
|
|
||||||
case "$token" in
|
|
||||||
'{') parse_object "$jpath" ;;
|
|
||||||
'[') parse_array "$jpath" ;;
|
|
||||||
# At this point, the only valid single-character tokens are digits.
|
|
||||||
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
|
|
||||||
*) value=$token
|
|
||||||
# if asked, replace solidus ("\/") in json strings with normalized value: "/"
|
|
||||||
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=${value//\\\//\/}
|
|
||||||
isleaf=1
|
|
||||||
[ "$value" = '""' ] && isempty=1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
[ "$value" = '' ] && return
|
|
||||||
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
|
|
||||||
|
|
||||||
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1
|
|
||||||
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1
|
|
||||||
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
|
|
||||||
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
|
|
||||||
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
|
|
||||||
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
|
|
||||||
:
|
|
||||||
}
|
|
||||||
|
|
||||||
parse () {
|
|
||||||
read -r token
|
|
||||||
parse_value
|
|
||||||
read -r token
|
|
||||||
case "$token" in
|
|
||||||
'') ;;
|
|
||||||
*) throw "EXPECTED EOF GOT $token" ;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]);
|
|
||||||
then
|
|
||||||
parse_options "$@"
|
|
||||||
tokenize | parse
|
|
||||||
fi
|
|
||||||
|
|
||||||
# vi: expandtab sw=2 ts=2
|
|
1
JSON.sh
Submodule
1
JSON.sh
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 2de457939e9b3fcf0d1e3f55b6072d060cb47a45
|
18
LICENSE
Normal file
18
LICENSE
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
This project is released to the public domain where applicable.
|
||||||
|
|
||||||
|
Otherwise, it is released under the terms of the WTFPLv2:
|
||||||
|
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
Version 2, December 2004
|
||||||
|
|
||||||
|
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim or modified
|
||||||
|
copies of this license document, and changing it is allowed as long
|
||||||
|
as the name is changed.
|
||||||
|
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. You just DO WHAT THE FUCK YOU WANT TO.
|
||||||
|
|
146
README.md
146
README.md
@ -1,7 +1,11 @@
|
|||||||
#bashbot
|
#bashbot
|
||||||
A Telegram bot written in bash.
|
A Telegram bot written in bash.
|
||||||
|
|
||||||
Uses [json.sh](https://github.com/dominictarr/JSON.sh) and tmux (for interactive chats).
|
Depends on [tmux](http://github.com/tmux/tmux).
|
||||||
|
Uses [JSON.sh](http://github.com/dominictarr/JSON.sh).
|
||||||
|
|
||||||
|
Released to the public domain wherever applicable.
|
||||||
|
Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/).
|
||||||
|
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
@ -64,110 +68,110 @@ group. This step is up to you actually.
|
|||||||
|
|
||||||
13. @botfather replies with `Success! The new status is: DISABLED. /help`
|
13. @botfather replies with `Success! The new status is: DISABLED. /help`
|
||||||
|
|
||||||
### Install bashbot
|
### Install bashbot
|
||||||
Clone the repository:
|
Clone the repository:
|
||||||
```
|
```
|
||||||
git clone https://github.com/topkecleon/telegram-bot-bash
|
git clone https://github.com/topkecleon/telegram-bot-bash
|
||||||
```
|
```
|
||||||
|
|
||||||
Paste the token on line 6 of commands.sh (instead of tokenhere).
|
Paste the token on line 6 of commands.sh (instead of tokenhere).
|
||||||
Then start editing the commands.
|
Then start editing the commands.
|
||||||
|
|
||||||
### Receive data
|
|
||||||
You can read incoming data using the following variables:
|
|
||||||
|
|
||||||
* ```$MESSAGE```: Incoming messages
|
### Receive data
|
||||||
* ```$CAPTION```: Captions
|
You can read incoming data using the following variables:
|
||||||
|
|
||||||
|
* ```$MESSAGE```: Incoming messages
|
||||||
|
* ```$CAPTION```: Captions
|
||||||
* ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message.
|
* ```$USER```: This array contains the First name, last name, username and user id of the sender of the current message.
|
||||||
* ```${USER[ID]}```: User id
|
* ```${USER[ID]}```: User id
|
||||||
* ```${USER[FIRST_NAME]}```: User's first name
|
* ```${USER[FIRST_NAME]}```: User's first name
|
||||||
* ```${USER[LAST_NAME]}```: User's last name
|
* ```${USER[LAST_NAME]}```: User's last name
|
||||||
* ```${USER[USERNAME]}```: Username
|
* ```${USER[USERNAME]}```: Username
|
||||||
* ```$URLS```: This array contains documents, audio files, stickers, voice recordings and stickers stored in the form of URLs.
|
* ```$URLS```: This array contains documents, audio files, stickers, voice recordings and stickers stored in the form of URLs.
|
||||||
* ```${URLS[AUDIO]}```: Audio files
|
* ```${URLS[AUDIO]}```: Audio files
|
||||||
* ```${URLS[VIDEO]}```: Videos
|
* ```${URLS[VIDEO]}```: Videos
|
||||||
* ```${URLS[PHOTO]}```: Photos (maximum quality)
|
* ```${URLS[PHOTO]}```: Photos (maximum quality)
|
||||||
* ```${URLS[VOICE]}```: Voice recordings
|
* ```${URLS[VOICE]}```: Voice recordings
|
||||||
* ```${URLS[STICKER]}```: Stickers
|
* ```${URLS[STICKER]}```: Stickers
|
||||||
* ```${URLS[DOCUMENT]}```: Any other file
|
* ```${URLS[DOCUMENT]}```: Any other file
|
||||||
* ```$CONTACT```: This array contains info about contacts sent in a chat.
|
* ```$CONTACT```: This array contains info about contacts sent in a chat.
|
||||||
* ```${CONTACT[NUMBER]}```: Phone number
|
* ```${CONTACT[NUMBER]}```: Phone number
|
||||||
* ```${CONTACT[FIRST_NAME]}```: First name
|
* ```${CONTACT[FIRST_NAME]}```: First name
|
||||||
* ```${CONTACT[LAST_NAME]}```: Last name
|
* ```${CONTACT[LAST_NAME]}```: Last name
|
||||||
* ```${CONTACT[ID]}```: User id
|
* ```${CONTACT[ID]}```: User id
|
||||||
* ```$LOCATION```: This array contains info about locations sent in a chat.
|
* ```$LOCATION```: This array contains info about locations sent in a chat.
|
||||||
* ```${LOCATION[LONGITUDE]}```: Longitude
|
* ```${LOCATION[LONGITUDE]}```: Longitude
|
||||||
* ```${LOCATION[LATITUDE]}```: Latitude
|
* ```${LOCATION[LATITUDE]}```: Latitude
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
To send messages use the ```send_message``` function:
|
To send messages use the ```send_message``` function:
|
||||||
```
|
```
|
||||||
send_message "${USER[ID]}" "lol"
|
send_message "${USER[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*"
|
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]}" "html_parse_mode lol <b>bold</b>"
|
send_message "${USER[ID]}" "markdown_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]}" "html_parse_mode lol <b>bold</b>"
|
||||||
|
```
|
||||||
|
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 "${USER[ID]}" "lol" "safe"
|
||||||
```
|
```
|
||||||
To send images, videos, voice files, photos ecc use the ```send_photo``` function (remember to change the safety Regex @ line 11 of command.sh to allow sending files only from certain directories):
|
To send images, videos, voice files, photos ecc use the ```send_photo``` function (remember to change the safety Regex @ line 11 of command.sh to allow sending files only from certain directories):
|
||||||
```
|
```
|
||||||
send_file "${USER[ID]}" "/home/user/doge.jpg" "Lool"
|
send_file "${USER[ID]}" "/home/user/doge.jpg" "Lool"
|
||||||
```
|
```
|
||||||
To send custom keyboards use the ```send_keyboard``` function:
|
To send custom keyboards use the ```send_keyboard``` function:
|
||||||
```
|
```
|
||||||
send_keyboard "${USER[ID]}" "Text that will appear in chat?" "Yep" "No"
|
send_keyboard "${USER[ID]}" "Text that will appear in chat?" "Yep" "No"
|
||||||
```
|
```
|
||||||
To send locations use the ```send_location``` function:
|
To send locations use the ```send_location``` function:
|
||||||
```
|
```
|
||||||
send_location "${USER[ID]}" "Latitude" "Longitude"
|
send_location "${USER[ID]}" "Latitude" "Longitude"
|
||||||
```
|
```
|
||||||
To send venues use the ```send_venue``` function:
|
To send venues use the ```send_venue``` function:
|
||||||
```
|
```
|
||||||
send_venue "${USER[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id"
|
send_venue "${USER[ID]}" "Latitude" "Longitude" "Title" "Address" "optional foursquare id"
|
||||||
```
|
```
|
||||||
To forward messages use the ```forward``` function:
|
To forward messages use the ```forward``` function:
|
||||||
```
|
```
|
||||||
forward "${USER[ID]}" "from_chat_id" "message_id"
|
forward "${USER[ID]}" "from_chat_id" "message_id"
|
||||||
```
|
```
|
||||||
To send a chat action use the ```send_action``` function.
|
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.
|
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 "${USER[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.
|
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.
|
||||||
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, sure\" \"No, highly unlikely\""
|
echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\""
|
||||||
```
|
```
|
||||||
Same goes for files:
|
Same goes for files:
|
||||||
```
|
```
|
||||||
echo "Text that will appear in chat? myfilelocationstartshere /home/user/doge.jpg"
|
echo "Text that will appear in chat? myfilelocationstartshere /home/user/doge.jpg"
|
||||||
```
|
```
|
||||||
And locations:
|
And locations:
|
||||||
```
|
```
|
||||||
echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45"
|
echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45"
|
||||||
```
|
```
|
||||||
And venues:
|
And venues:
|
||||||
```
|
```
|
||||||
echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 mytitlestartshere my home myaddressstartshere Diagon Alley N. 37"
|
echo "Text that will appear in chat. mylatstartshere 45 mylongstartshere 45 mytitlestartshere my home myaddressstartshere Diagon Alley N. 37"
|
||||||
```
|
```
|
||||||
You can combine them:
|
You can combine them:
|
||||||
```
|
```
|
||||||
echo "Text that will appear in chat? mykeyboardstartshere \"Yep, sure\" \"No, highly unlikely\" 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"
|
||||||
```
|
```
|
||||||
Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords.
|
Please note that you can either send a location or a venue, not both. To send a venue add the mytitlestartshere and the myaddressstartshere keywords.
|
||||||
|
|
||||||
The following commands allows users to interact with your bot via *inline queries*.
|
The following commands allows users to interact with your bot via *inline queries*.
|
||||||
In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bot’s name.
|
In order to enable **inline mode**, send `/setinline` command to [@BotFather](https://telegram.me/botfather) and provide the placeholder text that the user will see in the input field after typing your bot’s name.
|
||||||
Also, edit line 9 from `commands.sh` putting a "1".
|
Also, edit line 9 from `commands.sh` putting a "1".
|
||||||
Note that you can't modify the first two parameters of the function `answer_inline_query`, only the ones after them.
|
Note that you can't modify the first two parameters of the function `answer_inline_query`, only the ones after them.
|
||||||
|
|
||||||
To send messsages or links through an *inline query*:
|
To send messsages or links through an *inline query*:
|
||||||
@ -208,27 +212,27 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker"
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
To modify the responses to commands edit the commands.sh file (this should ease upgrades of the bot core).
|
To modify the responses to commands edit the commands.sh file (this should ease upgrades of the bot core).
|
||||||
|
|
||||||
Once you're done editing start the bot with ```./bashbot.sh start```. If you want to do some more changes make them and then rerun the same command.
|
Once you're done editing start the bot with ```./bashbot.sh start```. If you want to do some more changes make them and then rerun the same command.
|
||||||
To stop the bot run ```./bashbot.sh kill```.
|
To stop the bot run ```./bashbot.sh kill```.
|
||||||
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 simply source bashbot: ```source bashbot.sh```
|
To use the functions provided in this script in other scripts simply source bashbot: ```source bashbot.sh```
|
||||||
|
|
||||||
|
|
||||||
## User count
|
## User count
|
||||||
To count the total number of users that ever used the bot run the following command:
|
To count the total number of users that ever used the bot run the following command:
|
||||||
```
|
```
|
||||||
bash bashbot.sh count
|
bash bashbot.sh count
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Sending broadcasts to all users
|
## Sending broadcasts to all users
|
||||||
To send a broadcast to all of users that ever used the bot run the following command:
|
To send a broadcast to all of users that ever used the bot run the following command:
|
||||||
```
|
```
|
||||||
bash bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!"
|
bash bashbot.sh broadcast "Hey! I just wanted to let you know that the bot's been updated!"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
53
bashbot.sh
53
bashbot.sh
@ -1,16 +1,15 @@
|
|||||||
#!/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 Drew (@topkecleon) and Daniil Gentili (@danogentili).
|
||||||
|
# Also contributed: JuanPotato, BigNerd95, TiagoDanin, iicc1.
|
||||||
# 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) (MIT/Apache),
|
||||||
# which is MIT/Apache-licensed
|
# and on tmux (http://github.com/tmux/tmux) (BSD).
|
||||||
# And on tmux (https://github.com/tmux/tmux),
|
|
||||||
# which is BSD-licensed
|
|
||||||
|
|
||||||
|
|
||||||
# This file is public domain in the USA and all free countries.
|
# This file is public domain in the USA and all free countries.
|
||||||
# If you're in Europe, and public domain does not exist, then haha.
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
|
|
||||||
source commands.sh source
|
source commands.sh source
|
||||||
URL='https://api.telegram.org/bot'$TOKEN
|
URL='https://api.telegram.org/bot'$TOKEN
|
||||||
@ -30,7 +29,7 @@ ACTION_URL=$URL'/sendChatAction'
|
|||||||
FORWARD_URL=$URL'/forwardMessage'
|
FORWARD_URL=$URL'/forwardMessage'
|
||||||
INLINE_QUERY=$URL'/answerInlineQuery'
|
INLINE_QUERY=$URL'/answerInlineQuery'
|
||||||
ME_URL=$URL'/getMe'
|
ME_URL=$URL'/getMe'
|
||||||
ME=$(curl -s $ME_URL | ./JSON.sh -s | egrep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)
|
ME=$(curl -s $ME_URL | ./JSON.sh/JSON.sh -s | egrep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)
|
||||||
|
|
||||||
|
|
||||||
FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/'
|
FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/'
|
||||||
@ -52,11 +51,11 @@ send_message() {
|
|||||||
local lat="$(echo "$2" | sed '/mylatstartshere /!d;s/.*mylatstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g')"
|
local lat="$(echo "$2" | sed '/mylatstartshere /!d;s/.*mylatstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g')"
|
||||||
|
|
||||||
local long="$(echo "$2" | sed '/mylongstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g')"
|
local long="$(echo "$2" | sed '/mylongstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g')"
|
||||||
|
|
||||||
local title="$(echo "$2" | sed '/mytitlestartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ myaddressstartshere.*//g')"
|
local title="$(echo "$2" | sed '/mytitlestartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ myaddressstartshere.*//g')"
|
||||||
|
|
||||||
local address="$(echo "$2" | sed '/myaddressstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g')"
|
local address="$(echo "$2" | sed '/myaddressstartshere /!d;s/.*mylongstartshere //g;s/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mytitlestartshere.*//g')"
|
||||||
|
|
||||||
}
|
}
|
||||||
if [ "$keyboard" != "" ]; then
|
if [ "$keyboard" != "" ]; then
|
||||||
send_keyboard "$chat" "$text" "$keyboard"
|
send_keyboard "$chat" "$text" "$keyboard"
|
||||||
@ -137,7 +136,7 @@ answer_inline_query() {
|
|||||||
"contact")
|
"contact")
|
||||||
InlineQueryResult='[{"type":"'$2'","id":"$RANDOM","phone_number":"'$3'","first_name":"'$4'"}]'
|
InlineQueryResult='[{"type":"'$2'","id":"$RANDOM","phone_number":"'$3'","first_name":"'$4'"}]'
|
||||||
;;
|
;;
|
||||||
|
|
||||||
# Cached media stored in Telegram server
|
# Cached media stored in Telegram server
|
||||||
|
|
||||||
"cached_photo")
|
"cached_photo")
|
||||||
@ -164,11 +163,11 @@ answer_inline_query() {
|
|||||||
"cached_audio")
|
"cached_audio")
|
||||||
InlineQueryResult='[{"type":"audio","id":"$RANDOM","audio_file_id":"'$3'"}]'
|
InlineQueryResult='[{"type":"audio","id":"$RANDOM","audio_file_id":"'$3'"}]'
|
||||||
;;
|
;;
|
||||||
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
res=$(curl -s "$INLINE_QUERY" -F "inline_query_id=$1" -F "results=$InlineQueryResult")
|
res=$(curl -s "$INLINE_QUERY" -F "inline_query_id=$1" -F "results=$InlineQueryResult")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send_keyboard() {
|
send_keyboard() {
|
||||||
@ -185,7 +184,7 @@ send_keyboard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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/JSON.sh -s | egrep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
send_file() {
|
send_file() {
|
||||||
@ -194,7 +193,7 @@ send_file() {
|
|||||||
local file=$2
|
local file=$2
|
||||||
echo "$file" | grep -qE $FILE_REGEX || return
|
echo "$file" | grep -qE $FILE_REGEX || return
|
||||||
local ext="${file##*.}"
|
local ext="${file##*.}"
|
||||||
case $ext in
|
case $ext in
|
||||||
"mp3")
|
"mp3")
|
||||||
CUR_URL=$AUDIO_URL
|
CUR_URL=$AUDIO_URL
|
||||||
WHAT=audio
|
WHAT=audio
|
||||||
@ -234,7 +233,7 @@ send_file() {
|
|||||||
# 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 location
|
# 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 location
|
||||||
|
|
||||||
send_action() {
|
send_action() {
|
||||||
[ "$2" = "" ] && return
|
[ "$2" = "" ] && return
|
||||||
res=$(curl -s "$ACTION_URL" -F "chat_id=$1" -F "action=$2")
|
res=$(curl -s "$ACTION_URL" -F "chat_id=$1" -F "action=$2")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +251,7 @@ send_venue() {
|
|||||||
|
|
||||||
forward() {
|
forward() {
|
||||||
[ "$3" = "" ] && return
|
[ "$3" = "" ] && return
|
||||||
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() {
|
||||||
@ -274,7 +273,7 @@ inproc() {
|
|||||||
process_client() {
|
process_client() {
|
||||||
# Message
|
# Message
|
||||||
MESSAGE=$(echo "$res" | egrep '\["result",0,"message","text"\]' | cut -f 2 | cut -d '"' -f 2)
|
MESSAGE=$(echo "$res" | egrep '\["result",0,"message","text"\]' | cut -f 2 | cut -d '"' -f 2)
|
||||||
|
|
||||||
# User
|
# User
|
||||||
USER[ID]=$(echo "$res" | egrep '\["result",0,"message","chat","id"\]' | cut -f 2)
|
USER[ID]=$(echo "$res" | egrep '\["result",0,"message","chat","id"\]' | cut -f 2)
|
||||||
USER[FIRST_NAME]=$(echo "$res" | egrep '\["result",0,"message","chat","first_name"\]' | cut -f 2 | cut -d '"' -f 2)
|
USER[FIRST_NAME]=$(echo "$res" | egrep '\["result",0,"message","chat","first_name"\]' | cut -f 2 | cut -d '"' -f 2)
|
||||||
@ -308,11 +307,11 @@ process_client() {
|
|||||||
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[*]} &>/dev/null)"
|
NAME="$(basename ${URLS[*]} &>/dev/null)"
|
||||||
|
|
||||||
# Tmux
|
# Tmux
|
||||||
copname="$ME"_"${USER[ID]}"
|
copname="$ME"_"${USER[ID]}"
|
||||||
|
|
||||||
source commands.sh
|
source commands.sh
|
||||||
|
|
||||||
tmpcount="COUNT${USER[ID]}"
|
tmpcount="COUNT${USER[ID]}"
|
||||||
cat count | grep -q "$tmpcount" || echo "$tmpcount">>count
|
cat count | grep -q "$tmpcount" || echo "$tmpcount">>count
|
||||||
# To get user count execute bash bashbot.sh count
|
# To get user count execute bash bashbot.sh count
|
||||||
@ -321,7 +320,7 @@ process_client() {
|
|||||||
# source the script with source as param to use functions in other scripts
|
# source the script with source as param to use functions in other scripts
|
||||||
while [ "$1" == "startbot" ]; do {
|
while [ "$1" == "startbot" ]; do {
|
||||||
|
|
||||||
res=$(curl -s $UPD_URL$OFFSET | ./JSON.sh -s)
|
res=$(curl -s $UPD_URL$OFFSET | ./JSON.sh/JSON.sh -s)
|
||||||
|
|
||||||
# Offset
|
# Offset
|
||||||
OFFSET=$(echo "$res" | egrep '\["result",0,"update_id"\]' | cut -f 2)
|
OFFSET=$(echo "$res" | egrep '\["result",0,"update_id"\]' | cut -f 2)
|
||||||
@ -361,6 +360,12 @@ case "$1" in
|
|||||||
echo "Bot was killed successfully. "
|
echo "Bot was killed successfully. "
|
||||||
;;
|
;;
|
||||||
"help")
|
"help")
|
||||||
cat README.md
|
less README.md
|
||||||
|
;;
|
||||||
|
"attach")
|
||||||
|
tmux attach -t $ME
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Available arguments: outproc, count, broadcast, start, kill, help, attach"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
34
commands.sh
34
commands.sh
@ -1,10 +1,13 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Edit your commands in this file.
|
# Edit your commands in this file.
|
||||||
|
|
||||||
|
# This file is public domain in the USA and all free countries.
|
||||||
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
|
|
||||||
if [ "$1" = "source" ];then
|
if [ "$1" = "source" ];then
|
||||||
# Edit the token in here
|
# Edit the token in here
|
||||||
TOKEN='tokenhere'
|
TOKEN='tokenhere'
|
||||||
# Set INLINE to 1 in order to receive inline queries.
|
# Set INLINE to 1 in order to receive inline queries.
|
||||||
# To enable this option in your bot, send the /setinline command to @BotFather.
|
# To enable this option in your bot, send the /setinline command to @BotFather.
|
||||||
INLINE=0
|
INLINE=0
|
||||||
# Set to .* to allow sending files from all locations
|
# Set to .* to allow sending files from all locations
|
||||||
@ -17,8 +20,8 @@ else
|
|||||||
rm "$NAME"
|
rm "$NAME"
|
||||||
}
|
}
|
||||||
[ ! -z ${LOCATION[*]} ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}"
|
[ ! -z ${LOCATION[*]} ] && send_location "${USER[ID]}" "${LOCATION[LATITUDE]}" "${LOCATION[LONGITUDE]}"
|
||||||
|
|
||||||
# Inline
|
# Inline
|
||||||
if [ $INLINE == 1 ]; then
|
if [ $INLINE == 1 ]; then
|
||||||
# inline query data
|
# inline query data
|
||||||
iUSER[FIRST_NAME]=$(echo "$res" | sed 's/^.*\(first_name.*\)/\1/g' | cut -d '"' -f3 | tail -1)
|
iUSER[FIRST_NAME]=$(echo "$res" | sed 's/^.*\(first_name.*\)/\1/g' | cut -d '"' -f3 | tail -1)
|
||||||
@ -26,16 +29,16 @@ else
|
|||||||
iUSER[USERNAME]=$(echo "$res" | sed 's/^.*\(username.*\)/\1/g' | cut -d '"' -f3 | tail -1)
|
iUSER[USERNAME]=$(echo "$res" | sed 's/^.*\(username.*\)/\1/g' | cut -d '"' -f3 | tail -1)
|
||||||
iQUERY_ID=$(echo "$res" | sed 's/^.*\(inline_query.*\)/\1/g' | cut -d '"' -f5 | tail -1)
|
iQUERY_ID=$(echo "$res" | sed 's/^.*\(inline_query.*\)/\1/g' | cut -d '"' -f5 | tail -1)
|
||||||
iQUERY_MSG=$(echo "$res" | sed 's/^.*\(inline_query.*\)/\1/g' | cut -d '"' -f5 | tail -6 | head -1)
|
iQUERY_MSG=$(echo "$res" | sed 's/^.*\(inline_query.*\)/\1/g' | cut -d '"' -f5 | tail -6 | head -1)
|
||||||
|
|
||||||
# Inline examples
|
# Inline examples
|
||||||
if [[ $iQUERY_MSG == photo ]]; then
|
if [[ $iQUERY_MSG == photo ]]; then
|
||||||
answer_inline_query "$iQUERY_ID" "photo" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg"
|
answer_inline_query "$iQUERY_ID" "photo" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $iQUERY_MSG == sticker ]]; then
|
if [[ $iQUERY_MSG == sticker ]]; then
|
||||||
answer_inline_query "$iQUERY_ID" "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC"
|
answer_inline_query "$iQUERY_ID" "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $iQUERY_MSG == gif ]]; then
|
if [[ $iQUERY_MSG == gif ]]; then
|
||||||
answer_inline_query "$iQUERY_ID" "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC"
|
answer_inline_query "$iQUERY_ID" "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC"
|
||||||
fi
|
fi
|
||||||
@ -53,16 +56,15 @@ else
|
|||||||
;;
|
;;
|
||||||
'/start')
|
'/start')
|
||||||
send_message "${USER[ID]}" "This is bashbot, the Telegram bot written entirely in bash.
|
send_message "${USER[ID]}" "This is bashbot, the Telegram bot written entirely in bash.
|
||||||
Features background tasks and interactive chats.
|
It features background tasks and interactive chats, and can serve as an interface for CLI programs.
|
||||||
Can serve as an interface for cli programs.
|
It currently can send, recieve and forward messages, custom keyboards, photos, audio, voice, documents, locations and video files.
|
||||||
Currently can send, recieve and forward messages, custom keyboards, photos, audio, voice, documents, locations and video files.
|
|
||||||
Available commands:
|
Available commands:
|
||||||
/start: Start bot and get this message.
|
• /start: Start bot and get this message.
|
||||||
/info: Get shorter info message about this bot.
|
• /info: Get shorter info message about this bot.
|
||||||
/question: Start interactive chat.
|
• /question: Start interactive chat.
|
||||||
/cancel: Cancel any currently running interactive chats.
|
• /cancel: Cancel any currently running interactive chats.
|
||||||
Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (@danogentili)
|
Written by Drew (@topkecleon) and Daniil Gentili (@danogentili).
|
||||||
Contribute to the project: https://github.com/topkecleon/telegram-bot-bash
|
http://github.com/topkecleon/telegram-bot-bash
|
||||||
"
|
"
|
||||||
;;
|
;;
|
||||||
'/cancel')
|
'/cancel')
|
||||||
@ -72,4 +74,4 @@ Contribute to the project: https://github.com/topkecleon/telegram-bot-bash
|
|||||||
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 "${USER[ID]}" "$MESSAGE" "safe";fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
3
question
3
question
@ -1,5 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# This file is public domain in the USA and all free countries.
|
||||||
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
|
|
||||||
echo "Why hello there.
|
echo "Why hello there.
|
||||||
Would you like some tea (y/n)?"
|
Would you like some tea (y/n)?"
|
||||||
read answer
|
read answer
|
||||||
|
Loading…
Reference in New Issue
Block a user