Skip to content

Instantly share code, notes, and snippets.

@dodopontocom
Last active July 21, 2020 23:56
Show Gist options
  • Save dodopontocom/d3c6e2832ea8ab6ba7f55c7e28183006 to your computer and use it in GitHub Desktop.
Save dodopontocom/d3c6e2832ea8ab6ba7f55c7e28183006 to your computer and use it in GitHub Desktop.
Dynamic Buttons for Telegram bots
#!/usr/bin/env bash
BASEDIR="$(cd $(dirname ${BASH_SOURCE[0]}) >/dev/null 2>&1 && pwd)"
API_VERSION_RAW_URL="https://raw.githubusercontent.com/shellscriptx/shellbot/master/ShellBot.sh"
API_GIT_URL="https://github.com/shellscriptx/shellbot.git"
_UNTICKED="⚪"
_TICKED="🔘"
_OPTIONS=(pdf jpg/jpeg svg gif)
_COMMAND="${1:-test}"
exitOnError() {
code=${2:-$?}
if [[ $code -ne 0 ]]; then
if [ ! -z "$1" ]; then echo -e "ERROR: $1" >&2 ; fi
echo "Exiting..." >&2
exit $code
fi
}
helper.validate_vars() {
local vars_list=($@)
for v in $(echo ${vars_list[@]}); do
export | grep ${v} > /dev/null
result=$?
if [[ ${result} -ne 0 ]]; then
echo "Dependency of ${v} is missing"
echo "Exiting..."
exit -1
fi
done
}
helper.get_api() {
local tmp_folder
tmp_folder=$(mktemp -d)
echo "[INFO] ShellBot API - Getting the newest version"
git clone ${API_GIT_URL} ${tmp_folder} > /dev/null
echo "[INFO] Providing the API for the bot's project folder"
cp ${tmp_folder}/ShellBot.sh ${BASEDIR}/
rm -fr ${tmp_folder}
}
helper.validate_vars TELEGRAM_TOKEN
if [[ ! -f ${BASEDIR}/ShellBot.sh ]]; then
helper.get_api
exitOnError "Error trying to get API (${API_GIT_URL})" $?
fi
init.button() {
local button1 keyboard title count
title="*Select one option:*"
count=1
button1=''
for i in $(echo ${_OPTIONS[@]}); do
ShellBot.InlineKeyboardButton --button 'button1' --text "${_UNTICKED} ${i}" --callback_data "tick_${i%%/*}" --line ${count}
count=$((count+1))
done
keyboard="$(ShellBot.InlineKeyboardMarkup -b 'button1')"
ShellBot.deleteMessage --chat_id ${message_chat_id[$id]} --message_id ${message_message_id[$id]}
ShellBot.sendMessage --chat_id ${message_chat_id[$id]} \
--text "$(echo -e ${title})" \
--parse_mode markdown \
--reply_markup "$keyboard"
}
tick.button() {
local button2 keyboard2 count
count=1
button2=''
for i in $(echo ${_OPTIONS[@]}); do
if [[ "${callback_query_data[$id]}" == "tick_${i%%/*}" ]]; then
ShellBot.InlineKeyboardButton --button 'button2' --text "${_TICKED} ${i}" --callback_data "untick_${i%%/*}" --line ${count}
else
ShellBot.InlineKeyboardButton --button 'button2' --text "${_UNTICKED} ${i}" --callback_data "tick_${i%%/*}" --line ${count}
fi
count=$((count+1))
done
keyboard2="$(ShellBot.InlineKeyboardMarkup -b 'button2')"
ShellBot.answerCallbackQuery --callback_query_id ${callback_query_id[$id]} --text "ticking ${callback_query_data[$id]}..."
ShellBot.editMessageReplyMarkup --chat_id ${callback_query_message_chat_id[$id]} \
--message_id ${callback_query_message_message_id[$id]} \
--reply_markup "$keyboard2"
}
untick.button() {
local button3 keyboard3 count
count=1
button3=''
for i in $(echo ${_OPTIONS[@]}); do
ShellBot.InlineKeyboardButton --button 'button3' --text "${_UNTICKED} ${i}" --callback_data "tick_${i%%/*}" --line ${count}
count=$((count+1))
done
keyboard3="$(ShellBot.InlineKeyboardMarkup -b 'button3')"
ShellBot.answerCallbackQuery --callback_query_id ${callback_query_id[$id]} --text "unticking..."
ShellBot.editMessageReplyMarkup --chat_id ${callback_query_message_chat_id[$id]} \
--message_id ${callback_query_message_message_id[$id]} \
--reply_markup "$keyboard3"
}
source ${BASEDIR}/ShellBot.sh
ShellBot.init --token "${TELEGRAM_TOKEN}" --monitor --flush
while :
do
ShellBot.getUpdates --limit 100 --offset $(ShellBot.OffsetNext) --timeout 30
for id in $(ShellBot.ListUpdates)
do
(
ShellBot.watchHandle --callback_data ${callback_query_data[$id]}
if [[ ${message_entities_type[$id]} == bot_command ]]; then
case ${message_text[$id]} in
"/${_COMMAND}")
init.button
;;
esac
fi
for i in $(echo ${_OPTIONS[@]}); do
case ${callback_query_data[$id]} in
"tick_${i%%/*}")
tick.button
;;
"untick_${i%%/*}")
untick.button
;;
esac
done
) &
done
done
@dodopontocom
Copy link
Author

dodopontocom commented Jul 21, 2020

save it locally and execute it like this:
$ chmod +x bot.sh && TELEGRAM_TOKEN="your_token" ./bot.sh "options"

send the command bot to your bot /options

@dodopontocom
Copy link
Author

dodopontocom commented Jul 21, 2020

alternatively you can run like this:
$ export TELEGRAM_TOKEN="your_token" && wget -O - https://gist.githubusercontent.com/dodopontocom/d3c6e2832ea8ab6ba7f55c7e28183006/raw/3944a15a994eedb2a0eebce48396e4ec902eba3b/bot.sh | bash

@dodopontocom
Copy link
Author

Screenshot from 2020-07-21 20-45-49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment