mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-22 15:35:09 +00:00
Merge pull request #80 from topkecleon/develop
Sync develop fixes for process_message
This commit is contained in:
commit
c45610f5b1
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,7 +1,7 @@
|
||||
*~
|
||||
count
|
||||
token
|
||||
/count
|
||||
/token
|
||||
*.save
|
||||
*.log
|
||||
JSON.sh/*
|
||||
tmp-bot-bash/
|
||||
/JSON.sh/*
|
||||
/tmp-bot-bash/
|
||||
|
1
JSON.sh
1
JSON.sh
@ -1 +0,0 @@
|
||||
Subproject commit 022ec337c3225d5857856fd924cef0ab20443088
|
@ -102,4 +102,4 @@ Bashbot is not more (in)secure as any other Bot written in any other language, w
|
||||
|
||||
If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
|
||||
|
||||
#### $$VERSION$$ v0.61-0-g3b17bc2
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
@ -166,5 +166,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.61-0-g3b17bc2
|
||||
latexmath:[\[VERSION\]] v0.62-0-g5d5dbae
|
||||
++++++++++++++++++++++++++++++++++++++++
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
# description: Start or stop telegram-bash-bot
|
||||
#
|
||||
#### $$VERSION$$ v0.61-0-g3b17bc2
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
# shellcheck disable=SC2009
|
||||
# shellcheck disable=SC2181
|
||||
|
||||
|
208
bashbot.sh
208
bashbot.sh
@ -10,7 +10,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.61-0-g3b17bc2
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
#
|
||||
# Exit Codes:
|
||||
# - 0 sucess (hopefully)
|
||||
@ -34,7 +34,7 @@ SCRIPT="./$(basename "$0")"
|
||||
SCRIPTDIR="$(dirname "$0")"
|
||||
RUNUSER="${USER}" # USER is overwritten by bashbot array, $USER may not work later on...
|
||||
|
||||
if ! cd "${SCRIPTDIR}" ; then
|
||||
if [ "$1" != "source" ] && ! cd "${SCRIPTDIR}" ; then
|
||||
echo -e "${RED}ERROR: Can't change to ${SCRIPTDIR} ...${NC}"
|
||||
exit 1
|
||||
fi
|
||||
@ -44,9 +44,9 @@ if [ ! -w "." ]; then
|
||||
ls -ld .
|
||||
fi
|
||||
|
||||
TOKEN="./token"
|
||||
if [ ! -f "${TOKEN}" ]; then
|
||||
if [ "${CLEAR}" = "" ]; then
|
||||
TOKENFILE="./token"
|
||||
if [ ! -f "${TOKENFILE}" ]; then
|
||||
if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then
|
||||
echo "Running headless, run ${SCRIPT} init first!"
|
||||
exit 2
|
||||
else
|
||||
@ -54,14 +54,16 @@ if [ ! -f "${TOKEN}" ]; then
|
||||
echo -e "${RED}TOKEN MISSING.${NC}"
|
||||
echo -e "${ORANGE}PLEASE WRITE YOUR TOKEN HERE OR PRESS CTRL+C TO ABORT${NC}"
|
||||
read -r token
|
||||
echo "${token}" > "${TOKEN}"
|
||||
echo "${token}" > "${TOKENFILE}"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -f "JSON.sh/JSON.sh" ]; then
|
||||
echo "You did not clone recursively! Downloading JSON.sh..."
|
||||
git clone http://github.com/dominictarr/JSON.sh
|
||||
echo "JSON.sh has been downloaded. Proceeding."
|
||||
JSONSHFILE="JSON.sh/JSON.sh"
|
||||
if [ ! -f "${JSONSHFILE}" ]; then
|
||||
echo "Seems to be first run, Downloading ${JSONSHFILE}..."
|
||||
mkdir "JSON.sh" 2>/dev/null;
|
||||
curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh"
|
||||
chmod +x "${JSONSHFILE}"
|
||||
fi
|
||||
|
||||
BOTADMIN="./botadmin"
|
||||
@ -82,7 +84,7 @@ fi
|
||||
BOTACL="./botacl"
|
||||
if [ ! -f "${BOTACL}" ]; then
|
||||
echo -e "${ORANGE}Create empty ${BOTACL} file.${NC}"
|
||||
touch "${BOTACL}"
|
||||
echo "" >"${BOTACL}"
|
||||
fi
|
||||
|
||||
TMPDIR="./tmp-bot-bash"
|
||||
@ -95,28 +97,31 @@ elif [ ! -w "${TMPDIR}" ]; then
|
||||
exit 2
|
||||
fi
|
||||
|
||||
COUNT="./count"
|
||||
if [ ! -f "${COUNT}" ]; then
|
||||
touch "${COUNT}"
|
||||
elif [ ! -w "${COUNT}" ]; then
|
||||
COUNTFILE="./count"
|
||||
if [ ! -f "${COUNTFILE}" ]; then
|
||||
echo "" >"${COUNTFILE}"
|
||||
elif [ ! -w "${COUNTFILE}" ]; then
|
||||
${CLEAR}
|
||||
echo -e "${RED}ERROR: Can't write to ${COUNT}!.${NC}"
|
||||
ls -l "${COUNT}"
|
||||
echo -e "${RED}ERROR: Can't write to ${COUNTFILE}!.${NC}"
|
||||
ls -l "${COUNTFILE}"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
COMMANDS="./commands.sh"
|
||||
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
|
||||
${CLEAR}
|
||||
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
|
||||
exit 3
|
||||
ls -l "${COMMANDS}"
|
||||
if [ "$1" != "source" ]; then
|
||||
if [ ! -f "${COMMANDS}" ] || [ ! -r "${COMMANDS}" ]; then
|
||||
${CLEAR}
|
||||
echo -e "${RED}ERROR: ${COMMANDS} does not exist or is not readable!.${NC}"
|
||||
ls -l "${COMMANDS}"
|
||||
exit 3
|
||||
fi
|
||||
# shellcheck source=./commands.sh
|
||||
source "${COMMANDS}" "source"
|
||||
fi
|
||||
|
||||
# shellcheck source=./commands.sh
|
||||
source "${COMMANDS}" "source"
|
||||
|
||||
URL='https://api.telegram.org/bot'$TOKEN
|
||||
BOTTOKEN="$(cat "${TOKENFILE}")"
|
||||
URL='https://api.telegram.org/bot'$BOTTOKEN
|
||||
|
||||
|
||||
MSG_URL=$URL'/sendMessage'
|
||||
@ -139,7 +144,7 @@ DELETE_URL=$URL'/deleteMessage'
|
||||
GETMEMBER_URL=$URL'/getChatMember'
|
||||
|
||||
|
||||
FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/'
|
||||
FILE_URL='https://api.telegram.org/file/bot'$BOTTOKEN'/'
|
||||
UPD_URL=$URL'/getUpdates?offset='
|
||||
GET_URL=$URL'/getFile'
|
||||
OFFSET=0
|
||||
@ -244,7 +249,7 @@ delete_message() {
|
||||
|
||||
# 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'
|
||||
curl -s "$GETMEMBER_URL" -F "chat_id=$1" -F "user_id=$2" | "./${JSONSHFILE}" -s -b -n | sed -n -e '/\["result","status"\]/ s/.*\][ \t]"\(.*\)"$/\1/p'
|
||||
}
|
||||
|
||||
kick_chat_member() {
|
||||
@ -387,7 +392,8 @@ remove_keyboard() {
|
||||
}
|
||||
|
||||
get_file() {
|
||||
[ "$1" != "" ] && echo "$FILE_URL$(curl -s "$GET_URL" -F "file_id=$1" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","file_path"')"
|
||||
[ "$1" = "" ] && return
|
||||
echo "${FILE_URL}$(curl -s "${GET_URL}" -F "file_id=$1" | "./${JSONSHFILE}" -s -b -n | grep '\["result","file_path"\]' | cut -f 2 | cut -d '"' -f 2)"
|
||||
}
|
||||
|
||||
send_file() {
|
||||
@ -511,104 +517,112 @@ process_updates() {
|
||||
fi
|
||||
done
|
||||
}
|
||||
process_client() {
|
||||
process_message "$PROCESS_NUMBER"
|
||||
# Tmux
|
||||
copname="$ME"_"${CHAT[ID]}"
|
||||
source commands.sh
|
||||
tmpcount="COUNT${CHAT[ID]}"
|
||||
grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNTFILE}
|
||||
# To get user count execute bash bashbot.sh count
|
||||
}
|
||||
JsonGetString() {
|
||||
sed -n -e '/\['"$1"'\]/ s/.*\][ \t]"\(.*\)"$/\1/p'
|
||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]"\(.*\)"$/\1/p'
|
||||
}
|
||||
JsonGetLine() {
|
||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\]\][ \t]//p'
|
||||
}
|
||||
JsonGetValue() {
|
||||
sed -n -e '/\['"$1"'\]/ s/.*\][ \t]//p'
|
||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
||||
}
|
||||
process_client() {
|
||||
process_message() {
|
||||
local num="$1"
|
||||
local TMP="${TMPDIR:-.}/$RANDOM$RANDOM-MESSAGE"
|
||||
echo "$UPDATE" >"$TMP"
|
||||
# Message
|
||||
MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","text"' <"$TMP")" | sed 's#\\/#/#g')"
|
||||
MESSAGE[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","message_id"' <"$TMP" )"
|
||||
MESSAGE[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","text"' <"$TMP")" | sed 's#\\/#/#g')"
|
||||
MESSAGE[ID]="$(JsonGetValue '"result",'"${num}"',"message","message_id"' <"$TMP" )"
|
||||
|
||||
# Chat
|
||||
CHAT[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","chat","id"' <"$TMP" )"
|
||||
CHAT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","first_name"' <"$TMP")")"
|
||||
CHAT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","last_name"' <"$TMP")")"
|
||||
CHAT[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","username"' <"$TMP")")"
|
||||
CHAT[TITLE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","title"' <"$TMP")")"
|
||||
CHAT[TYPE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","type"' <"$TMP")")"
|
||||
CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","chat","all_members_are_administrators"' <"$TMP")")"
|
||||
CHAT[ID]="$(JsonGetValue '"result",'"${num}"',"message","chat","id"' <"$TMP" )"
|
||||
CHAT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","first_name"' <"$TMP")")"
|
||||
CHAT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","last_name"' <"$TMP")")"
|
||||
CHAT[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","username"' <"$TMP")")"
|
||||
CHAT[TITLE]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","title"' <"$TMP")")"
|
||||
CHAT[TYPE]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","type"' <"$TMP")")"
|
||||
CHAT[ALL_MEMBERS_ARE_ADMINISTRATORS]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","chat","all_members_are_administrators"' <"$TMP")")"
|
||||
|
||||
# User
|
||||
USER[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","from","id"' <"$TMP" )"
|
||||
USER[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","first_name"' <"$TMP")")"
|
||||
USER[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","last_name"' <"$TMP")")"
|
||||
USER[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","from","username"' <"$TMP")")"
|
||||
USER[ID]="$(JsonGetValue '"result",'"${num}"',"message","from","id"' <"$TMP" )"
|
||||
USER[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","from","first_name"' <"$TMP")")"
|
||||
USER[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","from","last_name"' <"$TMP")")"
|
||||
USER[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","from","username"' <"$TMP")")"
|
||||
|
||||
# in reply to message from
|
||||
REPLYTO[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","id"' <"$TMP" )"
|
||||
REPLYTO[UID]="$(JsonGetValue '"result",'"${num}"',"message","reply_to_message","from","id"' <"$TMP" )"
|
||||
if [ "${REPLYTO[UID]}" != "" ]; then
|
||||
REPLYTO[0]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","text"' <"$TMP")")"
|
||||
REPLYTO[ID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","reply_to_message","message_id"' <"$TMP")"
|
||||
REPLYTO[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","first_name"' <"$TMP")")"
|
||||
REPLYTO[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","last_name"' <"$TMP")")"
|
||||
REPLYTO[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","reply_to_message","from","username"' <"$TMP")")"
|
||||
REPLYTO[0]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","text"' <"$TMP")")"
|
||||
REPLYTO[ID]="$(JsonGetValue '"result",'"${num}"',"message","reply_to_message","message_id"' <"$TMP")"
|
||||
REPLYTO[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","from","first_name"' <"$TMP")")"
|
||||
REPLYTO[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","from","last_name"' <"$TMP")")"
|
||||
REPLYTO[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","reply_to_message","from","username"' <"$TMP")")"
|
||||
fi
|
||||
|
||||
# forwarded message from
|
||||
FORWARD[UID]="$(JsonGetValue '"result",'$PROCESS_NUMBER',"message","forward_from","id"' <"$TMP" )"
|
||||
FORWARD[UID]="$(JsonGetValue '"result",'"${num}"',"message","forward_from","id"' <"$TMP" )"
|
||||
if [ "${FORWARD[UID]}" != "" ]; then
|
||||
FORWARD[ID]="${MESSAGE[ID]}" # same as message ID
|
||||
FORWARD[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","first_name"' <"$TMP")")"
|
||||
FORWARD[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","last_name"' <"$TMP")")"
|
||||
FORWARD[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","forward_from","username"' <"$TMP")")"
|
||||
FORWARD[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","forward_from","first_name"' <"$TMP")")"
|
||||
FORWARD[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","forward_from","last_name"' <"$TMP")")"
|
||||
FORWARD[USERNAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","forward_from","username"' <"$TMP")")"
|
||||
fi
|
||||
|
||||
# Audio
|
||||
URLS[AUDIO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","audio","file_id"' <"$TMP")")")"
|
||||
URLS[AUDIO]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","audio","file_id"' <"$TMP")")"
|
||||
# Document
|
||||
URLS[DOCUMENT]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","document","file_id"' <"$TMP")")")"
|
||||
URLS[DOCUMENT]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","document","file_id"' <"$TMP")")"
|
||||
# Photo
|
||||
URLS[PHOTO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","photo",.*,"file_id"' <"$TMP")")")"
|
||||
URLS[PHOTO]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","photo",0,"file_id"' <"$TMP")")"
|
||||
# Sticker
|
||||
URLS[STICKER]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","sticker","file_id"' <"$TMP")")")"
|
||||
URLS[STICKER]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","sticker","file_id"' <"$TMP")")"
|
||||
# Video
|
||||
URLS[VIDEO]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","video","file_id"' <"$TMP")")")"
|
||||
URLS[VIDEO]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","video","file_id"' <"$TMP")")"
|
||||
# Voice
|
||||
URLS[VOICE]="$(get_file "$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","voice","file_id"' <"$TMP")")")"
|
||||
URLS[VOICE]="$(get_file "$(JsonGetString '"result",'"${num}"',"message","voice","file_id"' <"$TMP")")"
|
||||
|
||||
# Contact
|
||||
CONTACT[NUMBER]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","phone_number"' <"$TMP")")"
|
||||
CONTACT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","first_name"' <"$TMP")")"
|
||||
CONTACT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","last_name"' <"$TMP")")"
|
||||
CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","contact","user_id"' <"$TMP")")"
|
||||
CONTACT[NUMBER]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","phone_number"' <"$TMP")")"
|
||||
CONTACT[FIRST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","first_name"' <"$TMP")")"
|
||||
CONTACT[LAST_NAME]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","last_name"' <"$TMP")")"
|
||||
CONTACT[USER_ID]="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","contact","user_id"' <"$TMP")")"
|
||||
|
||||
# Caption
|
||||
CAPTION="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","caption"' <"$TMP")")"
|
||||
CAPTION="$(JsonDecode "$(JsonGetString '"result",'"${num}"',"message","caption"' <"$TMP")")"
|
||||
|
||||
# Location
|
||||
LOCATION[LONGITUDE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","location","longitude"' <"$TMP")")"
|
||||
LOCATION[LATITUDE]="$(JsonDecode "$(JsonGetString '"result",'$PROCESS_NUMBER',"message","location","latitude"' <"$TMP")")"
|
||||
LOCATION[LONGITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","longitude"' <"$TMP")"
|
||||
LOCATION[LATITUDE]="$(JsonGetValue '"result",'"${num}"',"message","location","latitude"' <"$TMP")"
|
||||
NAME="$(echo "${URLS[*]}" | sed 's/.*\///g')"
|
||||
rm "$TMP"
|
||||
|
||||
# Tmux
|
||||
copname="$ME"_"${CHAT[ID]}"
|
||||
|
||||
source commands.sh
|
||||
|
||||
tmpcount="COUNT${CHAT[ID]}"
|
||||
grep -q "$tmpcount" <"${COUNT}" >/dev/null 2>&1 || echo "$tmpcount">>${COUNT}
|
||||
# To get user count execute bash bashbot.sh count
|
||||
}
|
||||
# get bot name
|
||||
getBotName() {
|
||||
res="$(curl -s "$ME_URL")"
|
||||
echo "$res" | ./JSON.sh/JSON.sh -s | JsonGetString '"result","username"'
|
||||
echo "$res" | "./${JSONSHFILE}" -s -b -n | JsonGetString '"result","username"'
|
||||
}
|
||||
|
||||
ME="$(getBotName)"
|
||||
if [ "$ME" = "" ]; then
|
||||
if [ "$(cat "${TOKENFILE}")" = "bashbottestscript" ]; then
|
||||
ME="bashbottestscript"
|
||||
else
|
||||
echo -e "${RED}ERROR: Can't connect to Telegram Bot! May be your TOKEN is invalid ...${NC}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback
|
||||
if which python >/dev/null 2>&1 || which phyton2 >/dev/null 2>&1; then
|
||||
if [ "${BASHDECODE}" != "yes" ] && which python >/dev/null 2>&1 ; then
|
||||
JsonDecode() {
|
||||
printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))'
|
||||
}
|
||||
@ -632,9 +646,12 @@ else
|
||||
fi
|
||||
|
||||
# source the script with source as param to use functions in other scripts
|
||||
while [ "$1" = "startbot" ]; do {
|
||||
# do not execute if read from other scripts
|
||||
|
||||
UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./JSON.sh/JSON.sh)"
|
||||
if [ "$1" != "source" ]; then
|
||||
while [ "$1" = "startbot" ]; do {
|
||||
|
||||
UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./${JSONSHFILE})"
|
||||
|
||||
# Offset
|
||||
OFFSET="$(echo "$UPDATE" | grep '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
|
||||
@ -648,10 +665,10 @@ while [ "$1" = "startbot" ]; do {
|
||||
fi
|
||||
fi
|
||||
|
||||
}; done
|
||||
}; done
|
||||
|
||||
|
||||
case "$1" in
|
||||
case "$1" in
|
||||
"outproc")
|
||||
until [ "$line" = "imprettydarnsuredatdisisdaendofdacmd" ];do
|
||||
line=""
|
||||
@ -661,15 +678,15 @@ case "$1" in
|
||||
rm -f -r "${TMPDIR:-.}/$3"
|
||||
;;
|
||||
"count")
|
||||
echo "A total of $(wc -l <"${COUNT}") users used me."
|
||||
echo "A total of $(wc -l <"${COUNTFILE}") users used me."
|
||||
exit
|
||||
;;
|
||||
"broadcast")
|
||||
NUMCOUNT="$(wc -l <"${COUNT}")"
|
||||
NUMCOUNT="$(wc -l <"${COUNTFILE}")"
|
||||
echo "Sending the broadcast $* to $NUMCOUNT users."
|
||||
[ "$NUMCOUNT" -gt "300" ] && sleep="sleep 0.5"
|
||||
shift
|
||||
while read -r f; do send_message "${f//COUNT}" "$*"; $sleep; done <"${COUNT}"
|
||||
while read -r f; do send_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}"
|
||||
;;
|
||||
"start")
|
||||
${CLEAR}
|
||||
@ -687,14 +704,13 @@ case "$1" in
|
||||
echo -e "${RED}User \"$TOUSER\" not found!${NC}"
|
||||
exit 3
|
||||
else
|
||||
echo "Adjusting user in bashbot.rc ..."
|
||||
echo "Adjusting user \"${TOUSER}\" files and permissions ..."
|
||||
sed -i '/^[# ]*runas=/ s/runas=.*$/runas="'$TOUSER'"/' bashbot.rc
|
||||
echo "Adjusting Owner and Permissions ..."
|
||||
chown -R "$TOUSER" . ./*
|
||||
chmod 711 .
|
||||
chmod -R a-w ./*
|
||||
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
|
||||
chmod -R u+w "${COUNTFILE}" "${TMPDIR}" "${BOTADMIN}" ./*.log 2>/dev/null
|
||||
chmod -R o-r,o-w "${COUNTFILE}" "${TMPDIR}" "${TOKENFILE}" "${BOTADMIN}" "${BOTACL}" 2>/dev/null
|
||||
ls -la
|
||||
exit
|
||||
fi
|
||||
@ -751,19 +767,19 @@ case "$1" in
|
||||
tmux attach -t "$ME"
|
||||
;;
|
||||
"source")
|
||||
echo "OK"
|
||||
exit
|
||||
# this should never happen
|
||||
echo "OK"
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}${ME}: BAD REQUEST${NC}"
|
||||
echo -e "${RED}Available arguments: outproc, count, broadcast, start, suspendback, resumeback, kill, killback, help, attach${NC}"
|
||||
exit 4
|
||||
;;
|
||||
esac
|
||||
esac
|
||||
|
||||
# warn if root
|
||||
if [[ "$(id -u)" -eq "0" ]] ; then
|
||||
# warn if root
|
||||
if [[ "$(id -u)" -eq "0" ]] ; then
|
||||
echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}"
|
||||
echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}"
|
||||
fi
|
||||
|
||||
fi
|
||||
fi # end source
|
||||
|
29
commands.sh
29
commands.sh
@ -4,7 +4,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.61-0-g3b17bc2
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
#
|
||||
# shellcheck disable=SC2154
|
||||
# shellcheck disable=SC2034
|
||||
@ -20,15 +20,16 @@ unset IFS
|
||||
# set -f # if you are paranoid use set -f to disable globbing
|
||||
|
||||
|
||||
# change Info anf Help to fit your needs
|
||||
bashbot_info() {
|
||||
if [ "$1" != "source" ]; then
|
||||
# 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() {
|
||||
bashbot_help() {
|
||||
send_markdown_message "${1}" '*Available commands*:
|
||||
*• /start*: _Start bot and get this message_.
|
||||
*• /info*: _Get shorter info message about this bot_.
|
||||
@ -39,23 +40,21 @@ bashbot_help() {
|
||||
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() {
|
||||
_is_botadmin() {
|
||||
user_is_botadmin "${USER[ID]}"
|
||||
}
|
||||
_is_admin() {
|
||||
}
|
||||
_is_admin() {
|
||||
user_is_admin "${CHAT[ID]}" "${USER[ID]}"
|
||||
}
|
||||
_is_allowed() { # $1 = resource
|
||||
}
|
||||
_is_allowed() { # $1 = resource
|
||||
user_is_allowed "${USER[ID]}" "$1" "${CHAT[ID]}"
|
||||
}
|
||||
|
||||
}
|
||||
fi
|
||||
|
||||
if [ "$1" = "source" ];then
|
||||
# Place the token in the token file
|
||||
TOKEN="$(cat token)"
|
||||
# Set INLINE to 1 in order to receive inline queries.
|
||||
# To enable this option in your bot, send the /setinline command to @BotFather.
|
||||
INLINE="0"
|
||||
|
@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# magic to ensure that we're always inside the root of our application,
|
||||
# no matter from which directory we'll run script
|
||||
@ -28,8 +28,10 @@ else
|
||||
fi
|
||||
|
||||
# run shellcheck before commit
|
||||
echo "read files from dev/shellcheck.files ..."
|
||||
FILES=$(sed '/^#/d' <"dev/shellcheck.files")
|
||||
set +f
|
||||
FILES="$(find ./* -name '*.sh')"
|
||||
set -f
|
||||
FILES="${FILES} $(sed '/^#/d' <"dev/shellcheck.files")"
|
||||
if [ "$FILES" != "" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
shellcheck -x ${FILES} || exit 1
|
||||
|
@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# magic to ensure that we're always inside the root of our application,
|
||||
# no matter from which directory we'll run script
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
# this has to run once atfer git clone
|
||||
# and every time we create new hooks
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# magic to ensure that we're always inside the root of our application,
|
||||
# no matter from which directory we'll run script
|
||||
|
@ -1,15 +1,3 @@
|
||||
# list of files to run shellscheck against
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
# main files
|
||||
bashbot.sh
|
||||
commands.sh
|
||||
# list of additional files to check from shellcheck
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
bashbot.rc
|
||||
# example files
|
||||
examples/calc.sh
|
||||
examples/notify.sh
|
||||
examples/question.sh
|
||||
# dev files
|
||||
dev/version.sh
|
||||
dev/install-hooks.sh
|
||||
dev/hooks/pre-commit.sh
|
||||
dev/hooks/pre-push.sh
|
||||
|
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
# shellcheck disable=SC2016
|
||||
#
|
||||
# Easy Versioning in git:
|
||||
@ -45,7 +45,7 @@ unset IFS
|
||||
VERSION="$(git describe --tags --long)"
|
||||
echo "Update to version $VERSION ..."
|
||||
|
||||
FILES="* doc/* dev/* dev/*/*"
|
||||
FILES="$(find ./*)"
|
||||
[ "$1" != "" ] && FILES="$*"
|
||||
|
||||
for file in $FILES
|
||||
|
@ -61,5 +61,5 @@ group. This step is up to you actually.
|
||||
|
||||
#### [Next Getting started](2_usage.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -159,5 +159,5 @@ send_action "${CHAT[ID]}" "action"
|
||||
#### [Prev Create Bot](1_firstbot.md)
|
||||
#### [Next Advanced Usage](3_advanced.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -156,5 +156,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker"
|
||||
#### [Prev Advanced Usage](3_advanced.md)
|
||||
#### [Next Expert Use](4_expert.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -104,5 +104,5 @@ An example crontab is provided in ```examples/bashbot.cron```.
|
||||
#### [Prev Expert Use](4_expert.md)
|
||||
#### [Next Best Practice](5_practice.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -112,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam
|
||||
#### [Prev Best Practice](5_practice.md)
|
||||
#### [Next Functions Reference](6_reference.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -348,5 +348,5 @@ Send Input from Telegram to waiting Interactive Chat.
|
||||
#### [Prev Best Practice](5_practice.md)
|
||||
#### [Next Developer Rules](7_develop.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -31,7 +31,44 @@ For a shell script running as a service it's important to be paranoid about quot
|
||||
In addition you can run ```dev/hooks/pre-commit.sh``` every time you want to shellcheck all files given in 'dev/shellcheck.files'.
|
||||
|
||||
|
||||
## bashbot tests
|
||||
Starting with version 0.70 bashbot has a test suite. To start testsuite run ```test/ALL-tests.sh```. ALL-tests.sh will only return 'SUCCESS' if all tests pass.
|
||||
|
||||
### enabling / disabling tests
|
||||
|
||||
All tests are placed in the directory ```test```. To disable a test remove the x flag from the '*-test.sh' test script, to (re)enable
|
||||
a test make the script executable again.
|
||||
|
||||
|
||||
### creating new tests
|
||||
Each test consists of a script script named like ```p-name-test.sh``` *(where p is test pass 'a-z' and name the name of your test)* and
|
||||
an optional dir ```p-name-test/``` *(script name minus '.sh')* for additional files.
|
||||
|
||||
The file ```ALL-tests.inc.sh``` must be included from all tests, do not forget the shellcheck source directive to statisfy shellcheck.
|
||||
|
||||
Tests with no dependency to other tests will run in pass 'a', tests which need an initialized bahsbot environment must run in pass 'd' or later.
|
||||
If '$1' is present the script is started from 'ALL-tests.sh' and a temporary test environment is setup in directory '$1'.
|
||||
The temporary test environment is created when 'ALL-tests.sh' starts and deleted after all tests are finished.
|
||||
|
||||
Example test
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
# file: b-example-test.sh
|
||||
|
||||
# include common functions and definitions
|
||||
# shellcheck source=test/ALL-tests.inc.sh
|
||||
source "./ALL-tests.inc.sh"
|
||||
|
||||
if [ -f "bashbot.sh" ]; then
|
||||
echo "${SUCCESS} bashbot.sh exist!"
|
||||
exit 0
|
||||
else
|
||||
echo "${NOSUCCESS} bashbot.sh missing!"
|
||||
exit 1
|
||||
fi
|
||||
```
|
||||
|
||||
#### [Prev Function Reference](6_function.md)
|
||||
|
||||
#### $$VERSION$$ v0.61-2-gfd4dd8c
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
@ -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$$ 0.70-dev-4-g12fd839
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
|
||||
SHELL=/bin/sh
|
||||
|
@ -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$$ 0.70-dev-4-g12fd839
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# adjust your language setting here
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
|
@ -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$$ 0.70-dev-4-g12fd839
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# adjust your language setting here
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
|
@ -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$$ 0.70-dev-4-g12fd839
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# adjust your language setting here
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
|
30
examples/question.sh
Executable file
30
examples/question.sh
Executable file
@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# adjust your language setting here
|
||||
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
|
||||
export 'LC_ALL=C.UTF-8'
|
||||
export 'LANG=C.UTF-8'
|
||||
export 'LANGUAGE=C.UTF-8'
|
||||
|
||||
unset IFS
|
||||
# set -f # if you are paranoid use set -f to disable globbing
|
||||
|
||||
echo "Why hello there.
|
||||
Would you like some tea (y/n)?"
|
||||
read -r 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."
|
||||
until [ "$SUCCESS" = "y" ] ;do
|
||||
echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"'
|
||||
read -r answer
|
||||
case $answer in
|
||||
'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;;
|
||||
'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;;
|
||||
*) SUCCESS=n;;
|
||||
esac
|
||||
done
|
||||
exit
|
52
test/ALL-tests.inc.sh
Executable file
52
test/ALL-tests.inc.sh
Executable file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# common variables
|
||||
export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME
|
||||
TESTME="$(basename "$0")"
|
||||
DIRME="$(pwd)"
|
||||
TESTDIR="$1"
|
||||
LOGFILE="${TESTDIR}/${TESTME}.log"
|
||||
REFDIR="${TESTME%.sh}"
|
||||
TESTNAME="${REFDIR//-/ }"
|
||||
|
||||
# common filenames
|
||||
export TOKENFILE ACLFILE COUNTFILE ADMINFILE
|
||||
TOKENFILE="token"
|
||||
ACLFILE="botacl"
|
||||
COUNTFILE="count"
|
||||
ADMINFILE="botadmin"
|
||||
|
||||
# SUCCESS NOSUCCES
|
||||
export SUCCESS NOSUCCESS
|
||||
SUCCESS=" OK"
|
||||
NOSUCCESS=" FAILED!"
|
||||
|
||||
# default input, reference and output files
|
||||
INPUTFILE="${DIRME}/${REFDIR}/${REFDIR}.input"
|
||||
REFFILE="${DIRME}/${REFDIR}/${REFDIR}.result"
|
||||
OUTPUTFILE="${TESTDIR}/${REFDIR}.out"
|
||||
|
||||
# print arrays in reproducible order
|
||||
print_array() {
|
||||
local idx t
|
||||
local arrays=( "${@}" )
|
||||
for idx in "${arrays[@]}"; do
|
||||
declare -n temp="$idx"
|
||||
for t in "${!temp[@]}"; do
|
||||
printf "%s:\t%s\t%s\n" "$idx" "$t" "${temp[$t]}"
|
||||
done | sort
|
||||
done | grep -v '^USER: 0'
|
||||
}
|
||||
|
||||
|
||||
######
|
||||
# lets go ...
|
||||
echo "Running ${TESTNAME#? } ..."
|
||||
echo "............................"
|
||||
[ "${TESTDIR}" = "" ] && echo "${NOSUCCESS} not called from testsuite, exit" && exit 1
|
||||
|
||||
# reset env for test
|
||||
unset IFS; set -f
|
||||
export TERM=""
|
||||
|
54
test/ALL-tests.sh
Executable file
54
test/ALL-tests.sh
Executable file
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
# this has to run once atfer git clone
|
||||
# and every time we create new hooks
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# magic to ensure that we're always inside the root of our application,
|
||||
# no matter from which directory we'll run script
|
||||
GIT_DIR=$(git rev-parse --git-dir)
|
||||
cd "${GIT_DIR}/.." || exit 1
|
||||
|
||||
# create test environment
|
||||
TESTENV="/tmp/bashbot.test$$"
|
||||
mkdir "${TESTENV}"
|
||||
cp -r * "${TESTENV}"
|
||||
cd "test" || exit 1
|
||||
|
||||
#set -e
|
||||
fail=0
|
||||
tests=0
|
||||
passed=0
|
||||
#all_tests=${__dirname:}
|
||||
#echo PLAN ${#all_tests}
|
||||
for test in $(find ./${pass}*-test.sh | sort -u) ;
|
||||
do
|
||||
[ "${test}" = "test/all-tests.sh" ] && continue
|
||||
[ ! -x "${test}" ] && continue
|
||||
tests=$((tests+1))
|
||||
echo "TEST: ${test}"
|
||||
"${test}" "${TESTENV}"
|
||||
ret=$?
|
||||
if [ "$ret" -eq 0 ] ; then
|
||||
echo "OK: ---- ${test}"
|
||||
passed=$((passed+1))
|
||||
else
|
||||
echo "FAIL: $test $fail"
|
||||
fail=$((fail+ret))
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$fail" -eq 0 ]; then
|
||||
/bin/echo -n 'SUCCESS '
|
||||
exitcode=0
|
||||
rm -rf "${TESTENV}"
|
||||
else
|
||||
/bin/echo -n 'FAILURE '
|
||||
exitcode=1
|
||||
rm -rf "${TESTENV}/test"
|
||||
find "${TESTENV}/"* ! -name '[a-z]-*' -delete
|
||||
fi
|
||||
|
||||
echo "${passed} / ${tests}"
|
||||
[ -d "${TESTENV}" ] && echo "Logfiles from run are in ${TESTENV}"
|
||||
exit ${exitcode}
|
5
test/a-commit-test.sh
Normal file
5
test/a-commit-test.sh
Normal file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
../dev/hooks/pre-commit.sh
|
||||
|
4
test/a-push-test.sh
Normal file
4
test/a-push-test.sh
Normal file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
../dev/hooks/pre-push.sh
|
15
test/b-example-test.sh
Normal file
15
test/b-example-test.sh
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# file: b-example-test.sh
|
||||
|
||||
# include common functions and definitions
|
||||
# shellcheck source=test/ALL-tests.inc.sh
|
||||
source "./ALL-tests.inc.sh"
|
||||
|
||||
if [ -f "${TESTDIR}/bashbot.sh" ]; then
|
||||
echo "${SUCCESS} bashbot.sh exist!"
|
||||
exit 0
|
||||
else
|
||||
echo "${NOSUCCESS} bashbot.sh missing!"
|
||||
exit 1
|
||||
fi
|
||||
|
46
test/c-init-test.sh
Executable file
46
test/c-init-test.sh
Executable file
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# include common functions and definitions
|
||||
# shellcheck source=test/ALL-tests.inc.sh
|
||||
source "./ALL-tests.inc.sh"
|
||||
|
||||
TESTTOKEN="bashbottestscript"
|
||||
TESTFILES="${TOKENFILE} ${ACLFILE} ${COUNTFILE} ${ADMINFILE}"
|
||||
|
||||
set -e
|
||||
|
||||
# run bashbot first time with init
|
||||
"${TESTDIR}/bashbot.sh" init >"${LOGFILE}" <<EOF
|
||||
$TESTTOKEN
|
||||
nobody
|
||||
botadmin
|
||||
EOF
|
||||
echo "${SUCCESS}"
|
||||
|
||||
# compare files with refrence files
|
||||
echo "Check new files after init ..."
|
||||
export FAIL="0"
|
||||
for file in ${TESTFILES}
|
||||
do
|
||||
ls -d "${TESTDIR}/${file}" >>"${LOGFILE}"
|
||||
if ! diff -q "${TESTDIR}/${file}" "${REFDIR}/${file}" >>"${LOGFILE}"; then echo "${NOSUCCESS} Fail diff ${file}!"; FAIL="1"; fi
|
||||
|
||||
done
|
||||
[ "${FAIL}" != "0" ] && exit "${FAIL}"
|
||||
echo "${SUCCESS}"
|
||||
|
||||
echo "Test Sourcing of bashbot.sh ..."
|
||||
trap exit 1 EXIT
|
||||
cd "${TESTDIR}" || exit
|
||||
|
||||
# shellcheck source=./bashbot.sh
|
||||
source "${TESTDIR}/bashbot.sh" source
|
||||
trap '' EXIT
|
||||
cd "${DIRME}" || exit 1
|
||||
echo "${SUCCESS}"
|
||||
|
||||
echo "Test bashbot.sh count"
|
||||
cp "${REFDIR}/count.test" "${TESTDIR}/count"
|
||||
"${TESTDIR}/bashbot.sh" count
|
||||
|
1
test/c-init-test/botacl
Normal file
1
test/c-init-test/botacl
Normal file
@ -0,0 +1 @@
|
||||
|
1
test/c-init-test/botadmin
Normal file
1
test/c-init-test/botadmin
Normal file
@ -0,0 +1 @@
|
||||
?
|
1
test/c-init-test/count
Normal file
1
test/c-init-test/count
Normal file
@ -0,0 +1 @@
|
||||
|
80
test/c-init-test/count.test
Normal file
80
test/c-init-test/count.test
Normal file
@ -0,0 +1,80 @@
|
||||
COUNT-3474588
|
||||
COUNT2224533
|
||||
COUNT-10011894466
|
||||
COUNT7053247
|
||||
COUNT6391479
|
||||
COUNT6341244
|
||||
COUNT4919830
|
||||
COUNT2640175
|
||||
COUNT7349731
|
||||
COUNT7244826
|
||||
COUNT2741728
|
||||
COUNT6376258
|
||||
COUNT-10012414459
|
||||
COUNT5869285
|
||||
COUNT-10011297801
|
||||
COUNT190323
|
||||
COUNT7784183
|
||||
COUNT-2766119
|
||||
COUNT-3859393
|
||||
COUNT-10013599717
|
||||
COUNT-10012886611
|
||||
COUNT2442778
|
||||
COUNT5759451
|
||||
COUNT5254245
|
||||
COUNT3781067
|
||||
COUNT7916264
|
||||
COUNT7235018
|
||||
COUNT3349831
|
||||
COUNT2933766
|
||||
COUNT5924516
|
||||
COUNT6374645
|
||||
COUNT6407149
|
||||
COUNT2383316
|
||||
COUNT5447722
|
||||
COUNT231261
|
||||
COUNT7853337
|
||||
COUNT7768269
|
||||
COUNT7546987
|
||||
COUNT7882954
|
||||
COUNT6081264
|
||||
COUNT4473899
|
||||
COUNT6000622
|
||||
COUNT7489339
|
||||
COUNT7925467
|
||||
COUNT146383
|
||||
COUNT1850458
|
||||
COUNT5232050
|
||||
COUNT7827320
|
||||
COUNT3230155
|
||||
COUNT5110579
|
||||
COUNT2968981
|
||||
COUNT6709735
|
||||
COUNT3042403
|
||||
COUNT6973351
|
||||
COUNT3739552
|
||||
COUNT6073516
|
||||
COUNT7773218
|
||||
COUNT7570810
|
||||
COUNT7165830
|
||||
COUNT7836756
|
||||
COUNT2603688
|
||||
COUNT2792256
|
||||
COUNT1199924
|
||||
COUNT7044680
|
||||
COUNT5011616
|
||||
COUNT5944645
|
||||
COUNT6040303
|
||||
COUNT7787713
|
||||
COUNT7670967
|
||||
COUNT4902763
|
||||
COUNT8168450
|
||||
COUNT5233276
|
||||
COUNT-10012311537
|
||||
COUNT7648681
|
||||
COUNT4170584
|
||||
COUNT3045040
|
||||
COUNT5919227
|
||||
COUNT3364980
|
||||
COUNT-3450384
|
||||
COUNT7287381
|
1
test/c-init-test/token
Normal file
1
test/c-init-test/token
Normal file
@ -0,0 +1 @@
|
||||
bashbottestscript
|
40
test/d-process_message-test.sh
Executable file
40
test/d-process_message-test.sh
Executable file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
#### $$VERSION$$ v0.62-0-g5d5dbae
|
||||
|
||||
# include common functions and definitions
|
||||
# shellcheck source=test/ALL-tests.inc.sh
|
||||
source "./ALL-tests.inc.sh"
|
||||
|
||||
set -e
|
||||
|
||||
# source bashbot.sh functionw
|
||||
cd "${TESTDIR}" || exit 1
|
||||
# shellcheck source=./bashbot.sh
|
||||
source "${TESTDIR}/bashbot.sh" source
|
||||
|
||||
# overwrite get_file for test
|
||||
get_file() {
|
||||
echo "$1"
|
||||
}
|
||||
|
||||
# get telegram input from file
|
||||
export UPDATE
|
||||
UPDATE="$(cat "${INPUTFILE}")"
|
||||
|
||||
# run process_message with and without phyton
|
||||
echo "Check process_message ..."
|
||||
for i in 1 2
|
||||
do
|
||||
[ "${i}" = "1" ] && ! which python >/dev/null 2>&1 && continue
|
||||
[ "${i}" = "1" ] && echo " ... JsonDecode Phyton"
|
||||
[ "${i}" = "2" ] && echo " ... JsonDecode Bash" && export BASHDECODE="yes"
|
||||
set -x
|
||||
{ process_message "0"; set +x; } >>"${LOGFILE}" 2>&1;
|
||||
|
||||
# output processed input
|
||||
print_array "USER" "CHAT" "REPLYTO" "FORWARD" "URLS" "CONTACT" "CAPTION" "LOCATION" "MESSAGE" >"${OUTPUTFILE}"
|
||||
diff -c "${REFFILE}" "${OUTPUTFILE}" || exit 1
|
||||
echo "${SUCCESS}"
|
||||
done
|
||||
|
||||
cd "${DIRME}" || exit 1
|
93
test/d-process_message-test/d-process_message-test.input
Normal file
93
test/d-process_message-test/d-process_message-test.input
Normal file
@ -0,0 +1,93 @@
|
||||
["ok"] true
|
||||
["result",0,"update_id"] 146860800
|
||||
["result",0,"message","message_id"] 6541 6541 # this is a cross check for JsonGetValue
|
||||
["result",0,"message","text"] "\ud83d\ude02\ud83d\ude1d\ud83d\udc4c\u263a\u2764\ud83d\ude15\ud83d\ude08#\u20e3\ud83c\udf0f\ud83c\udf89\ud83d\ude4a\ud83d\ude49\u2615\ud83d\ude80\u2708\ud83d\ude82\ud83d\udcaf\u2714\u303d\ud83d\udd1a"
|
||||
["result",0,"message","text"] "Cross check, this second text must be ignored"
|
||||
["result",0,"message","from","id"] 123456789
|
||||
["result",0,"message","from","is_bot"] false
|
||||
["result",0,"message","from","first_name"] "Kay"
|
||||
["result",0,"message","from","last_name"] "M"
|
||||
["result",0,"message","from","username"] "Gnadelwartz"
|
||||
["result",0,"message","from","language_code"] "de"
|
||||
["result",0,"message","chat","id"] 123456789
|
||||
["result",0,"message","chat","first_name"] "Test"
|
||||
["result",0,"message","chat","last_name"] "Bot"
|
||||
["result",0,"message","chat","username"] "BotTest"
|
||||
["result",0,"message","chat","title"] "BotTestTitle"
|
||||
["result",0,"message","chat","type"] "private"
|
||||
["result",0,"message","date"] 1555822879
|
||||
["result",0,"message","reply_to_message","message_id"] 6542
|
||||
["result",0,"message","reply_to_message","from","id"] 987654321
|
||||
["result",0,"message","reply_to_message","from","is_bot"] true
|
||||
["result",0,"message","reply_to_message","from","first_name"] "dealzbot"
|
||||
["result",0,"message","reply_to_message","from","username"] "Deal_O_Mat_bot"
|
||||
["result",0,"message","reply_to_message","chat","id"] 123456789
|
||||
["result",0,"message","reply_to_message","chat","first_name"] "Kay"
|
||||
["result",0,"message","reply_to_message","chat","last_name"] "M"
|
||||
["result",0,"message","reply_to_message","chat","username"] "Gnadelwartz"
|
||||
["result",0,"message","reply_to_message","chat","type"] "private"
|
||||
["result",0,"message","reply_to_message","date"] 1555822747
|
||||
["result",0,"message","reply_to_message","text"] "Ich bin der Deal-O-Mat Bot. F\u00fcr eine Liste der Befehle sende /help"
|
||||
["result",0,"message","reply_to_message","entities",0,"offset"] 12
|
||||
["result",0,"message","reply_to_message","entities",0,"length"] 14
|
||||
["result",0,"message","reply_to_message","entities",0,"type"] "bold"
|
||||
["result",0,"message","reply_to_message","entities",0] {"offset":12,"length":14,"type":"bold"}
|
||||
["result",0,"message","reply_to_message","entities",1,"offset"] 61
|
||||
["result",0,"message","reply_to_message","entities",1,"length"] 5
|
||||
["result",0,"message","reply_to_message","entities",1,"type"] "bot_command"
|
||||
["result",0,"message","forward_from","id"] 123456789
|
||||
["result",0,"message","forward_from","is_bot"] false
|
||||
["result",0,"message","forward_from","first_name"] "Kay"
|
||||
["result",0,"message","forward_from","last_name"] "M"
|
||||
["result",0,"message","forward_from","username"] "Gnadelwartz"
|
||||
["result",0,"message","forward_from","language_code"] "de"
|
||||
["result",0,"message","forward_date"] 1555822879
|
||||
["result",0,"message","audio","file_id"] "audio-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
["result",0,"message","video","file_id"] "video-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
["result",0,"message","document","file_id"] "document-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
["result",0,"message","sticker","file_id"] "sticker-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
["result",0,"message","sticker","file_id"] "sticker-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
["result",0,"message","voice","file_id"] "voice-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
|
||||
["result",0,"message","photo",0,"file_id"] "photo-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa"
|
||||
["result",0,"message","photo",0,"file_size"] 1468
|
||||
["result",0,"message","photo",0,"width"] 67
|
||||
["result",0,"message","photo",0,"height"] 90
|
||||
["result",0,"message","photo",0] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa_gDAAEC","file_size":1468,"width":67,"height":90}
|
||||
["result",0,"message","photo",1,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOFli9a5TF3rbPgDAAEC"
|
||||
["result",0,"message","photo",1,"file_size"] 2512
|
||||
["result",0,"message","photo",1,"width"] 240
|
||||
["result",0,"message","photo",1,"height"] 320
|
||||
["result",0,"message","photo",1] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOFli9a5TF3rbPgDAAEC","file_size":25125,"width":240,"height":320}
|
||||
["result",0,"message","photo",2,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABCVM2ZSdqfMZbfgDAAEC"
|
||||
["result",0,"message","photo",2,"file_size"] 127851
|
||||
["result",0,"message","photo",2,"width"] 600
|
||||
["result",0,"message","photo",2,"height"] 800
|
||||
["result",0,"message","photo",2] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABCVM2ZSdqfMZbfgDAAEC","file_size":127851,"width":600,"height":800}
|
||||
["result",0,"message","photo",3,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABGs7gD6jTEJuafgDAAEC"
|
||||
["result",0,"message","photo",3,"file_size"] 245679
|
||||
["result",0,"message","photo",3,"width"] 1200
|
||||
["result",0,"message","photo",3,"height"] 1600
|
||||
["result",0,"message","photo",3] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABGs7gD6jTEJuafgDAAEC","file_size":245679,"width":1200,"height":1600}
|
||||
["result",0,"message","photo",4,"file_id"] "AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABDsKqM0vfngJavgDAAEC"
|
||||
["result",0,"message","photo",4,"file_size"] 272842
|
||||
["result",0,"message","photo",4,"width"] 960
|
||||
["result",0,"message","photo",4,"height"] 1280
|
||||
["result",0,"message","photo",4] {"file_id":"AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABDsKqM0vfngJavgDAAEC","file_size":272842,"width":960,"height":1280}
|
||||
["result",0,"message","location","latitude"] 49.631824
|
||||
["result",0,"message","location","longitude"] 8.377072
|
||||
["result",0,"message","location"] {"latitude":49.631824,"longitude":8.377072}
|
||||
["result",0,"message","venue","location","latitude"] 49.631824
|
||||
["result",0,"message","venue","location","longitude"] 8.377072
|
||||
["result",0,"message","venue","location"] {"latitude":49.631824,"longitude":8.377072}
|
||||
["result",0,"message","venue","title"] "Kolb's Biergarten"
|
||||
["result",0,"message","venue","address"] "Am Rhein 1"
|
||||
["result",0,"message","venue","foursquare_id"] "4c4321afce54e21eee980d1a"
|
||||
["result",0,"message","contact","phone_number"] "222222"
|
||||
["result",0,"message","contact","first_name"] "ADAC"
|
||||
["result",0,"message","contact","last_name"] "Pannenhilfe"
|
||||
["result",0,"message","contact","vcard"] "BEGIN:VCARD\nVERSION:2.1\nN:Pannenhilfe;ADAC;;;\nFN:ADAC Pannenhilfe\nTEL;CELL;PREF:+49179222222\nTEL;X-Mobil:222222\nEND:VCARD"
|
||||
["result",0,"message","voice","duration"] 2
|
||||
["result",0,"message","voice","mime_type"] "audio/ogg"
|
||||
["result",0,"message","voice","file_id"] "AwADAgADOAQAAqd24Emnm_VGmmVEuAI"
|
||||
["result",0,"message","voice","file_size"] 4262
|
37
test/d-process_message-test/d-process_message-test.result
Normal file
37
test/d-process_message-test/d-process_message-test.result
Normal file
@ -0,0 +1,37 @@
|
||||
USER: FIRST_NAME Kay
|
||||
USER: ID 123456789
|
||||
USER: LAST_NAME M
|
||||
USER: USERNAME Gnadelwartz
|
||||
CHAT: ALL_MEMBERS_ARE_ADMINISTRATORS
|
||||
CHAT: FIRST_NAME Test
|
||||
CHAT: ID 123456789
|
||||
CHAT: LAST_NAME Bot
|
||||
CHAT: TITLE BotTestTitle
|
||||
CHAT: TYPE private
|
||||
CHAT: USERNAME BotTest
|
||||
REPLYTO: 0 Ich bin der Deal-O-Mat Bot. Für eine Liste der Befehle sende /help
|
||||
REPLYTO: FIRST_NAME dealzbot
|
||||
REPLYTO: ID 6542
|
||||
REPLYTO: LAST_NAME
|
||||
REPLYTO: UID 987654321
|
||||
REPLYTO: USERNAME Deal_O_Mat_bot
|
||||
FORWARD: FIRST_NAME Kay
|
||||
FORWARD: ID 6541
|
||||
FORWARD: LAST_NAME M
|
||||
FORWARD: UID 123456789
|
||||
FORWARD: USERNAME Gnadelwartz
|
||||
URLS: AUDIO audio-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa
|
||||
URLS: DOCUMENT document-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa
|
||||
URLS: PHOTO photo-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa
|
||||
URLS: STICKER sticker-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa
|
||||
URLS: VIDEO video-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa
|
||||
URLS: VOICE voice-AgADAgADL6oxG-Gw4EndCWGl2WUfUo1pXw8ABOusSilDGzAYa
|
||||
CONTACT: FIRST_NAME ADAC
|
||||
CONTACT: LAST_NAME Pannenhilfe
|
||||
CONTACT: NUMBER 222222
|
||||
CONTACT: USER_ID
|
||||
CAPTION: 0
|
||||
LOCATION: LATITUDE 49.631824
|
||||
LOCATION: LONGITUDE 8.377072
|
||||
MESSAGE: 0 😂😝👌☺❤😕😈#⃣🌏🎉🙊🙉☕🚀✈🚂💯✔〽🔚
|
||||
MESSAGE: ID 6541
|
Loading…
Reference in New Issue
Block a user