Skip to content

Commit

Permalink
util: add support for sending inline buttons.
Browse files Browse the repository at this point in the history
* util.sh: add --sendwithbutton

* bot: Add .button for demonstration.
  • Loading branch information
ksauraj authored Oct 20, 2024
1 parent 450ff39 commit 2117164
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bot/button.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
button() {
tg --sendwithinlinebutton "$RET_CHAT_ID" "Press this button before 5 minute to see hidden message." "Press"
runtime="5 minute"
endtime=$(date -ud "$runtime" +%s)
while [[ $(date -u +%s) -le $endtime ]]
do
update
if [ "$CALL_BACK_QUERY_DATA" == "$CALLBACK_DATA" ]; then
echo "button was pressed"
tg --editmsg "$QUERY_SENT_CHAT_ID" "$SENT_MSG_ID" "Thanks for pressing the button. You are awsome...."
break
fi
done
}
2 changes: 2 additions & 0 deletions tgbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ source util.sh

# Source functions
source bot/all_replace.sh
source bot/button.sh
source bot/calc.sh
source bot/choice.sh
source bot/info.sh
Expand Down Expand Up @@ -71,6 +72,7 @@ while true; do

'/start'*) start | tee -a log ;;
'.all_replace'*) all_replace | tee -a log ;;
'.button'*) button & ;;
'.calc'*) calc | tee -a log ;;
'.choice'*) choice | tee -a log ;;
'.iq'*) iq | tee -a log ;;
Expand Down
19 changes: 19 additions & 0 deletions util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ tg() {
echo $RESULT | jq .
SENT_MSG_ID=$(echo "$RESULT" | jq '.result | .message_id')
;;
--sendwithinlinebutton)
shift
local CHAT_ID=$1
local MSG=$2
local BUTTON_TEXT=$3
CALLBACK_DATA=$(echo $RANDOM | md5sum | head -c 20)
local RESULT=$(curl -s -X POST "$API/sendMessage" -H "Content-Type: application/json" -d "{\"chat_id\":\"$CHAT_ID\", \"text\":\"$MSG\", \"reply_markup\": {\"inline_keyboard\": [[{\"text\":\"$BUTTON_TEXT\", \"callback_data\": \"$CALLBACK_DATA\"}]]} }")
SENT_MSG_ID=$(echo "$RESULT" | jq '.result.message_id')
QUERY_SENT_CHAT_ID=$CHAT_ID
QUERY_ID=$(echo "$RESULT" | jq '.result.message_id')
echo $RESULT | jq .
;;
esac
}

Expand Down Expand Up @@ -285,6 +297,13 @@ update() {
STICKER_EMOJI=$(echo "$FETCH" | jq -r '.message.sticker.emoji')
STICKER_FILE_ID=$(echo "$FETCH" | jq -r '.message.sticker.file_id')
STICKER_PACK_NAME=$(echo "$FETCH" | jq -r '.message.sticker.set_name')

# Callback Query
CALL_BACK_QUERY_ID=$(echo "$FETCH" | jq -r '.callback_query.id')
CALL_BACK_QUERY_FROM=$(echo "$FETCH" | jq -r '.callback_query.from')
CALL_BACK_QUERY_DATA=$(echo "$FETCH" | jq -r '.callback_query.data')
CALL_BACK_QUERY_MESSAGE=$(echo "$FETCH" | jq -r '.callback_query.message')

fi
}

Expand Down

0 comments on commit 2117164

Please sign in to comment.