telegram-bot-bash/addons/example.sh

116 lines
4.1 KiB
Bash
Raw Normal View History

2019-05-25 15:02:25 +00:00
#!/bin/bash
# file: addons/example.sh.dist
#
2020-06-23 14:35:50 +00:00
# Addons can register to bashbot events at startup
2019-05-25 15:02:25 +00:00
# by providing their name and a callback per event
#
2020-12-31 17:20:32 +00:00
#### $$VERSION$$ v1.21-dev-44-gb04ee84
2019-05-30 18:59:17 +00:00
#
2020-06-23 14:35:50 +00:00
# If an event occurs each registered event function is called.
2019-05-25 17:31:20 +00:00
#
# Events run in the same context as the main bashbot event loop
# so variables set here are persistent as long bashbot is running.
#
# Note: For the same reason event function MUST return imideatly!
# compute intensive tasks must be run in a nonblocking subshell,
# e.g. "(long running) &"
#
2019-05-25 15:02:25 +00:00
2020-06-23 14:35:50 +00:00
# Available events:
2019-05-25 17:31:20 +00:00
# on events startbot and init, this file is sourced
2019-05-25 15:02:25 +00:00
#
2019-05-26 15:40:51 +00:00
# BASHBOT_EVENT_INLINE inline query received
# BASHBOT_EVENT_MESSAGE any type of message received
2019-05-27 10:27:09 +00:00
# BASHBOT_EVENT_TEXT message containing message text received
# BASHBOT_EVENT_CMD a command is received
2019-05-26 15:40:51 +00:00
# BASHBOT_EVENT_REPLYTO reply to message received
# BASHBOT_EVENT_FORWARD forwarded message received
# BASHBOT_EVENT_CONTACT contact received
# BASHBOT_EVENT_LOCATION location or venue received
# BASHBOT_EVENT_FILE file received
2019-05-25 15:02:25 +00:00
#
# BASHBOT_EVENT_TIMER this event is a bit special as it fires every Minute
2020-06-23 14:35:50 +00:00
# and has 3 meanings: oneshot, every time, every X minutes.
2019-05-29 11:49:05 +00:00
#
2019-05-25 17:31:20 +00:00
# all global variables and functions can be used in registered functions.
#
# parameters when loaded
# $1 event: init, startbot ...
# $2 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic
#
2020-06-23 14:35:50 +00:00
# parameters on events
2019-05-25 17:31:20 +00:00
# $1 event: inline, message, ..., file
2019-06-03 18:34:43 +00:00
# $2 key: key of array BASHBOT_EVENT_xxx
# $3 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic
2019-05-25 17:31:20 +00:00
#
2019-05-25 15:02:25 +00:00
2019-05-26 15:40:51 +00:00
# export used events
2019-06-04 16:04:52 +00:00
export BASHBOT_EVENT_INLINE BASHBOT_EVENT_CMD BASHBOT_EVENT_REPLY BASHBOT_EVENT_TIMER BASHBOT_EVENT_SEND
2019-05-26 15:40:51 +00:00
2019-05-25 15:02:25 +00:00
# any global variable defined by addons MUST be prefixed by addon name
EXAMPLE_ME="example"
2019-05-25 17:31:20 +00:00
# initialize after installation or update
if [[ "$1" = "init"* ]]; then
2019-05-27 10:27:09 +00:00
: # nothing to do
2019-05-25 17:31:20 +00:00
fi
2019-05-25 15:02:25 +00:00
2019-05-25 17:31:20 +00:00
# register on startbot
if [[ "$1" = "start"* ]]; then
# register to reply
2019-05-26 15:40:51 +00:00
BASHBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply"
2020-12-31 17:20:32 +00:00
EXAMPLE_ADMIN="$(getConfigKey "botadmin")"
2019-05-25 15:02:25 +00:00
2019-05-25 17:31:20 +00:00
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_reply(){
2019-06-03 18:34:43 +00:00
local msg="message" event="$1" key="$2"
send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to ${msg} from *${REPLYTO[USERNAME]}* (Event: ${event} Key:{${key})" &
2019-05-25 17:31:20 +00:00
}
2019-05-27 10:27:09 +00:00
# register to inline and command
BASHBOT_EVENT_INLINE["${EXAMPLE_ME}"]="${EXAMPLE_ME}_multievent"
BASHBOT_EVENT_CMD["${EXAMPLE_ME}"]="${EXAMPLE_ME}_multievent"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_multievent(){
2019-06-03 18:34:43 +00:00
local event="$1" key="$2"
2019-05-27 10:27:09 +00:00
local msg="${MESSAGE[0]}"
# shellcheck disable=SC2154
[ "${type}" = "inline" ] && msg="${iQUERY[0]}"
2019-06-03 18:34:43 +00:00
send_normal_message "${CHAT[ID]}" "${event} from ${key} received: ${msg}" &
2019-05-29 11:49:05 +00:00
}
BASHBOT_EVENT_TIMER["${EXAMPLE_ME}after5min","-5"]="${EXAMPLE_ME}_after5min"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_after5min(){
2020-12-31 17:20:32 +00:00
send_markdown_message "${EXAMPLE_ADMIN}" "This is a one time event after 5 Minutes!" &
2019-05-29 11:49:05 +00:00
}
BASHBOT_EVENT_TIMER["${EXAMPLE_ME}every2min","2"]="${EXAMPLE_ME}_every2min"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_every2min(){
2020-12-31 17:20:32 +00:00
send_markdown_message "${EXAMPLE_ADMIN}" "This a a every 2 minute event ..." &
2019-05-27 10:27:09 +00:00
}
2019-06-04 16:04:52 +00:00
# register to send
BASHBOT_EVENT_SEND["${EXAMPLE_ME}"]="${EXAMPLE_ME}_log"
EXAMPLE_LOG="${BASHBOT_ETC:-.}/addons/${EXAMPLE_ME}.log"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
# $1 = send / upload
# $* remaining args are from sendJson and sendUpload
# Note: do not call any send message functions from EVENT_SEND!
example_log(){
local send="$1"; shift
2020-12-26 20:29:16 +00:00
printf "%s: Type: %s Args: %s\n" "$(date)" "${send}" "$*" >>"${EXAMPLE_LOG}"
2019-06-04 16:04:52 +00:00
}
2019-05-25 17:31:20 +00:00
fi