telegram-bot-bash/mycommands.sh

360 lines
14 KiB
Bash
Raw Normal View History

#!/bin/bash
#######################################################
2020-05-14 12:21:15 +00:00
#
# File: mycommands.sh.dist
#
2020-08-01 07:10:14 +00:00
# this is an out of the box test and example file to show what's possible in mycommands.sh
2020-05-14 12:21:15 +00:00
#
2020-05-14 13:02:17 +00:00
# #### if you start to develop your own bot, use the clean version of this file:
2020-05-14 12:21:15 +00:00
# #### mycommands.clean
#
# Usage: will be executed when a bot command is received
#
# License: WTFPLv2 http://www.wtfpl.net/txt/copying/
# Author: KayM (gnadelwartz), kay@rrr.de
#
2021-06-03 12:21:40 +00:00
#### $$VERSION$$ v1.51-0-g6e66a28
#######################################################
# shellcheck disable=SC1117
####################
# Config has moved to bashbot.conf
# shellcheck source=./commands.sh
[ -r "${BASHBOT_ETC:-.}/mycommands.conf" ] && source "${BASHBOT_ETC:-.}/mycommands.conf" "$1"
2020-05-14 13:02:17 +00:00
2020-08-01 07:10:14 +00:00
##################
# let's go ...
2020-05-14 13:02:17 +00:00
if [ "$1" = "startbot" ];then
###################
2020-08-15 07:29:13 +00:00
# this section is processed on startup
# mark startup, triggers action on first message
2020-08-06 06:43:28 +00:00
setConfigKey "startupaction" "await"
2020-05-14 13:02:17 +00:00
else
#############################
# your own bashbot commands
2020-08-01 07:10:14 +00:00
# NOTE: command can have @botname attached, you must add * to case tests...
mycommands() {
2020-08-15 07:29:13 +00:00
#############
# action triggered on first message after startup
if [[ "$(getConfigKey "startupaction")" != "done"* ]]; then
# send message to first user on startup
send_normal_message "${CHAT[ID]}" "Hi, you are the first user after startup!"
2020-08-15 07:29:13 +00:00
# mark as done and when
setConfigKey "startupaction" "done $(date)"
fi
2020-05-14 13:02:17 +00:00
##############
# a service Message was received
2020-05-14 13:02:17 +00:00
# add your own stuff here
if [ -n "${SERVICE}" ]; then
2020-05-14 13:02:17 +00:00
# example: delete every service message
if [ "${SILENCER}" = "yes" ]; then
delete_message "${CHAT[ID]}" "${MESSAGE[ID]}"
fi
fi
2020-06-27 14:01:27 +00:00
# remove keyboard if you use keyboards
[ -n "${REMOVEKEYBOARD}" ] && remove_keyboard "${CHAT[ID]}" &
[[ -n "${REMOVEKEYBOARD_PRIVATE}" && "${CHAT[ID]}" == "${USER[ID]}" ]] && remove_keyboard "${CHAT[ID]}" &
2020-06-23 14:35:50 +00:00
# example for actions based on chat or sender
case "${USER[ID]}+${CHAT[ID]}" in
2020-11-29 14:34:00 +00:00
'USERID1+'*) # do something for all messages from USER
printf "%s: U=%s C=%s M=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${MESSAGE}" >>"${DATADIR}/${USER[ID]}.log"
;;&
2020-11-29 14:34:00 +00:00
*'+CHATID1') # do something for all messages from CHAT
printf "%s: U=%s C=%s M=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${MESSAGE}" >>"${DATADIR}/${CHAT[ID]}.log"
;;&
2020-11-29 14:34:00 +00:00
'USERID2+CHATID2') # do something only for messages form USER in CHAT
printf "%s: U=%s C=%s M=%s\n" "$(date)" "${USER[ID]}" "${CHAT[ID]}" "${MESSAGE}" >>"${DATADIR}/${CHAT[ID]}+${USER[ID]}.log"
;;&
esac
# fix first letter upper case because of smartphone auto correction
[[ "${MESSAGE}" =~ ^/[[:upper:]] ]] && MESSAGE="${MESSAGE:0:1}$(tr '[:upper:]' '[:lower:]' <<<"${MESSAGE:1:1}")${MESSAGE:2}"
2020-08-01 07:10:14 +00:00
# pre-check admin only commands
2020-05-16 20:10:12 +00:00
case "${MESSAGE}" in
# must be private, group admin, or botadmin
'/run_'*|'stop_'*)
send_action "${CHAT[ID]}" "typing"
if ! user_is_admin "${CHAT[ID]}" "${USER[ID]}" ; then
send_normal_message "${CHAT[ID]}" "${NOTADMIN}"; return 1
fi
# ok, now lets process the real command
;;
# must be botadmin
'/echo'*)
send_action "${CHAT[ID]}" "typing"
if ! user_is_botadmin "${USER[ID]}" ; then
2020-12-14 15:26:27 +00:00
send_markdownv2_message "${CHAT[ID]}" "*${NOTBOTADMIN}*"; return 1
2020-05-16 20:10:12 +00:00
fi
;;
# will we process edited messages also?
2020-06-29 13:39:03 +00:00
'/_edited_message'*)
return 1 # no
# but if we do, remove /edited_message
MESSAGE="${MESSAGE#/* }"
;;
'/_new_chat_member'*)
if [[ -n "${WELCOME_NEWMEMBER}" && "${NEWMEMBER[ISBOT]}" != "true" ]] && bot_is_admin "${CHAT[ID]}"; then
send_normal_message "${CHAT[ID]}"\
"${WELCOME_MSG} ${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]} (@${NEWMEMBER[USERNAME]})"
MYSENTID="${BOTSENT[ID]}"
{ sleep 5; delete_message "${CHAT[ID]}" "${MYSENTID}"; } &
2021-03-03 14:33:40 +00:00
[ -n "${REPORT_NEWMEMBER}" ] && send_normal_message "${BOTADMIN}"\
2020-07-12 13:09:52 +00:00
"New member: ${CHAT[TITLE]} (${CHAT[ID]}): ${NEWMEMBER[FIRST_NAME]} ${NEWMEMBER[LAST_NAME]} (@${NEWMEMBER[USERNAME]})"
fi
;;
2020-07-12 13:18:11 +00:00
'/_left_chat_member'*)
2021-03-03 14:33:40 +00:00
[ -n "${REPORT_LEFTMEMBER}" ] && send_normal_message "${BOTADMIN}"\
2020-08-04 06:31:38 +00:00
"Left member: ${CHAT[TITLE]} (${CHAT[ID]}): ${LEFTMEMBER[FIRST_NAME]} ${LEFTMEMBER[LAST_NAME]} (@${LEFTMEMBER[USERNAME]})"
2020-07-12 13:18:11 +00:00
;;
'/_migrate_group'*)
# call group migration function if provided
_exec_if_function my_migrate_group "${MIGRATE[FROM]}" "${MIGRATE[TO]}"
;;
2020-05-16 20:10:12 +00:00
esac
case "${MESSAGE}" in
2019-05-25 11:31:36 +00:00
##################
2021-02-07 19:44:58 +00:00
# example commands, replace them by your own
2021-02-10 09:38:21 +00:00
'/_dice_re'*) # dice from user received
sleep 5
local gameresult="*Congratulation ${USER[FIRST_NAME]} ${USER[LAST_NAME]}* you got *${MESSAGE[RESULT]} Points*."
2021-02-14 09:46:26 +00:00
[ -z "${FORWARD[UID]}" ] && send_markdownv2_message "${CHAT[ID]}" "${gameresult}"
2021-02-10 09:38:21 +00:00
;;
2021-02-08 16:17:44 +00:00
'/game'*) # send random dice, edit list to fit your needs
send_dice "${CHAT[ID]}" ":$(printf "slot_machine\ngame_die\ndart\nbasketball\nsoccer\nslot_machine"|sort -R|shuf -n 1shuf -n 1):"
2021-02-07 19:44:58 +00:00
if [ "${BOTSENT[OK]}" = "true" ]; then
2021-02-10 09:38:21 +00:00
local gameresult="*Congratulation ${USER[FIRST_NAME]}* ${USER[LAST_NAME]} you got *${BOTSENT[RESULT]} Points*."
2021-02-07 19:44:58 +00:00
sleep 5
case "${BOTSENT[RESULT]}" in
1) gameresult="*Sorry* only *one Point* ...";;
2) gameresult="*Hey*, 2 Points are *more then one!*";;
2021-02-08 16:17:44 +00:00
5|6) [[ "${BOTSENT[EMOJI]}" =~ fb0$ ]] || gameresult="*Super! ${BOTSENT[RESULT]} Points!*";;
6*) gameresult="*JACKPOT! ${BOTSENT[RESULT]} Points!*";;
2021-02-07 19:44:58 +00:00
esac
send_markdownv2_message "${CHAT[ID]}" "${gameresult}"
fi
;;
'/unpin'*) # unpin all messages if (bot)admin or allowed for user
user_is_allowed "${USER[ID]}" "unpin" "${CHAT[ID]}" &&\
unpinall_chat_messages "${CHAT[ID]}"
;;
'/echo'*) # example echo command
2021-01-04 22:08:09 +00:00
send_normal_message "${CHAT[ID]}" "${MESSAGE}"
;;
2021-01-27 10:54:48 +00:00
'/button'*)# inline button, set CALLBACK=1 for processing callbacks
send_inline_buttons "${CHAT[ID]}" "Press Button ..." " Button |RANDOM-BUTTON"
;;
2019-04-27 11:36:32 +00:00
'/question'*) # start interactive questions
checkproc
2021-01-04 22:08:09 +00:00
if [ "${res}" -gt 0 ] ; then
startproc "examples/question.sh" || send_normal_message "${CHAT[ID]}" "Can't start question."
else
2021-01-04 22:08:09 +00:00
send_normal_message "${CHAT[ID]}" "${MESSAGE} already running ..."
fi
;;
'/cancel'*) # cancel interactive command
checkproc
2021-01-04 22:08:09 +00:00
if [ "${res}" -gt 0 ] ;then
killproc && send_normal_message "${CHAT[ID]}" "Command canceled."
else
send_normal_message "${CHAT[ID]}" "No command is currently running."
fi
;;
2019-05-01 17:21:57 +00:00
'/run_notify'*) # start notify background job
2021-01-04 22:08:09 +00:00
myback="notify"; checkback "${myback}"
if [ "${res}" -gt 0 ] ; then
background "examples/notify.sh 60" "${myback}" || send_normal_message "${CHAT[ID]}" "Can't start notify."
else
2021-01-04 22:08:09 +00:00
send_normal_message "${CHAT[ID]}" "Background command ${myback} already running ..."
fi
;;
2019-05-01 17:21:57 +00:00
'/stop_notify'*) # kill notify background job
2021-01-04 22:08:09 +00:00
myback="notify"; checkback "${myback}"
if [ "${res}" -eq 0 ] ; then
killback "${myback}"
send_normal_message "${CHAT[ID]}" "Background command ${myback} canceled."
else
2021-01-04 22:08:09 +00:00
send_normal_message "${CHAT[ID]}" "No background command ${myback} is currently running.."
fi
;;
2019-05-25 11:31:36 +00:00
##########
# command overwrite examples
'/info'*) # output date in front of regular info
2019-05-25 11:31:36 +00:00
send_normal_message "${CHAT[ID]}" "$(date)"
return 0
;;
'/kickme'*) # this will replace the /kickme command
2020-12-14 15:26:27 +00:00
send_markdownv2_mesage "${CHAT[ID]}" "This bot will *not* kick you!"
2019-05-25 11:31:36 +00:00
return 1
;;
esac
2019-05-02 10:33:10 +00:00
}
mycallbacks() {
#######################
2021-01-25 15:49:15 +00:00
# callbacks from buttons attached to messages will be processed here
# no standard use case for processing callbacks, let's log them for some users and chats
case "${iBUTTON[USER_ID]}+${iBUTTON[CHAT_ID]}" in
'USERID1+'*) # do something for all callbacks from USER
printf "%s: U=%s C=%s D=%s\n" "$(date)" "${iBUTTON[USER_ID]}" "${iBUTTON[CHAT_ID]}" "${iBUTTON[DATA]}"\
>>"${DATADIR}/${iBUTTON[USER_ID]}.log"
answer_callback_query "${iBUTTON[ID]}" "Request has been logged in your user log..."
return
;;
*'+CHATID1') # do something for all callbacks from CHAT
printf "%s: U=%s C=%s D=%s\n" "$(date)" "${iBUTTON[USER_ID]}" "${iBUTTON[CHAT_ID]}" "${iBUTTON[DATA]}"\
>>"${DATADIR}/${iBUTTON[CHAT_ID]}.log"
answer_callback_query "${iBUTTON[ID]}" "Request has been logged in chat log..."
return
;;
'USERID2+CHATID2') # do something only for callbacks form USER in CHAT
printf "%s: U=%s C=%s D=%s\n" "$(date)" "${iBUTTON[USER_ID]}" "${iBUTTON[CHAT_ID]}" "${iBUTTON[DATA]}"\
>>"${DATADIR}/${iBUTTON[USER_ID]}-${iBUTTON[CHAT_ID]}.log"
answer_callback_query "${iBUTTON[ID]}" "Request has been logged in user-chat log..."
return
;;
*) # all other callbacks are processed here
2021-01-27 10:54:48 +00:00
local callback_answer
2021-01-26 19:59:10 +00:00
# your processing here ...
2021-01-27 10:54:48 +00:00
# message available?
2021-01-26 19:59:10 +00:00
if [[ -n "${iBUTTON[CHAT_ID]}" && -n "${iBUTTON[MESSAGE_ID]}" ]]; then
2021-01-27 10:54:48 +00:00
if [ "${iBUTTON[DATA]}" = "RANDOM-BUTTON" ]; then
callback_answer="Button pressed"
edit_inline_buttons "${iBUTTON[CHAT_ID]}" "${iBUTTON[MESSAGE_ID]}" "Button ${RANDOM}|RANDOM-BUTTON"
fi
else
callback_answer="Button to old, sorry."
2021-01-26 19:59:10 +00:00
fi
# Telegram needs an ack each callback query, default empty
2021-01-27 10:54:48 +00:00
answer_callback_query "${iBUTTON[ID]}" "${callback_answer}"
;;
esac
}
2019-05-09 21:34:45 +00:00
myinlines() {
#######################
2020-06-23 14:35:50 +00:00
# Inline query examples, do not use them in production (except image search ;-)
2019-05-09 21:34:45 +00:00
# shellcheck disable=SC2128
2019-05-10 19:38:25 +00:00
iQUERY="${iQUERY,,}" # all lowercase
2019-05-09 21:34:45 +00:00
case "${iQUERY}" in
2019-05-10 16:50:30 +00:00
"image "*) # search images with yahoo
2019-05-09 21:34:45 +00:00
local search="${iQUERY#* }"
answer_inline_multi "${iQUERY[ID]}" "$(my_image_search "${search}")"
;;
2019-05-10 16:50:30 +00:00
"0"*) # a single message with title
answer_inline_query "${iQUERY[ID]}" "message" "Title of the result" "Content of the message to be sent"
;;
"1"*) # a single photo
answer_inline_query "${iQUERY[ID]}" "photo" "https://avatars.githubusercontent.com/u/13046303" "https://avatars.githubusercontent.com/u/13046303"
;;
"2"*) # two photos
2019-05-09 21:34:45 +00:00
answer_inline_multi "${iQUERY[ID]}" "
2021-01-04 22:08:09 +00:00
$(inline_query_compose "${RANDOM}" "photo" "https://avatars.githubusercontent.com/u/13046303"),
$(inline_query_compose "${RANDOM}" "photo" "https://avatars.githubusercontent.com/u/4593242")
2019-05-09 21:34:45 +00:00
"
;;
2019-05-10 16:50:30 +00:00
"3"*) # three photos
answer_inline_multi "${iQUERY[ID]}" "
2021-01-04 22:08:09 +00:00
$(inline_query_compose "${RANDOM}" "photo" "https://avatars.githubusercontent.com/u/13046303"),
$(inline_query_compose "${RANDOM}" "photo" "https://avatars.githubusercontent.com/u/4593242")
$(inline_query_compose "${RANDOM}" "photo" "https://avatars.githubusercontent.com/u/102707")
2019-05-10 16:50:30 +00:00
"
;;
2019-05-09 21:34:45 +00:00
2020-08-01 07:10:14 +00:00
"4") # four photos from array
2019-05-10 12:56:53 +00:00
local sep=""
local avatar=("https://avatars.githubusercontent.com/u/13046303" "https://avatars.githubusercontent.com/u/4593242" "https://avatars.githubusercontent.com/u/102707" "https://avatars.githubusercontent.com/u/6460407")
answer_inline_multi "${iQUERY[ID]}" "
$(for photo in ${avatar[*]} ; do
2021-01-04 22:08:09 +00:00
printf "%s\n" "${sep}"; inline_query_compose "${RANDOM}" "photo" "${photo}" "${photo}"; sep=","
2019-05-10 12:56:53 +00:00
done)
"
;;
2020-08-01 07:10:14 +00:00
"sticker") # example cached telegram sticker
2019-05-09 21:34:45 +00:00
answer_inline_query "${iQUERY[ID]}" "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC"
;;
"gif") # example cached gif
2019-05-09 21:34:45 +00:00
answer_inline_query "${iQUERY[ID]}" "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC"
;;
esac
}
# debug function called on start, stop of bot, interactive and background processes
# if your bot was started with debug as second argument
2020-07-02 15:13:09 +00:00
# $1 current date, $2 from where the function was called, $3 ... $n optional information
my_debug_checks() {
2020-07-02 15:13:09 +00:00
# example check because my bot created a wrong file
[ -f ".jssh" ] && printf "%s: %s\n" "$1" "Ups, found file \"${PWD:-.}/.jssh\"! =========="
}
###########################
# example recover from telegram block function
2020-08-01 07:10:14 +00:00
# called when bashbot send_xxx command failed because we can not connect to telegram
# return 0 to retry, return non 0 to give up
bashbotBlockRecover() {
2020-07-12 11:52:03 +00:00
# place your commands to unblock here, e.g. change IP or simply wait
2020-12-14 13:00:23 +00:00
sleep 60 # may be temporary
# check connection working
[ -n "$(getJson "${ME_URL}")" ] && return 0
return 1
}
###########################
# example error processing
# called when delete Message failed
# func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args
bashbotError_delete_message() {
log_debug "custom errorProcessing delete_message: ERR=$2 CHAT=$3 MSGID=$6 ERTXT=$5"
}
# called when error 403 is returned (and no func processing)
# func="$1" err="$2" chat="$3" user="$4" emsg="$5" remaining args
bashbotError_403() {
log_debug "custom errorProcessing error 403: FUNC=$1 CHAT=$3 USER=${4:-no-user} MSGID=$6 ERTXT=$5"
2021-04-04 09:22:34 +00:00
local user="$4"; [[ -z "$4" && -n "$3" ]] && user="$3"
if [ -n "${user}" ]; then
# block chat/user
case "$6" in
*"blocked"*)
jssh_insertKeyDB "${user}" "User blocked bot on (LANG=C date)" "${BLOCKEDFILE}";;
*"kicked"*)
jssh_insertKeyDB "${user}" "Bot kicked from chat on (LANG=C date)" "${BLOCKEDFILE}";;
*)
jssh_insertKeyDB "${user}" "Reason: $6 on (LANG=C date)" "${BLOCKEDFILE}";;
esac
fi
}
2021-04-04 09:22:34 +00:00
###########################
# place your processing functions here --------------
2019-05-09 21:34:45 +00:00
# $1 search parameter
my_image_search(){
2019-05-10 13:58:19 +00:00
local image result sep="" count="1"
result="$(wget --user-agent 'Mozilla/5.0' -qO - "https://images.search.yahoo.com/search/images?p=$1" | sed 's/</\n</g' | grep "<img src=")"
2019-05-09 21:34:45 +00:00
while read -r image; do
2021-01-04 22:08:09 +00:00
[ "${count}" -gt "20" ] && break
2019-05-10 13:58:19 +00:00
image="${image#* src=\'}"; image="${image%%&pid=*}"
[[ "${image}" = *"src="* ]] && continue
2021-01-04 22:08:09 +00:00
printf "%s\n" "${sep}"; inline_query_compose "${RANDOM}" "photo" "${image}"; sep=","
2019-05-10 13:58:19 +00:00
count=$(( count + 1 ))
2019-05-09 21:34:45 +00:00
done <<<"${result}"
}
2019-05-02 10:33:10 +00:00
fi