telegram-bot-bash/mycommands.sh

116 lines
3.6 KiB
Bash
Raw Normal View History

#!/bin/bash
# files: mycommands.sh.dist
# copy to mycommands.sh and add all your commands and functions here ...
#
2019-05-10 12:56:53 +00:00
#### $$VERSION$$ v0.72-dev-6-ga51f8ca
#
# shellcheck disable=SC2154
# shellcheck disable=SC2034
2019-04-24 11:40:59 +00:00
# uncomment the following lines to overwrite info and help messages
# bashbot_info='This is bashbot, the Telegram bot written entirely in bash.
#'
# bashbot_help='*Available commands*:
#'
2019-05-02 10:33:10 +00:00
if [ "$1" = "source" ];then
# Set INLINE to 1 in order to receive inline queries.
# To enable this option in your bot, send the /setinline command to @BotFather.
2019-05-02 13:14:18 +00:00
INLINE="1"
2019-05-02 10:33:10 +00:00
# Set to .* to allow sending files from all locations
FILE_REGEX='/home/user/allowed/.*'
2019-05-02 10:33:10 +00:00
else
# your additional bahsbot commands
# NOTE: command can have @botname attached, you must add * in case tests...
mycommands() {
case "$MESSAGE" in
'/echo'*) # example echo command
send_normal_message "${CHAT[ID]}" "$MESSAGE"
;;
2019-04-27 11:36:32 +00:00
'/question'*) # start interactive questions
checkproc
if [ "$res" -gt 0 ] ; then
startproc "example/question"
else
send_normal_message "${CHAT[ID]}" "$MESSAGE already running ..."
fi
;;
2019-05-01 17:21:57 +00:00
'/run_notify'*) # start notify background job
myback="notify"; checkback "$myback"
if [ "$res" -gt 0 ] ; then
background "example/notify 60" "$myback" # notify every 60 seconds
else
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
myback="notify"; checkback "$myback"
if [ "$res" -eq 0 ] ; then
killback "$myback"
send_normal_message "${CHAT[ID]}" "Background command $myback canceled."
else
send_normal_message "${CHAT[ID]}" "No background command $myback is currently running.."
fi
;;
esac
2019-05-02 10:33:10 +00:00
}
2019-05-09 21:34:45 +00:00
myinlines() {
#######################
# Inline query examples
# shellcheck disable=SC2128
case "${iQUERY}" in
"google "*) # search in google images
local search="${iQUERY#* }"
answer_inline_multi "${iQUERY[ID]}" "$(my_image_search "${search}")"
2019-05-10 12:56:53 +00:00
exit
2019-05-09 21:34:45 +00:00
;;
2019-05-10 12:56:53 +00:00
"photo") # manually provide URLs
2019-05-09 21:34:45 +00:00
answer_inline_multi "${iQUERY[ID]}" "
2019-05-10 12:56:53 +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 12:56:53 +00:00
"avatar") # read URLS from array
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
echo "${sep}"; inline_query_compose "$RANDOM" "photo" "${photo}" "${photo}"; sep=","
done)
"
;;
2019-05-09 21:34:45 +00:00
"sticker")
answer_inline_query "${iQUERY[ID]}" "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC"
;;
"gif")
answer_inline_query "${iQUERY[ID]}" "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC"
;;
"web")
answer_inline_query "${iQUERY[ID]}" "article" "GitHub" "http://github.com/topkecleon/telegram-bot-bash"
;;
esac
}
2019-05-02 10:33:10 +00:00
# place your processing functions here
2019-05-09 21:34:45 +00:00
2019-05-10 12:56:53 +00:00
# problem: google returns png :-(
2019-05-09 21:34:45 +00:00
# $1 search parameter
my_image_search(){
2019-05-10 12:56:53 +00:00
local image result sep=""
2019-05-09 21:34:45 +00:00
result="$(wget --user-agent 'Mozilla/5.0' -qO - "https://www.google.com/search?q=$1&tbm=isch" | sed 's/</\n</g' | grep '<img')"
while read -r image; do
image="${image#* src=\"}"; image="${image%%\" width=\"*}"
2019-05-10 12:56:53 +00:00
echo "${sep}"; inline_query_compose "$RANDOM" "photo" "${image}"; sep=","
2019-05-09 21:34:45 +00:00
done <<<"${result}"
}
2019-05-02 10:33:10 +00:00
fi