refactor inline_query_result

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-02 22:13:06 +02:00
parent 66c390e481
commit 0582a2a8fd
3 changed files with 88 additions and 76 deletions

View File

@ -5,10 +5,11 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.80-dev-3-g9bcab66
#### $$VERSION$$ v0.70-7-g66c390e
#
# shellcheck disable=SC2154
# shellcheck disable=SC2034
# shellcheck disable=SC2128
# adjust your language setting here, e.g.when run from other user or cron.
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
@ -75,24 +76,24 @@ if [ "${1}" != "source" ];then
if _is_function process_inline; then
#######################
# Inline query examples
case "$iQUERY" in
case "${iQUERY}" in
"photo")
answer_inline_query "${iQUERY[ID]}" "photo" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg"
_answer_inline "photo" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg" "http://blog.techhysahil.com/wp-content/uploads/2016/01/Bash_Scripting.jpeg"
;;
"sticker")
answer_inline_query "${iQUERY[ID]}" "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC"
_answer_inline "cached_sticker" "BQADBAAD_QEAAiSFLwABWSYyiuj-g4AC"
;;
"gif")
answer_inline_query "${iQUERY[ID]}" "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC"
_answer_inline "cached_gif" "BQADBAADIwYAAmwsDAABlIia56QGP0YC"
;;
"web")
answer_inline_query "${iQUERY[ID]}" "article" "GitHub" "http://github.com/topkecleon/telegram-bot-bash"
_answer_inline "article" "GitHub" "http://github.com/topkecleon/telegram-bot-bash"
;;
################################################
# GLOBAL commands start here, edit messages only
'/info'*)
answer_inline_query "${iQUERY[ID]}" "article" "${bashbot_info}"
_answer_inline "article" "Info" "${bashbot_info}" "markdown"
;;
*) # forward iinline query to optional dispatcher
_is_function myinlines && myinlines
@ -100,7 +101,7 @@ if [ "${1}" != "source" ];then
fi
else
case "$MESSAGE" in
case "${MESSAGE}" in
################################################
# GLOBAL commands start here, edit messages only
'/info'*)

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.80-dev-3-g9bcab66
#### $$VERSION$$ v0.70-7-g66c390e
#
# source from commands.sh to use the aliases
@ -60,3 +60,8 @@ _keyboard_yesno() {
_del_keyboard() {
remove_keyboard "${CHAT[ID]}" ""
}
# inline query
iQUERY[ID]=""
_answer_inline() {
_is_function answer_inline_query && answer_inline_query "${iQUERY[ID]}" "$@"
}

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.70-6-g5b8f2a2
#### $$VERSION$$ v0.70-7-g66c390e
# source from commands.sh to use the inline functions
@ -24,71 +24,77 @@ process_inline() {
answer_inline_query() {
local JSON
case "${2}" in
"article")
JSON='[{"type":"'$2'","id":"'$RANDOM'","title":"'$3'","message_text":"'$4'"}]'
;;
"photo")
JSON='[{"type":"'$2'","id":"'$RANDOM'","photo_url":"'$3'","thumb_url":"'$4'"}]'
;;
"gif")
JSON='[{"type":"'$2'","id":"'$RANDOM'","gif_url":"'$3'", "thumb_url":"'$4'"}]'
;;
"mpeg4_gif")
JSON='[{"type":"'$2'","id":"'$RANDOM'","mpeg4_url":"'$3'"}]'
;;
"video")
JSON='[{"type":"'$2'","id":"'$RANDOM'","video_url":"'$3'","mime_type":"'$4'","thumb_url":"'$5'","title":"'$6'"}]'
;;
"audio")
JSON='[{"type":"'$2'","id":"'$RANDOM'","audio_url":"'$3'","title":"'$4'"}]'
;;
"voice")
JSON='[{"type":"'$2'","id":"'$RANDOM'","voice_url":"'$3'","title":"'$4'"}]'
;;
"document")
JSON='[{"type":"'$2'","id":"'$RANDOM'","title":"'$3'","caption":"'$4'","document_url":"'$5'","mime_type":"'$6'"}]'
;;
"location")
JSON='[{"type":"'$2'","id":"'$RANDOM'","latitude":"'$3'","longitude":"'$4'","title":"'$5'"}]'
;;
"venue")
JSON='[{"type":"'$2'","id":"'$RANDOM'","latitude":"'$3'","longitude":"'$4'","title":"'$5'","address":"'$6'"}]'
;;
"contact")
JSON='[{"type":"'$2'","id":"'$RANDOM'","phone_number":"'$3'","first_name":"'$4'"}]'
;;
# Cached media stored in Telegram server
"cached_photo")
JSON='[{"type":"photo","id":"'$RANDOM'","photo_file_id":"'$3'"}]'
;;
"cached_gif")
JSON='[{"type":"gif","id":"'$RANDOM'","gif_file_id":"'$3'"}]'
;;
"cached_mpeg4_gif")
JSON='[{"type":"mpeg4_gif","id":"'$RANDOM'","mpeg4_file_id":"'$3'"}]'
;;
"cached_sticker")
JSON='[{"type":"sticker","id":"'$RANDOM'","sticker_file_id":"'$3'"}]'
;;
"cached_document")
JSON='[{"type":"document","id":"'$RANDOM'","title":"'$3'","document_file_id":"'$4'"}]'
;;
"cached_video")
JSON='[{"type":"video","id":"'$RANDOM'","video_file_id":"'$3'","title":"'$4'"}]'
;;
"cached_voice")
JSON='[{"type":"voice","id":"'$RANDOM'","voice_file_id":"'$3'","title":"'$4'"}]'
;;
"cached_audio")
JSON='[{"type":"audio","id":"'$RANDOM'","audio_file_id":"'$3'"}]'
;;
esac
sendJson "" '"inline_query_id": '"${1}"', "results": '"${JSON}" "${INLINE_QUERY}"
sendJson "" '"inline_query_id": '"${1}"', "results": ['"$(shift; inline_query_result "$RANDOM" "$@")"']' "${INLINE_QUERY}"
}
# $1 unique ID for answer
# remaining arguments are in the order as shown in telegram doc: https://core.telegram.org/bots/api#inlinequeryresult
inline_query_result(){
local JSON="{}"
local ID="${1}"
case "${2}" in
# user provided media
"article")
local parse=',"parse_mode":'"${5}" && [ "${5}" = "" ] && parse=""
JSON='{"type":"article","id":"'$ID'","title":"'$3'","message_text":"'$4'"'"${parse}"'}'
;;
"photo")
JSON='{"type":"photo","id":"'$ID'","photo_url":"'$3'","thumb_url":"'$4'"}'
;;
"gif")
JSON='{"type":"gif","id":"'$ID'","gif_url":"'$3'", "thumb_url":"'$4'"}'
;;
"mpeg4_gif")
JSON='{"type":"mpeg4_gif","id":"'$ID'","mpeg4_url":"'$3'"}'
;;
"video")
JSON='{"type":"video","id":"'$ID'","video_url":"'$3'","mime_type":"'$4'","thumb_url":"'$5'","title":"'$6'"}'
;;
"audio")
JSON='{"type":"audio","id":"'$ID'","audio_url":"'$3'","title":"'$4'"}'
;;
"voice")
JSON='{"type":"voice","id":"'$ID'","voice_url":"'$3'","title":"'$4'"}'
;;
"document")
JSON='{"type":"document","id":"'$ID'","title":"'$3'","caption":"'$4'","document_url":"'$5'","mime_type":"'$6'"}'
;;
"location")
JSON='{"type":"location","id":"'$ID'","latitude":"'$3'","longitude":"'$4'","title":"'$5'"}'
;;
"venue")
JSON='{"type":"venue","id":"'$ID'","latitude":"'$3'","longitude":"'$4'","title":"'$5'","address":"'$6'"}'
;;
"contact")
JSON='{"type":"contact","id":"'$ID'","phone_number":"'$3'","first_name":"'$4'"}'
;;
# Cached media stored in Telegram server
"cached_photo")
JSON='{"type":"photo","id":"'$ID'","photo_file_id":"'$3'"}'
;;
"cached_gif")
JSON='{"type":"gif","id":"'$ID'","gif_file_id":"'$3'"}'
;;
"cached_mpeg4_gif")
JSON='{"type":"mpeg4_gif","id":"'$ID'","mpeg4_file_id":"'$3'"}'
;;
"cached_sticker")
JSON='{"type":"sticker","id":"'$ID'","sticker_file_id":"'$3'"}'
;;
"cached_document")
JSON='{"type":"document","id":"'$ID'","title":"'$3'","document_file_id":"'$4'"}'
;;
"cached_video")
JSON='{"type":"video","id":"'$ID'","video_file_id":"'$3'","title":"'$4'"}'
;;
"cached_voice")
JSON='{"type":"voice","id":"'$ID'","voice_file_id":"'$3'","title":"'$4'"}'
;;
"cached_audio")
JSON='{"type":"audio","id":"'$ID'","audio_file_id":"'$3'"}'
;;
esac
echo "${JSON}"
}