From 0582a2a8fd4323f1025f6ae8c5f428a30ba2d0d0 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Thu, 2 May 2019 22:13:06 +0200 Subject: [PATCH] refactor inline_query_result --- commands.sh | 17 +++--- modules/aliases.sh | 7 ++- modules/inline.sh | 140 +++++++++++++++++++++++---------------------- 3 files changed, 88 insertions(+), 76 deletions(-) diff --git a/commands.sh b/commands.sh index 2eab54a..cc818fe 100644 --- a/commands.sh +++ b/commands.sh @@ -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'*) diff --git a/modules/aliases.sh b/modules/aliases.sh index 33ba090..128ae5e 100644 --- a/modules/aliases.sh +++ b/modules/aliases.sh @@ -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]}" "$@" +} diff --git a/modules/inline.sh b/modules/inline.sh index 37f7feb..2193df2 100644 --- a/modules/inline.sh +++ b/modules/inline.sh @@ -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}" }