telegram-bot-bash/addons/example.sh

73 lines
2.3 KiB
Bash
Raw Normal View History

2019-05-25 15:02:25 +00:00
#!/bin/bash
# file: addons/example.sh.dist
#
# Addons can register to bashbot events at statup
# by providing their name and a callback per event
#
2019-05-25 17:31:20 +00:00
# If an event occours each registered event function is called.
#
# 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
# Availible 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
# 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
#
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
#
# prameters on events
# $1 event: inline, message, ..., file
# $2 debug: use "[[ "$2" = *"debug"* ]]" if you want to output extra diagnostic
#
2019-05-25 15:02:25 +00:00
2019-05-26 15:40:51 +00:00
# export used events
export BASHBOT_EVENT_INLINE BASHBOT_EVENT_REPLY
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
: # notihung to do
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 inline
2019-05-26 15:40:51 +00:00
BASHBOT_EVENT_INLINE["${EXAMPLE_ME}"]="${EXAMPLE_ME}_inline"
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_inline(){
2019-05-25 15:08:28 +00:00
local msg="${MESSAGE}"
send_normal_message "${CHAT[ID]}" "Inline query received: ${msg}"
2019-05-25 17:31:20 +00:00
}
2019-05-25 15:02:25 +00:00
2019-05-25 17:31:20 +00:00
# register to reply
2019-05-26 15:40:51 +00:00
BASHBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply"
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-05-25 15:08:28 +00:00
local msg="message"
send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to ${msg} from *${REPLYTO[USERNAME]}*"
2019-05-25 17:31:20 +00:00
}
fi