diff --git a/commands.sh b/commands.sh old mode 100755 new mode 100644 index 819970c..5b9fa33 --- a/commands.sh +++ b/commands.sh @@ -1,14 +1,14 @@ #!/bin/bash -# Edit your commands in this file. +# file: commands.sh +# do not edit this file, instead place all your commands in mycommands.sh # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.70-dev2-10-gfa9e879 +#### $$VERSION$$ v0.70-dev2-15-g03f22c9 # # 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 @@ -20,8 +20,10 @@ unset IFS # set -f # if you are paranoid use set -f to disable globbing +# to change the default info and help messages copy bashbot_info() and bahsbot_help() +# to mycommands.sh and edit the messages there. + 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. @@ -42,16 +44,12 @@ 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]}" - } + # load additional modules + [ -r "modules/aliases.sh" ] && source "modules/aliases.sh" # some handy aliases, e.g. _is_admin and _message + # ... + # shellcheck source=./commands.sh + [ -r "mycommands.sh" ] && source "mycommands.sh" + fi if [ "$1" = "source" ];then @@ -96,33 +94,6 @@ else fi & fi case "$MESSAGE" in - '/question') - checkproc - if [ "$res" -gt 0 ] ; then - startproc "example/question" - else - send_normal_message "${CHAT[ID]}" "$MESSAGE already running ..." - fi - ;; - - '/run-notify') - myback="notify"; checkback "$myback" - if [ "$res" -gt 0 ] ; then - background "example/notify 60" "$myback" # notify every 60 seconds - else - send_normal_message "${CHAT[ID]}" "Background command $myback already running ..." - fi - ;; - '/stop-notify') - myback="notify"; checkback "$myback" - if [ "$res" -eq 0 ] ; then - killback "$myback" - send_normal_message "${CHAT[ID]}" "Background command $myback canceled." - else - send_normal_message "${CHAT[ID]}" "No background command $myback is currently running.." - fi - ;; - ################################################ # DEFAULT commands start here, edit messages only '/info') @@ -156,6 +127,7 @@ else ;; *) # forward input to interactive chat if running if tmux ls | grep -v send | grep -q "$copname"; then inproc; fi + _is_function mycommands && mycommands ;; esac fi diff --git a/dev/make-dist.sh b/dev/make-dist.sh index 9ef2619..f66d3ee 100755 --- a/dev/make-dist.sh +++ b/dev/make-dist.sh @@ -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.70-dev2-10-gfa9e879 +#### $$VERSION$$ v0.70-dev2-15-g03f22c9 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -12,7 +12,7 @@ VERSION="$(git describe --tags | sed -e 's/-.*//' -e 's/v//')" DISTNAME="telegram-bot-bash" DISTDIR="./dist/${DISTNAME}" -DISTFILES="bashbot.rc bashbot.sh commands.sh doc examples LICENSE README.md README.txt" +DISTFILES="bashbot.rc bashbot.sh commands.sh mycommands.sh doc examples modules LICENSE README.md README.txt" # run tests first! @@ -32,6 +32,7 @@ cd "${DISTDIR}" || exit 1 # additional stuff mv "commands.sh" "commands.sh.dist" +mv "mycommands.sh" "mycommands.sh.dist" JSONSHFILE="JSON.sh/JSON.sh" if [ ! -f "${JSONSHFILE}" ]; then @@ -49,8 +50,4 @@ tar -czf "${DISTNAME}-${VERSION}.tar.gz" "${DISTNAME}" # shellcheck disable=SC2086 ls -ld ${DISTNAME}-${VERSION}.* -# remove temporary dist dir -cd "$GIT_DIR/.." || exit 1 -rm -rf ${DISTDIR} - diff --git a/modules/aliases.sh b/modules/aliases.sh new file mode 100644 index 0000000..bf76c79 --- /dev/null +++ b/modules/aliases.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# file: modules/alaises.sh +# do not edit, this file will be overwritten on update + +# This file is public domain in the USA and all free countries. +# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) +# +#### $$VERSION$$ v0.70-dev2-15-g03f22c9 + +# source from commands.sh to use the aliases +# some handy aliases for use when processing messages: +_is_botadmin() { + user_is_botadmin "${USER[ID]}" +} +_is_admin() { + user_is_admin "${CHAT[ID]}" "${USER[ID]}" +} +_is_allowed() { + user_is_allowed "${USER[ID]}" "$1" "${CHAT[ID]}" +} +# same for easy sending messages of messages +_message() { + send_normal_message "${CHAT[ID]}" "$1" +} +_normal_message() { + send_normal_message "${CHAT[ID]}" "$1" +} +_html_message() { + send_html_message "${CHAT[ID]}" "$1" +} +_markdown_message() { + send_markdown_message "${CHAT[ID]}" "$1" +} diff --git a/mycommands.sh b/mycommands.sh new file mode 100644 index 0000000..9e6fd89 --- /dev/null +++ b/mycommands.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# files: mycommands.sh.dist +# copy to mycommands.sh and add all your commands and functions here ... +# +#### $$VERSION$$ v0.70-dev2-15-g03f22c9 +# +# shellcheck disable=SC2154 +# shellcheck disable=SC2034 + +# your additional bahsbot commands +mycommands() { + + case "$MESSAGE" in + '/echo'*) # example echo command + send_normal_message "${CHAT[ID]}" "$MESSAGE" + ;; + '/question') # start interactive questions + checkproc + if [ "$res" -gt 0 ] ; then + startproc "example/question" + else + send_normal_message "${CHAT[ID]}" "$MESSAGE already running ..." + fi + ;; + + '/run-notify') # start notify background job + myback="notify"; checkback "$myback" + if [ "$res" -gt 0 ] ; then + background "example/notify 60" "$myback" # notify every 60 seconds + else + send_normal_message "${CHAT[ID]}" "Background command $myback already running ..." + fi + ;; + '/stop-notify') # kill notify background job + myback="notify"; checkback "$myback" + if [ "$res" -eq 0 ] ; then + killback "$myback" + send_normal_message "${CHAT[ID]}" "Background command $myback canceled." + else + send_normal_message "${CHAT[ID]}" "No background command $myback is currently running.." + fi + ;; + + esac +} + +# place your processing functions here