add EVENT_TEXT and EVENT_CMD

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-27 12:27:09 +02:00
parent 9b05ae0220
commit bbbc8aece1
2 changed files with 53 additions and 18 deletions

View File

@ -19,6 +19,8 @@
# #
# BASHBOT_EVENT_INLINE inline query received # BASHBOT_EVENT_INLINE inline query received
# BASHBOT_EVENT_MESSAGE any type of message received # BASHBOT_EVENT_MESSAGE any type of message received
# BASHBOT_EVENT_TEXT message containing message text received
# BASHBOT_EVENT_CMD a command is recieved
# BASHBOT_EVENT_REPLYTO reply to message received # BASHBOT_EVENT_REPLYTO reply to message received
# BASHBOT_EVENT_FORWARD forwarded message received # BASHBOT_EVENT_FORWARD forwarded message received
# BASHBOT_EVENT_CONTACT contact received # BASHBOT_EVENT_CONTACT contact received
@ -37,29 +39,19 @@
# #
# export used events # export used events
export BASHBOT_EVENT_INLINE BASHBOT_EVENT_REPLY export BASHBOT_EVENT_INLINE BASHBOT_EVENT_CMD BASHBOT_EVENT_REPLY
# any global variable defined by addons MUST be prefixed by addon name # any global variable defined by addons MUST be prefixed by addon name
EXAMPLE_ME="example" EXAMPLE_ME="example"
# initialize after installation or update # initialize after installation or update
if [[ "$1" = "init"* ]]; then if [[ "$1" = "init"* ]]; then
: # notihung to do : # nothing to do
fi fi
# register on startbot # register on startbot
if [[ "$1" = "start"* ]]; then if [[ "$1" = "start"* ]]; then
# register to inline
BASHBOT_EVENT_INLINE["${EXAMPLE_ME}"]="${EXAMPLE_ME}_inline"
# any function defined by addons MUST be prefixed by addon name
# function local variables can have any name, but must be LOCAL
example_inline(){
local msg="${MESSAGE}"
send_normal_message "${CHAT[ID]}" "Inline query received: ${msg}"
}
# register to reply # register to reply
BASHBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply" BASHBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply"
@ -69,4 +61,18 @@ if [[ "$1" = "start"* ]]; then
local msg="message" local msg="message"
send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to ${msg} from *${REPLYTO[USERNAME]}*" send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to ${msg} from *${REPLYTO[USERNAME]}*"
} }
# 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(){
local type="$1"
local msg="${MESSAGE[0]}"
# shellcheck disable=SC2154
[ "${type}" = "inline" ] && msg="${iQUERY[0]}"
send_normal_message "${CHAT[ID]}" "${type} received: ${msg}"
}
fi fi

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$$ v0.90-dev2-6-g3c6b2d3 #### $$VERSION$$ v0.90-dev2-8-g9b05ae0
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -133,8 +133,9 @@ UPD_URL=$URL'/getUpdates?offset='
GETFILE_URL=$URL'/getFile' GETFILE_URL=$URL'/getFile'
unset USER unset USER
declare -a CMD
declare -A UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY declare -A UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY
export res UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY CAPTION export res CMD UPD BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE iQUERY CAPTION
COMMANDS="${BASHBOT_ETC:-.}/commands.sh" COMMANDS="${BASHBOT_ETC:-.}/commands.sh"
@ -327,6 +328,7 @@ process_updates() {
} }
process_client() { process_client() {
local num="$1" debug="$2" local num="$1" debug="$2"
CMD=( )
iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}" iQUERY[ID]="${UPD["result",${num},"inline_query","id"]}"
if [ "${iQUERY[ID]}" = "" ]; then if [ "${iQUERY[ID]}" = "" ]; then
[[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log" [[ "${debug}" = *"debug"* ]] && cat <<< "$UPDATE" >>"MESSAGE.log"
@ -353,6 +355,11 @@ process_client() {
grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${COUNTFILE}" grep -q "$tmpcount" <"${COUNTFILE}" &>/dev/null || cat <<< "$tmpcount" >>"${COUNTFILE}"
} }
declare -A BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASHBOT_EVENT_CMD BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD
declare -A BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT
export BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASHBOT_EVENT_CMD BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD
export BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE BASHBOT_EVENT_TEXT
event_inline() { event_inline() {
local event debug="$1" local event debug="$1"
# shellcheck disable=SC2153 # shellcheck disable=SC2153
@ -370,6 +377,24 @@ event_message() {
_exec_if_function "${BASHBOT_EVENT_MESSAGE[${event}]}" "messsage" "${debug}" _exec_if_function "${BASHBOT_EVENT_MESSAGE[${event}]}" "messsage" "${debug}"
done done
# ${TEXT[*]} event_text
if [ "${MESSAGE[0]}" != "" ]; then
# shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_TEXT[@]}"
do
_exec_if_function "${BASHBOT_EVENT_TEXT[${event}]}" "text" "${debug}"
done
# ${CMD[*]} event_cmd
if [ "${CMD[0]}" != "" ]; then
# shellcheck disable=SC2153
for event in "${!BASHBOT_EVENT_CMD[@]}"
do
_exec_if_function "${BASHBOT_EVENT_CMD[${event}]}" "command" "${debug}"
done
fi
fi
# ${REPLYTO[*]} event_replyto # ${REPLYTO[*]} event_replyto
if [ "${REPLYTO[UID]}" != "" ]; then if [ "${REPLYTO[UID]}" != "" ]; then
# shellcheck disable=SC2153 # shellcheck disable=SC2153
@ -504,11 +529,15 @@ process_message() {
# Location # Location
LOCATION[LONGITUDE]="${UPD["result",${num},"message","location","longitude"]}" LOCATION[LONGITUDE]="${UPD["result",${num},"message","location","longitude"]}"
LOCATION[LATITUDE]="${UPD["result",${num},"message","location","latitude"]}" LOCATION[LATITUDE]="${UPD["result",${num},"message","location","latitude"]}"
# event_message &
}
declare -A BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE
export BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE
# split message in command and args
if [[ "${MESSAGE[0]}" = "/"* ]]; then
set -f; unset IFS
# shellcheck disable=SC2206
CMD=( ${MESSAGE[0]} )
set +f
fi
}
######################### #########################
# main get updates loop, should never terminate # main get updates loop, should never terminate