telegram-bot-bash/mycommands.sh.clean

130 lines
3.9 KiB
Plaintext
Raw Normal View History

2020-05-14 12:21:15 +00:00
#!/bin/bash
#######################################################
#
# File: mycommands.sh.clean
2020-05-14 12:21:15 +00:00
#
# copy to mycommands.sh and add all your commands and functions here ...
#
# Usage: will be executed when a bot command is received
2020-05-14 12:21:15 +00:00
#
# License: WTFPLv2 http://www.wtfpl.net/txt/copying/
# Author: KayM (gnadelwartz), kay@rrr.de
#
2021-03-04 12:58:04 +00:00
#### $$VERSION$$ v1.45-dev-75-gfdb2b3a
#######################################################
# shellcheck disable=SC1117
2020-05-14 12:21:15 +00:00
####################
# Config has moved to bashbot.conf
# shellcheck source=./commands.sh
[ -r "${BASHBOT_ETC:-.}/mycommands.conf" ] && source "${BASHBOT_ETC:-.}/mycommands.conf" "$1"
2020-06-27 14:01:27 +00:00
2020-05-14 12:21:15 +00:00
##################
# lets's go
2020-05-14 13:02:17 +00:00
if [ "$1" = "startbot" ];then
###################
# this section is processed on startup
# run once after startup when the first message is received
2020-05-14 13:02:17 +00:00
my_startup(){
:
}
touch .mystartup
else
2020-08-01 07:10:14 +00:00
# call my_startup on first message after startup
# things to do only once
2020-05-14 13:02:17 +00:00
[ -f .mystartup ] && rm -f .mystartup && _exec_if_function my_startup
#############################
# your own bashbot commands
2020-08-01 07:10:14 +00:00
# NOTE: command can have @botname attached, you must add * to case tests...
2020-05-14 12:21:15 +00:00
mycommands() {
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]}" &
2021-01-27 12:35:31 +00:00
# uncommet to 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-05-14 12:21:15 +00:00
case "${MESSAGE}" in
##################
# example command, replace them by your own
'/echo'*) # example echo command
send_normal_message "${CHAT[ID]}" "${MESSAGE}"
2020-05-14 12:21:15 +00:00
;;
##########
# command overwrite examples
# return 0 -> run default command afterwards
# return 1 -> skip possible default commands
'/info'*) # output date in front of regular info
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!"
2020-05-14 12:21:15 +00:00
return 1
;;
esac
}
2021-01-26 19:59:10 +00:00
mycallbacks() {
#######################
# callbacks from buttons attached to messages will be processed here
case "${iBUTTON[USER_ID]}+${iBUTTON[CHAT_ID]}" in
*) # 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 ...
:
# Telegram needs an ack each callback query, default empty
2021-01-27 10:54:48 +00:00
answer_callback_query "${iBUTTON[ID]}" "${callback_answer}"
2021-01-26 19:59:10 +00:00
;;
esac
}
2020-05-14 12:21:15 +00:00
myinlines() {
#######################
# this fuinction is called only if you has set INLINE=1 !!
# shellcheck disable=SC2128
iQUERY="${iQUERY,,}"
case "${iQUERY}" in
##################
# example inline command, replace it by your own
"image "*) # search images with yahoo
local search="${iQUERY#* }"
answer_inline_multi "${iQUERY[ID]}" "$(my_image_search "${search}")"
;;
esac
}
#####################
# place your processing functions here
# example inline processing function, not really useful
# $1 search parameter
my_image_search(){
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=")"
while read -r image; do
[ "${count}" -gt "20" ] && break
2020-05-14 12:21:15 +00:00
image="${image#* src=\'}"; image="${image%%&pid=*}"
[[ "${image}" = *"src="* ]] && continue
printf "%s\n" "${sep}"; inline_query_compose "${RANDOM}" "photo" "${image}"; sep=","
2020-05-14 12:21:15 +00:00
count=$(( count + 1 ))
done <<<"${result}"
}
fi