From 7bad01b25812551feb53711843403f56c63725f2 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 25 May 2019 17:02:25 +0200 Subject: [PATCH] fix tests, start addon implementation --- addons/example.sh | 41 +++++++++++++++++++ bashbot.sh | 17 +++++++- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 13 +++--- dev/make-standalone.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- .../d-send_message-test.result | 38 ++++++++--------- 12 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 addons/example.sh diff --git a/addons/example.sh b/addons/example.sh new file mode 100644 index 0000000..ec47c73 --- /dev/null +++ b/addons/example.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# file: addons/example.sh.dist +# +# Addons can register to bashbot events at statup +# by providing their name and a callback per event +# +# If an event occours a subprocess is spawned and call +# all registered event functions. + +# Availible events: +# +# BASBOT_EVENT_INLINE inline query received +# BASBOT_EVENT_MESSAGE any type of message received +# BASBOT_EVENT_REPLY reply to message received +# BASBOT_EVENT_FORWARD forwarded message received +# BASBOT_EVENT_CONTACT contact received +# BASBOT_EVENT_LOCATION location or venue received +# BASBOT_EVENT_FILE file received +# +# all global variables and functions can be used. + +# any global variable defined by addons MUST be prefixed by addon name +# funtion local varibales can have an name +EXAMPLE_ME="example" + +# register to inline +export BASBOT_EVENT_INLINE["${EXAMPLE_ME}"]="${EXAMPLE_ME}_inline" + +# any function defined by addons MUST be prefixed by addon name +example_inline(){ + send_normal_message "${CHAT[ID]}" "Inline query received: ${MESSAGE}" +} + +# register to reply +export BASBOT_EVENT_REPLY["${EXAMPLE_ME}"]="${EXAMPLE_ME}_reply" + +# any function defined by addons MUST be prefixed by addon name +example_reply(){ + send_markdown_message "${CHAT[ID]}" "User *${USER[USERNAME]}* replied to message from *${REPLYTO[USERNAME]}*" +} + diff --git a/bashbot.sh b/bashbot.sh index 1d1fd13..13fae3d 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,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-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # # Exit Codes: # - 0 sucess (hopefully) @@ -341,6 +341,7 @@ process_inline() { iQUERY[FIRST_NAME]="$(JsonDecode "${UPD["result",${num},"inline_query","from","first_name"]}")" iQUERY[LAST_NAME]="$(JsonDecode "${UPD["result",${num},"inline_query","from","last_name"]}")" iQUERY[USERNAME]="$(JsonDecode "${UPD["result",${num},"inline_query","from","username"]}")" + # event_inline & } process_message() { local num="$1" @@ -420,7 +421,21 @@ process_message() { # Location LOCATION[LONGITUDE]="${UPD["result",${num},"message","location","longitude"]}" LOCATION[LATITUDE]="${UPD["result",${num},"message","location","latitude"]}" + # event_message & } +declare -A BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE +export BASBOT_EVENT_INLINE BASBOT_EVENT_MESSAGE BASBOT_EVENT_REPLY BASBOT_EVENT_FORWARD BASBOT_EVENT_CONTACT BASBOT_EVENT_LOCATION BASBOT_EVENT_FILE + +# event_inline() { +#} +# event_message() { +# # ${REPLYTO[*]} event_reply +# # ${FORWARD[*]} event_forward +# # ${CONTACT[*]} event_contact +# # ${VENUE[*]} event_location +# # ${LOCALTION[*]} event_location +# # ${URL[*]} event_file +#} ######################### diff --git a/dev/all-tests.sh b/dev/all-tests.sh index e261d61..2c53685 100755 --- a/dev/all-tests.sh +++ b/dev/all-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/git-add.sh b/dev/git-add.sh index b972143..520c235 100755 --- a/dev/git-add.sh +++ b/dev/git-add.sh @@ -3,7 +3,7 @@ # # works together with git pre-push.sh and ADD all changed files since last push -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/hooks/pre-commit.sh b/dev/hooks/pre-commit.sh index 4d52e09..d755d32 100755 --- a/dev/hooks/pre-commit.sh +++ b/dev/hooks/pre-commit.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/hooks/pre-push.sh b/dev/hooks/pre-push.sh index b2c38b1..26f65d3 100755 --- a/dev/hooks/pre-push.sh +++ b/dev/hooks/pre-push.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf ############ # NOTE: you MUST run install-hooks.sh again when updating this file! diff --git a/dev/install-hooks.sh b/dev/install-hooks.sh index 90927c7..57aa228 100755 --- a/dev/install-hooks.sh +++ b/dev/install-hooks.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/make-distribution.sh b/dev/make-distribution.sh index 8c5e700..f4bb14e 100755 --- a/dev/make-distribution.sh +++ b/dev/make-distribution.sh @@ -2,7 +2,7 @@ # file: make-distribution.sh # creates files and arcchives to dirtribute bashbot # -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script @@ -36,11 +36,14 @@ mkdir -p "${DISTDIR}" 2>/dev/null cp -r ${DISTFILES} "${DISTDIR}" cd "${DISTDIR}" || exit 1 -# additional stuff -mv "bashbot.rc" "bashbot.rc.dist" -# mv "commands.sh" "commands.sh.dist" # will be overwritten from v0.80 on -mv "mycommands.sh" "mycommands.sh.dist" +# do not overwrite on update +for file in mycommands.sh bashbot.rc addons/*.sh +do + [ "${file}" = "addons/*.sh" ] && continue + mv "${file}" "${file}.dist" +done +# dwonload JSON.sh JSONSHFILE="JSON.sh/JSON.sh" if [ ! -f "${JSONSHFILE}" ]; then mkdir "JSON.sh" 2>/dev/null diff --git a/dev/make-standalone.sh b/dev/make-standalone.sh index 5b9facd..5049198 100755 --- a/dev/make-standalone.sh +++ b/dev/make-standalone.sh @@ -5,7 +5,7 @@ # If you your bot is finished you can use make-standalone.sh to create the # the old all-in-one bashbot: bashbot.sh and commands.sh only! # -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/dev/shellcheck.files b/dev/shellcheck.files index df324a3..7ca0a9f 100644 --- a/dev/shellcheck.files +++ b/dev/shellcheck.files @@ -1,3 +1,3 @@ # list of additional files to check from shellcheck -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf bashbot.rc diff --git a/dev/version.sh b/dev/version.sh index 3ef24c6..c7e328a 100755 --- a/dev/version.sh +++ b/dev/version.sh @@ -1,6 +1,6 @@ #!/bin/bash # -#### $$VERSION$$ v0.80-18-g6b88656 +#### $$VERSION$$ v0.80-26-g9f74bcf # shellcheck disable=SC2016 # # Easy Versioning in git: diff --git a/test/d-send_message-test/d-send_message-test.result b/test/d-send_message-test/d-send_message-test.result index 3abd7e0..eb7d557 100644 --- a/test/d-send_message-test/d-send_message-test.result +++ b/test/d-send_message-test/d-send_message-test.result @@ -1,64 +1,64 @@ chat:123456 JSON:"text":"# test for text only output" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"This is a normal text" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"This is a normal text with a line break" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":" This is a HTML text","parse_mode":"html" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":" This is a HTML text with a line break","parse_mode":"html" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":" This is a *MARKDOWN* text","parse_mode":"markdown" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":" This is a *MARKDOWN* text with a line break","parse_mode":"markdown" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"# test for keyboard, file, venue output" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"Text plus keyboard will appear in chat", "reply_markup": {"keyboard": [ [ "Yep, sure" , "No, highly unlikely" ] ] , "one_time_keyboard":true} -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"latitude": la10, "longitude": lo20 -URL:https://api.telegram.org/botbashbottestscript/sendLocation +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendLocation chat:123456 JSON:"latitude": la10, "longitude": lo20, "address": "Diagon Alley N. 37", "title": "my home" -URL:https://api.telegram.org/botbashbottestscript/sendVenue +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendVenue chat:123456 JSON:"text":"# test for new inline button" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"Text plus keyboard will appear in chat", "reply_markup": {"inline_keyboard": [ [ {"text":"Button Text", "url":"https://www..."}] ]} -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"STABILO 88/240 Fineliner point 88 [https://images-na.ssl-images-amazon.com/images/I/41oypA3kmHL.l_SX300.jpg] second part of text plus newline.", "reply_markup": {"inline_keyboard": [ [ {"text":"Bei Amazon ansehen ...", "url":"https://www.amazon.de/dp/B014TN3JYW"}] ]} -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"text":"# test for sendfile" -URL:https://api.telegram.org/botbashbottestscript/sendMessage +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendMessage chat:123456 JSON:"action": "upload_photo" -URL:https://api.telegram.org/botbashbottestscript/sendChatAction +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendChatAction chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat" -URL:https://api.telegram.org/botbashbottestscript/sendPhoto +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendPhoto chat:123456 JSON:"action": "upload_document" -URL:https://api.telegram.org/botbashbottestscript/sendChatAction +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendChatAction chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat" -URL:https://api.telegram.org/botbashbottestscript/sendDocument +URL:https://my-json-server.typicode.com/topkecleon/telegram-bot-bash/getMe?bashbottestscript/sendDocument