get/save botid, new function bot_is_admin

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-12-03 14:07:39 +01:00
parent d30a700b33
commit 1b8a1d4253
6 changed files with 44 additions and 14 deletions

View File

@ -1,12 +1,11 @@
#!/bin/sh #!/bin/sh
# description: Start or stop telegram-bash-bot # description: Start or stop telegram-bash-bot
# #
# example service script to run bashbot in background # example service script to run bashbot in background as specified user
# may or may not work on your system
# #
# tested on: ubuntu, opensuse # tested on: ubuntu, opensuse, debian
# #
#### $$VERSION$$ v1.2-0-gc50499c #### $$VERSION$$ v1.2-1-gd30a700
# shellcheck disable=SC2009 # shellcheck disable=SC2009
# shellcheck disable=SC2181 # shellcheck disable=SC2181

View File

@ -11,7 +11,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v1.2-0-gc50499c #### $$VERSION$$ v1.2-1-gd30a700
# #
# Exit Codes: # Exit Codes:
# - 0 success (hopefully) # - 0 success (hopefully)
@ -607,7 +607,15 @@ title2Json(){
# get bot name # get bot name
getBotName() { getBotName() {
getJson "$ME_URL" | "${JSONSHFILE}" -b -n 2>/dev/null | JsonGetString '"result","username"' local response
declare -A BOTARRAY
response="$(getJson "$ME_URL" | "${JSONSHFILE}" -b -n 2>/dev/null)"
Json2Array 'BOTARRAY' <<<"${response}"
[[ -z "${response}" || -z "${BOTARRAY["result","username"]}" ]] && return 1
# save botname and id
setConfigKey "botname" "${BOTARRAY["result","username"]}"
setConfigKey "botid" "${BOTARRAY["result","id"]}"
echo "${BOTARRAY["result","username"]}"
} }
# pure bash implementation, done by KayM (@gnadelwartz) # pure bash implementation, done by KayM (@gnadelwartz)

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#### $$VERSION$$ v1.2-0-gc50499c #### $$VERSION$$ v1.2-1-gd30a700
############ ############
# NOTE: you MUST run install-hooks.sh again when updating this file! # NOTE: you MUST run install-hooks.sh again when updating this file!
@ -30,7 +30,7 @@ fi
# run shellcheck before commit # run shellcheck before commit
set +f set +f
FILES="$(find ./* -name '*.sh' | grep -v 'DIST\/' | grep -v 'STANDALONE\/')" FILES="$(find ./* -name '*.sh' | grep -v -e 'DIST\/' -e 'STANDALONE\/' -e 'JSON.sh')"
set -f set -f
FILES="${FILES} $(sed '/^#/d' <"dev/shellcheck.files")" FILES="${FILES} $(sed '/^#/d' <"dev/shellcheck.files")"
if [ "$FILES" != "" ]; then if [ "$FILES" != "" ]; then

View File

@ -244,6 +244,19 @@ fi
The following functions are bashbot only and not part of the Telegram API. The following functions are bashbot only and not part of the Telegram API.
##### bot_is_admin
Return true (0) if bot is admin or creator of given chat.
*usage:* bot_is_admin "${CHAT[ID]}"
*example:*
```bash
if bot_is_admin "${CHAT[ID]}"; then
send_markdown_message "${CHAT[ID]}" "*I'm admin...*"
fi
```
##### user_is_botadmin ##### user_is_botadmin
Return true (0) if user is admin of bot, user id if botadmin is read from file './botadmin'. Return true (0) if user is admin of bot, user id if botadmin is read from file './botadmin'.
@ -253,7 +266,7 @@ Return true (0) if user is admin of bot, user id if botadmin is read from file '
*example:* *example:*
```bash ```bash
_is_botadmin && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*." user_is_botadmin "${CHAT[ID]}" && send_markdown_message "${CHAT[ID]}" "You are *BOTADMIN*."
``` ```
##### user_is_creator ##### user_is_creator
@ -272,7 +285,7 @@ Return true (0) if user is admin or creator of given chat.
*example:* *example:*
```bash ```bash
if _is_admin ; then if user_is_admin "${CHAT[ID]}" ; then
send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*" send_markdown_message "${CHAT[ID]}" "*LEAVING CHAT...*"
leave_chat "${CHAT[ID]}" leave_chat "${CHAT[ID]}"
fi fi
@ -1049,5 +1062,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca
#### [Prev Best Practice](5_practice.md) #### [Prev Best Practice](5_practice.md)
#### [Next Notes for Developers](7_develop.md) #### [Next Notes for Developers](7_develop.md)
#### $$VERSION$$ v1.2-0-gc50499c #### $$VERSION$$ v1.2-1-gd30a700

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v1.2-0-gc50499c #### $$VERSION$$ v1.2-1-gd30a700
# will be automatically sourced from bashbot # will be automatically sourced from bashbot
@ -18,6 +18,7 @@ UNBAN_URL=$URL'/unbanChatMember'
GETMEMBER_URL=$URL'/getChatMember' GETMEMBER_URL=$URL'/getChatMember'
# usage: status="$(get_chat_member_status "chat" "user")" # usage: status="$(get_chat_member_status "chat" "user")"
# $1 chat # $2 user
get_chat_member_status() { get_chat_member_status() {
sendJson "$1" '"user_id":'"$2"'' "$GETMEMBER_URL" sendJson "$1" '"user_id":'"$2"'' "$GETMEMBER_URL"
# shellcheck disable=SC2154 # shellcheck disable=SC2154
@ -42,6 +43,13 @@ user_is_creator() {
return 1 return 1
} }
# $1 chat
bot_is_admin() {
[ "${2:-+}" ] && return 0
user_is_admin "$2" "$(getConfigKey "botid")"
}
# $1 chat # $2 user
user_is_admin() { user_is_admin() {
[ "${1:--}" == "${2:-+}" ] && return 0 [ "${1:--}" == "${2:-+}" ] && return 0
user_is_botadmin "$2" && return 0 user_is_botadmin "$2" && return 0
@ -50,6 +58,7 @@ user_is_admin() {
return 1 return 1
} }
# $1 user
user_is_botadmin() { user_is_botadmin() {
local admin; admin="$(getConfigKey "botadmin")"; [ -z "${admin}" ] && return 1 local admin; admin="$(getConfigKey "botadmin")"; [ -z "${admin}" ] && return 1
[[ "${admin}" == "${1}" || "${admin}" == "${2}" ]] && return 0 [[ "${admin}" == "${1}" || "${admin}" == "${2}" ]] && return 0
@ -58,6 +67,7 @@ user_is_botadmin() {
return 1 return 1
} }
# $1 user # $2 key # $3 chat
user_is_allowed() { user_is_allowed() {
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1
# user can do everything # user can do everything

View File

@ -9,7 +9,7 @@
# #### mycommands.clean # #### mycommands.clean
# #
# shellcheck disable=SC1117 # shellcheck disable=SC1117
#### $$VERSION$$ v1.2-0-gc50499c #### $$VERSION$$ v1.2-1-gd30a700
# #
# uncomment the following lines to overwrite info and help messages # uncomment the following lines to overwrite info and help messages
@ -149,7 +149,7 @@ else
MESSAGE="${MESSAGE#/* }" MESSAGE="${MESSAGE#/* }"
;; ;;
'/_new_chat_member'*) '/_new_chat_member'*)
if [[ -n "${WELCOME_NEWMEMBER}" && "${NEWMEMBER[ISBOT]}" != "true" ]]; then if [[ -n "${WELCOME_NEWMEMBER}" && "${NEWMEMBER[ISBOT]}" != "true" ]] && bot_is_admin "${CHAT[ID]}"; then
send_normal_message "${CHAT[ID]}"\ send_normal_message "${CHAT[ID]}"\
"${WELCOME_MSG} ${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]} (@${NEWMEMBER[USERNAME]})" "${WELCOME_MSG} ${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]} (@${NEWMEMBER[USERNAME]})"
MYSENTID="${BOTSENT[ID]}" MYSENTID="${BOTSENT[ID]}"