mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-22 15:35:09 +00:00
Merge pull request #71 from topkecleon/develop
Bashbot Version 0.52 - user access control
This commit is contained in:
commit
910016b10a
@ -8,7 +8,7 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M (@gnadel
|
||||
|
||||
Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.
|
||||
|
||||
[Download from github](https://github.com/topkecleon/telegram-bot-bash/releases)
|
||||
[Download latest release from github](https://github.com/topkecleon/telegram-bot-bash/releases)
|
||||
|
||||
Released to the public domain wherever applicable.
|
||||
Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/).
|
||||
@ -25,7 +25,8 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
|
||||
```
|
||||
3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather.
|
||||
|
||||
|
||||
## Update bashbot
|
||||
[Download latest update zip from github](https://github.com/topkecleon/telegram-bot-bash/releases) and copy all files to bashbot dir. run ```sudo ./bashbot.sh init```.
|
||||
|
||||
## Getting started
|
||||
- [Create your first telegram bot](doc/1_firstbot.md)
|
||||
@ -35,6 +36,7 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
|
||||
- Send Messages
|
||||
- Send files, location etc.
|
||||
- [Advanced Features](doc/3_advanced.md)
|
||||
- Access Control
|
||||
- Interactive Chats
|
||||
- Background Jobs
|
||||
- Inline queries
|
||||
@ -73,4 +75,4 @@ No - its not less (in)secure as any other Bot written in any other language. But
|
||||
|
||||
If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
14
README.txt
14
README.txt
@ -11,8 +11,8 @@ Written by Drew (@topkecleon), Daniil Gentili (@danogentili), and Kay M
|
||||
|
||||
Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.
|
||||
|
||||
https://github.com/topkecleon/telegram-bot-bash/releases[Download from
|
||||
github]
|
||||
https://github.com/topkecleon/telegram-bot-bash/releases[Download latest
|
||||
release from github]
|
||||
|
||||
Released to the public domain wherever applicable. Elsewhere, consider
|
||||
it released under the http://www.wtfpl.net/txt/copying/[WTFPLv2].
|
||||
@ -34,6 +34,13 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
|
||||
follow the instructions. At this stage you are asked for your Bots token
|
||||
given by botfather.
|
||||
|
||||
Update bashbot
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
https://github.com/topkecleon/telegram-bot-bash/releases[Download latest
|
||||
update zip from github] and copy all files to bashbot dir. run
|
||||
`sudo ./bashbot.sh init`.
|
||||
|
||||
Getting started
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
@ -44,6 +51,7 @@ Getting started
|
||||
* Send Messages
|
||||
* Send files, location etc.
|
||||
* link:doc/3_advanced.md[Advanced Features]
|
||||
* Access Control
|
||||
* Interactive Chats
|
||||
* Background Jobs
|
||||
* Inline queries
|
||||
@ -111,5 +119,5 @@ That's it!
|
||||
If you feel that there's something missing or if you found a bug, feel
|
||||
free to submit a pull request!
|
||||
|
||||
latexmath:[\[VERSION\]] v0.51-0-g0356270
|
||||
latexmath:[\[VERSION\]] v0.52-0-gdb7b19f
|
||||
++++++++++++++++++++++++++++++++++++++++
|
||||
|
@ -7,7 +7,7 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
|
||||
SHELL=/bin/sh
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
# description: Start or stop telegram-bash-bot
|
||||
#
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
# shellcheck disable=SC2009
|
||||
# shellcheck disable=SC2181
|
||||
|
||||
@ -17,14 +17,14 @@
|
||||
|
||||
# save default values
|
||||
TERM="" # disable bashbot clear and color output
|
||||
runas="nobody" # not recommended!
|
||||
runas="nobody"
|
||||
runcmd="echo Dry run:" # not actived until you edit lines below
|
||||
|
||||
#######################
|
||||
# Configuration Section
|
||||
|
||||
# edit the next line to fit the user you want to run bashbot, e.g. nobody:
|
||||
runas="nobody"
|
||||
# runas="nobody"
|
||||
|
||||
# uncomment one of the following lines to fit your system
|
||||
# runcmd="su $runas -s /bin/bash -c " # runasuser with *su*
|
||||
|
131
bashbot.sh
131
bashbot.sh
@ -10,12 +10,12 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
#
|
||||
# Exit Codes:
|
||||
# - 0 sucess (hopefully)
|
||||
# - 1 can't change to bashbot dir
|
||||
# - 2 can't write to tmp and / or count
|
||||
# - 1 can't change to dir
|
||||
# - 2 can't write to tmp, count or token
|
||||
# - 3 user not found
|
||||
# - 4 unkown command
|
||||
|
||||
@ -31,7 +31,7 @@ fi
|
||||
# get location of bashbot.sh an change to bashbot dir
|
||||
SCRIPT="./$(basename "$0")"
|
||||
SCRIPTDIR="$(dirname "$0")"
|
||||
RUNUSER="$USER" # USER is overwritten as array, $USER may not work later on...
|
||||
RUNUSER="${USER}" # USER is overwritten by bashbot array, $USER may not work later on...
|
||||
|
||||
if ! cd "${SCRIPTDIR}" ; then
|
||||
echo -e "${RED}ERROR: Can't change to ${SCRIPTDIR} ...${NC}"
|
||||
@ -39,7 +39,7 @@ if ! cd "${SCRIPTDIR}" ; then
|
||||
fi
|
||||
|
||||
if [ ! -w "." ]; then
|
||||
echo -e "${ORANGE}WARNING: $SCRIPTDIR is not writeable!${NC}"
|
||||
echo -e "${ORANGE}WARNING: ${SCRIPTDIR} is not writeable!${NC}"
|
||||
ls -ld .
|
||||
fi
|
||||
|
||||
@ -49,31 +49,58 @@ if [ ! -f "JSON.sh/JSON.sh" ]; then
|
||||
echo "JSON.sh has been downloaded. Proceeding."
|
||||
fi
|
||||
|
||||
if [ ! -f "token" ]; then
|
||||
$CLEAR
|
||||
TOKEN="./token"
|
||||
if [ ! -f "${TOKEN}" ]; then
|
||||
if [ "${CLEAR}" == "" ]; then
|
||||
echo "Running headless, run ${SCRIPT} init first!"
|
||||
exit 2
|
||||
else
|
||||
${CLEAR}
|
||||
echo -e "${RED}TOKEN MISSING.${NC}"
|
||||
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE${NC}"
|
||||
read -r token
|
||||
echo "$token" >> "token"
|
||||
echo "${token}" > "${TOKEN}"
|
||||
fi
|
||||
fi
|
||||
|
||||
BOTADMIN="./botadmin"
|
||||
if [ ! -f "${BOTADMIN}" ]; then
|
||||
if [ "${CLEAR}" == "" ]; then
|
||||
echo "Running headless, set botadmin to AUTO MODE!"
|
||||
echo "?" > "${BOTADMIN}"
|
||||
else
|
||||
${CLEAR}
|
||||
echo -e "${RED}BOTADMIN MISSING.${NC}"
|
||||
echo -e "${ORANGE}PLEASE WRITE YOUR TELEGRAM ID HERE OR ENTER '?'${NC}"
|
||||
echo -e "${ORANGE}TO MAKE FIRST USER TYPING '/start' TO BOTADMIN${NC}"
|
||||
read -r token
|
||||
echo "${token}" > "${BOTADMIN}"
|
||||
fi
|
||||
fi
|
||||
|
||||
BOTACL="./botacl"
|
||||
if [ ! -f "${BOTACL}" ]; then
|
||||
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}"
|
||||
touch "${BOTACL}"
|
||||
fi
|
||||
|
||||
TMPDIR="./tmp-bot-bash"
|
||||
if [ ! -d "$TMPDIR" ]; then
|
||||
mkdir "$TMPDIR"
|
||||
elif [ ! -w "$TMPDIR" ]; then
|
||||
$CLEAR
|
||||
echo -e "${RED}ERROR: Can't write to $TMPDIR!.${NC}"
|
||||
ls -ld "$TMPDIR"
|
||||
if [ ! -d "${TMPDIR}" ]; then
|
||||
mkdir "${TMPDIR}"
|
||||
elif [ ! -w "${TMPDIR}" ]; then
|
||||
${CLEAR}
|
||||
echo -e "${RED}ERROR: Can't write to ${TMPDIR}!.${NC}"
|
||||
ls -ld "${TMPDIR}"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
COUNT="./count"
|
||||
if [ ! -f "$COUNT" ]; then
|
||||
touch "$COUNT"
|
||||
elif [ ! -w "$COUNT" ]; then
|
||||
$CLEAR
|
||||
echo -e "${RED}ERROR: Can't write to $COUNT!.${NC}"
|
||||
ls -l "$COUNT"
|
||||
if [ ! -f "${COUNT}" ]; then
|
||||
touch "${COUNT}"
|
||||
elif [ ! -w "${COUNT}" ]; then
|
||||
${CLEAR}
|
||||
echo -e "${RED}ERROR: Can't write to ${COUNT}!.${NC}"
|
||||
ls -l "${COUNT}"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
@ -99,7 +126,8 @@ FORWARD_URL=$URL'/forwardMessage'
|
||||
INLINE_QUERY=$URL'/answerInlineQuery'
|
||||
ME_URL=$URL'/getMe'
|
||||
DELETE_URL=$URL'/deleteMessage'
|
||||
ME=$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)
|
||||
GETMEMBER_URL=$URL'/getChatMember'
|
||||
ME="$(curl -s "$ME_URL" | ./JSON.sh/JSON.sh -s | grep '\["result","username"\]' | cut -f 2 | cut -d '"' -f 2)"
|
||||
|
||||
|
||||
FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/'
|
||||
@ -120,7 +148,8 @@ send_message() {
|
||||
text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')"
|
||||
arg="$3"
|
||||
[ "$arg" != "safe" ] && {
|
||||
text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts
|
||||
#text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts
|
||||
text="${text// mynewlinestartshere /$'\r\n'}"
|
||||
no_keyboard="$(echo "$2" | sed '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')"
|
||||
|
||||
keyboard="$(echo "$2" | sed '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g;s/ myfilelocationstartshere.*//g;s/ mylatstartshere.*//g;s/ mylongstartshere.*//g;s/ mytitlestartshere.*//g;s/ myaddressstartshere.*//g;s/ mykeyboardendshere.*//g')"
|
||||
@ -205,6 +234,11 @@ delete_message() {
|
||||
res="$(curl -s "$DELETE_URL" -F "chat_id=$1" -F "message_id=$2")"
|
||||
}
|
||||
|
||||
# usage: status="$(get_chat_member_status "chat" "user")"
|
||||
get_chat_member_status() {
|
||||
curl -s "$GETMEMBER_URL" -F "chat_id=$1" -F "user_id=$2" | ./JSON.sh/JSON.sh -s | sed -n -e '/\["result","status"\]/ s/.*\][ \t]"\(.*\)"$/\1/p'
|
||||
}
|
||||
|
||||
kick_chat_member() {
|
||||
res="$(curl -s "$KICK_URL" -F "chat_id=$1" -F "user_id=$2")"
|
||||
}
|
||||
@ -217,6 +251,35 @@ leave_chat() {
|
||||
res="$(curl -s "$LEAVE_URL" -F "chat_id=$1")"
|
||||
}
|
||||
|
||||
user_is_creator() {
|
||||
if [ "${1:--}" == "${2:-+}" ] || [ "$(get_chat_member_status "$1" "$2")" == "creator" ]; then return 0; fi
|
||||
return 1
|
||||
}
|
||||
|
||||
user_is_admin() {
|
||||
local me; me="$(get_chat_member_status "$1" "$2")"
|
||||
if [ "${me}" == "creator" ] || [ "${me}" == "administrator" ]; then return 0; fi
|
||||
return 1
|
||||
}
|
||||
|
||||
user_is_botadmin() {
|
||||
local admin; admin="$(head -n 1 "${BOTADMIN}")"
|
||||
[ "${admin}" == "${1}" ] && return 0
|
||||
[[ "${admin}" == "@*" ]] && [[ "${admin}" == "${2}" ]] && return 0
|
||||
if [ "${admin}" == "?" ]; then echo "${1:-?}" >"${BOTADMIN}"; return 0; fi
|
||||
return 1
|
||||
}
|
||||
|
||||
user_is_allowed() {
|
||||
local acl; acl="$1"
|
||||
[ "$1" == "" ] && return 1
|
||||
grep -F -xq "${acl}:*:*" <"${BOTACL}" && return 0
|
||||
[ "$2" != "" ] && acl="${acl}:$2"
|
||||
grep -F -xq "${acl}:*" <"${BOTACL}" && return 0
|
||||
[ "$3" != "" ] && acl="${acl}:$3"
|
||||
grep -F -xq "${acl}" <"${BOTACL}"
|
||||
}
|
||||
|
||||
answer_inline_query() {
|
||||
case "$2" in
|
||||
"article")
|
||||
@ -549,7 +612,7 @@ case "$1" in
|
||||
while read -r f; do send_message "${f//COUNT}" "$*"; $sleep; done <"${COUNT}"
|
||||
;;
|
||||
"start")
|
||||
$CLEAR
|
||||
${CLEAR}
|
||||
tmux kill-session -t "$ME" &>/dev/null
|
||||
tmux new-session -d -s "$ME" "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}"
|
||||
echo "Tmux session name $ME" || echo -e "${RED}An error occurred while starting the bot. ${NC}"
|
||||
@ -570,20 +633,20 @@ case "$1" in
|
||||
chown -R "$TOUSER" . ./*
|
||||
chmod 711 .
|
||||
chmod -R a-w ./*
|
||||
chmod -R u+w "$COUNT" "$TMPDIR" ./*.log 2>/dev/null
|
||||
chmod -R o-r,o-w "$COUNT" "$TMPDIR" token 2>/dev/null
|
||||
chmod -R u+w "${COUNT}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null
|
||||
chmod -R o-r,o-w "${COUNT}" "${TMPDIR}" "${TOKEN}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
|
||||
ls -la
|
||||
exit
|
||||
fi
|
||||
;;
|
||||
"background" | "resumeback")
|
||||
$CLEAR
|
||||
${CLEAR}
|
||||
echo -e "${GREEN}Restart background processes ...${NC}"
|
||||
for FILE in "${TMPDIR:-.}/"*-back.cmd; do
|
||||
if [ "$FILE" == "${TMPDIR:-.}/*-back.cmd" ]; then
|
||||
if [ "${FILE}" == "${TMPDIR:-.}/*-back.cmd" ]; then
|
||||
echo -e "${RED}No background processes to start.${NC}"; break
|
||||
else
|
||||
RESTART="$(cat "$FILE")"
|
||||
RESTART="$(< "${FILE}")"
|
||||
CHAT[ID]="${RESTART%%:*}"
|
||||
JOB="${RESTART#*:}"
|
||||
PROG="${JOB#*:}"
|
||||
@ -598,29 +661,29 @@ case "$1" in
|
||||
done
|
||||
;;
|
||||
"kill")
|
||||
$CLEAR
|
||||
${CLEAR}
|
||||
tmux kill-session -t "$ME" &>/dev/null
|
||||
send_markdown_message "${CHAT[ID]}" "*Bot stopped*"
|
||||
echo -e "${GREEN}OK. Bot stopped successfully.${NC}"
|
||||
;;
|
||||
"killback" | "suspendback")
|
||||
$CLEAR
|
||||
${CLEAR}
|
||||
echo -e "${GREEN}Stopping background processes ...${NC}"
|
||||
for FILE in "${TMPDIR:-.}/"*-back.cmd; do
|
||||
if [ "$FILE" == "${TMPDIR:-.}/*-back.cmd" ]; then
|
||||
if [ "${FILE}" == "${TMPDIR:-.}/*-back.cmd" ]; then
|
||||
echo -e "${RED}No background processes.${NC}"; break
|
||||
else
|
||||
REMOVE="$(cat "$FILE")"
|
||||
REMOVE="$(< "${FILE}")"
|
||||
JOB="${REMOVE#*:}"
|
||||
fifo="back-${JOB%:*}-${ME}_${REMOVE%%:*}"
|
||||
echo "killbackground ${fifo}"
|
||||
[ "$1" == "killback" ] && rm -f "$FILE" # remove job
|
||||
[ "$1" == "killback" ] && rm -f "${FILE}" # remove job
|
||||
( tmux kill-session -t "${fifo}"; tmux kill-session -t "sendprocess_${fifo}"; rm -f -r "${TMPDIR:-.}/${fifo}") 2>/dev/null
|
||||
fi
|
||||
done
|
||||
;;
|
||||
"help")
|
||||
$CLEAR
|
||||
${CLEAR}
|
||||
less "README.txt"
|
||||
exit
|
||||
;;
|
||||
|
66
commands.sh
66
commands.sh
@ -4,10 +4,11 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-1-gdb7b19f
|
||||
#
|
||||
# shellcheck disable=SC2154
|
||||
# shellcheck disable=SC2034
|
||||
SC2034="$CONTACT" # mute CONTACT not used ;-)
|
||||
|
||||
# adjust your language setting here, e.g.when run from other user or cron.
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
@ -18,6 +19,40 @@ export 'LANGUAGE=C.UTF-8'
|
||||
unset IFS
|
||||
# set -f # if you are paranoid use set -f to disable globbing
|
||||
|
||||
|
||||
# change Info anf Help to fit your needs
|
||||
bashbot_info() {
|
||||
send_markdown_message "${1}" 'This is bashbot, the Telegram bot written entirely in bash.
|
||||
It features background tasks and interactive chats, and 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.
|
||||
'
|
||||
}
|
||||
|
||||
bashbot_help() {
|
||||
send_markdown_message "${1}" '*Available commands*:
|
||||
*• /start*: _Start bot and get this message_.
|
||||
*• /info*: _Get shorter info message about this bot_.
|
||||
*• /question*: _Start interactive chat_.
|
||||
*• /cancel*: _Cancel any currently running interactive chats_.
|
||||
*• /kickme*: _You will be autokicked from the chat_.
|
||||
*• /leavechat*: _The bot will leave the group with this command _.
|
||||
Written by Drew (@topkecleon), Daniil Gentili (@danogentili) and KayM(@gnadelwartz).
|
||||
Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash)
|
||||
'
|
||||
}
|
||||
|
||||
# some handy shortcuts, e.g.:
|
||||
_is_botadmin() {
|
||||
user_is_botadmin "${USER[ID]}"
|
||||
}
|
||||
_is_admin() {
|
||||
user_is_admin "${CHAT[ID]}" "${USER[ID]}"
|
||||
}
|
||||
_is_allowed() { # $1 = resource
|
||||
user_is_allowed "${USER[ID]}" "$1" "${CHAT[ID]}"
|
||||
}
|
||||
|
||||
|
||||
if [ "$1" = "source" ];then
|
||||
# Place the token in the token file
|
||||
TOKEN="$(cat token)"
|
||||
@ -92,28 +127,23 @@ else
|
||||
################################################
|
||||
# DEFAULT commands start here, edit messages only
|
||||
'/info')
|
||||
send_markdown_message "${CHAT[ID]}" "This is bashbot, the *Telegram* bot written entirely in *bash*."
|
||||
bashbot_info "${CHAT[ID]}"
|
||||
;;
|
||||
'/start')
|
||||
send_action "${CHAT[ID]}" "typing"
|
||||
send_markdown_message "${CHAT[ID]}" "This is bashbot, the Telegram bot written entirely in bash.
|
||||
It features background tasks and interactive chats, and 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.
|
||||
*Available commands*:
|
||||
*• /start*: _Start bot and get this message_.
|
||||
*• /info*: _Get shorter info message about this bot_.
|
||||
*• /question*: _Start interactive chat_.
|
||||
*• /cancel*: _Cancel any currently running interactive chats_.
|
||||
*• /kickme*: _You will be autokicked from the chat_.
|
||||
*• /leavechat*: _The bot will leave the group with this command _.
|
||||
Written by Drew (@topkecleon) and Daniil Gentili (@danogentili).
|
||||
Get the code in my [GitHub](http://github.com/topkecleon/telegram-bot-bash)
|
||||
"
|
||||
user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
|
||||
if user_is_allowed "${USER[ID]}" "start" "${CHAT[ID]}" ; then
|
||||
bot_help "${CHAT[ID]}"
|
||||
else
|
||||
send_normal_message "${CHAT[ID]}" "You are not allowed to start Bot."
|
||||
fi
|
||||
;;
|
||||
|
||||
'/leavechat')
|
||||
send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*"
|
||||
leave_chat "${CHAT[ID]}"
|
||||
'/leavechat') # bot leave chat if user is admin in chat
|
||||
if user_is_admin "${CHAT[ID]}" "${USER[ID]}"; then
|
||||
send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*"
|
||||
leave_chat "${CHAT[ID]}"
|
||||
fi
|
||||
;;
|
||||
|
||||
'/kickme')
|
||||
|
@ -70,5 +70,5 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
|
||||
```
|
||||
3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather.
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
|
@ -147,6 +147,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video
|
||||
send_action "${CHAT[ID]}" "action"
|
||||
```
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
|
||||
|
@ -1,5 +1,61 @@
|
||||
|
||||
## Advanced Features
|
||||
### Access control
|
||||
Bashbot offers functions to check if a has Telegram capabilities like chat admin or chat creator:
|
||||
```bash
|
||||
# return true if user is admin/owner of the bot
|
||||
# -> botadmin is stored in file './botadmin'
|
||||
user_is_botadmin "user"
|
||||
|
||||
# return true if user is creator or admin of a chat
|
||||
user_is_admin "chat" "user"
|
||||
|
||||
# return true if user is creator of a chat or it's a one to one chat
|
||||
user_is_creator "chat" "user"
|
||||
|
||||
# examples:
|
||||
user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
|
||||
|
||||
user_is_admin "${CHAT[ID]}" "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *CHATADMIN*."
|
||||
|
||||
```
|
||||
In addtion you can configure individual capabilities for users in the file ```./botacl```:
|
||||
```bash
|
||||
# botacl
|
||||
# if a user is not listed here, function 'user_is_allowed' will always return false
|
||||
#
|
||||
# Format:
|
||||
# user:ressource:chat
|
||||
|
||||
# allow user 123456789 access to all resources in all chats
|
||||
123456789:*:*
|
||||
|
||||
# allow user 12131415 to start bot in all chats
|
||||
12131415:start:*
|
||||
|
||||
# allow user 987654321 only to start bot in chat 98979695
|
||||
987654321:start:98979695
|
||||
|
||||
# * are only allowed on the right hand side and not for user!
|
||||
# the following exaples are NOT valid!
|
||||
*:*:*
|
||||
*:start:*
|
||||
*:*:98979695
|
||||
```
|
||||
you have to check yourself if a user is allowed to to something by calling function ```user_is_allowed```.
|
||||
example to check if a user is allowed to start bot:
|
||||
```bash
|
||||
case "$MESSAGE" in
|
||||
'/start')
|
||||
user_is_botadmin "${USER[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
|
||||
if user_is_allowed "${USER[ID]}" "start" "${CHAT[ID]}" ; then
|
||||
bot_help "${CHAT[ID]}"
|
||||
else
|
||||
send_normal_message "${CHAT[ID]}" "You are not allowed to start Bot."
|
||||
;;
|
||||
esac
|
||||
```
|
||||
|
||||
### Interactive Chats
|
||||
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).
|
||||
@ -97,5 +153,5 @@ To send stickers through an *inline query*:
|
||||
answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker"
|
||||
```
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
|
@ -102,5 +102,5 @@ An example crontab is provided in ```bashbot.cron```.
|
||||
- if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot```
|
||||
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
## Best Practices
|
||||
|
||||
### Customizing commands.sh
|
||||
To ease Updates never change ```bashbot.sh```, all changes should be done in ```commands.sh``` .
|
||||
|
||||
To ease Updates never change ```bashbot.sh```, all changes should be done in ```commands.sh``` .
|
||||
Insert your own Bot commands in the ```case ... esac``` block in commands.sh:
|
||||
```bash
|
||||
case "$MESSAGE" in
|
||||
@ -13,20 +13,18 @@ Insert your own Bot commands in the ```case ... esac``` block in commands.sh:
|
||||
################################################
|
||||
# DEFAULT commands start here, edit messages only
|
||||
'/info')
|
||||
send_markdown_message "${CHAT[ID]}" "This is bashbot, the *Telegram* bot written entirely in *bash*."
|
||||
bashbot_info "${CHAT[ID]}"
|
||||
;;
|
||||
'/start')
|
||||
send_action "${CHAT[ID]}" "typing"
|
||||
send_markdown_message "${CHAT[ID]}" "This is bashbot, the Telegram bot written entirely in bash."
|
||||
;;
|
||||
[...]
|
||||
esac
|
||||
```
|
||||
after editing commands.sh restart Bot.
|
||||
|
||||
### Seperate Bot logic from command
|
||||
|
||||
If a Bot command needs more than 2-3 lines of code I recommend to factor it out to a bash function in a seperate file, e.g.
|
||||
```mybotcommands.inc.sh``` and source the file from bashbot.sh.
|
||||
```mybotcommands.inc.sh``` and source the file from bashbot.sh. ```bashbot_info and bashbot_help``` are examples how to use
|
||||
bash functions to make customisation easy and keep case block small. ```process_message``` is an example for a complex
|
||||
processing logic as a bash funtcion in a seperate file.
|
||||
```bash
|
||||
source mybotcommands.inc.sh
|
||||
|
||||
@ -38,16 +36,15 @@ If a Bot command needs more than 2-3 lines of code I recommend to factor it out
|
||||
################################################
|
||||
# DEFAULT commands start here, edit messages only
|
||||
'/info')
|
||||
send_markdown_message "${CHAT[ID]}" "This is bashbot, the *Telegram* bot written entirely in *bash*."
|
||||
bashbot_info "${CHAT[ID]}"
|
||||
;;
|
||||
'/start')
|
||||
send_action "${CHAT[ID]}" "typing"
|
||||
send_markdown_message "${CHAT[ID]}" "This is bashbot, the Telegram bot written entirely in bash."
|
||||
bashbot_help "${CHAT[ID]}"
|
||||
;;
|
||||
#[...]
|
||||
esac
|
||||
```
|
||||
Doing it this way keeps commands.sh small and clean, while allowing complex tasks to be done in the included function. example ```mybotcommands.inc.sh```:
|
||||
Example ```mybotcommands.inc.sh```:
|
||||
```bash
|
||||
#!/bin/bash
|
||||
#
|
||||
@ -79,10 +76,10 @@ process_message() {
|
||||
}
|
||||
|
||||
```
|
||||
Doing it this way keeps commands.sh small and clean, while allowing complex tasks to be done in the included function.
|
||||
|
||||
### Test your Bot with shellcheck
|
||||
Shellcheck is a static linter for shell scripts providing excellent tips and hints for shell coding pittfalls. You can [use it online](https://www.shellcheck.net/) or [install it on your system](https://github.com/koalaman/shellcheck#installing).
|
||||
|
||||
All bashbot scripts are linted by shellcheck.
|
||||
|
||||
Shellcheck examples:
|
||||
@ -114,5 +111,5 @@ In bashbot.sh line 490:
|
||||
```
|
||||
As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-)
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
|
2
notify
2
notify
@ -2,7 +2,7 @@
|
||||
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
# adjust your language setting here
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
|
2
question
2
question
@ -3,7 +3,7 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
|
||||
# adjust your language setting here
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
|
5
version
5
version
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
#### $$VERSION$$ v0.51-0-g0356270
|
||||
#### $$VERSION$$ v0.52-0-gdb7b19f
|
||||
# shellcheck disable=SC2016
|
||||
#
|
||||
# Easy Versioning in git:
|
||||
@ -47,5 +47,8 @@ do
|
||||
echo -n " $file"
|
||||
sed -i 's/^#### $$VERSION$$.*/#### \$\$VERSION\$\$ '"$VERSION"'/' "$file"
|
||||
done
|
||||
# try to compile README.txt
|
||||
echo -n " README.txt"
|
||||
pandoc -f markdown -t asciidoc README.md | sed '/^\[\[/d' >README.txt
|
||||
echo " done."
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user