]> git.corax.cc Git - toolbox-restapis/commitdiff
include/slack.sh: Add module for interacting with Slack
authorMatthias Kruk <m@m10k.eu>
Wed, 16 Feb 2022 04:07:07 +0000 (13:07 +0900)
committerMatthias Kruk <m@m10k.eu>
Wed, 16 Feb 2022 04:07:07 +0000 (13:07 +0900)
This commit adds the slack module, which implements functions for
posting messages to private conversations and channels, as well as
formatting functions for Slack's markdown flavor.

Makefile
include/slack.sh [new file with mode: 0755]

index b48929af84a910e4feba36e186207899f8b93afe..97a7db405576d642af302372ab7c6a45d5e8c098 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-PHONY = install uninstall test deb
+PHONY = install uninstall clean
 
 ifeq ($(PREFIX), )
        PREFIX = /usr
@@ -8,8 +8,6 @@ all:
 
 clean:
 
-test:
-
 install:
        chown -R root.root include
        find include -type d -exec chmod 755 {} \;
@@ -20,5 +18,6 @@ install:
 uninstall:
        rm $(DESTDIR)$(PREFIX)/share/toolbox/include/iruca.sh
        rm $(DESTDIR)$(PREFIX)/share/toolbox/include/gitlab.sh
+       rm $(DESTDIR)$(PREFIX)/share/toolbox/include/slack.sh
 
 .PHONY: $(PHONY)
diff --git a/include/slack.sh b/include/slack.sh
new file mode 100755 (executable)
index 0000000..cbe1551
--- /dev/null
@@ -0,0 +1,89 @@
+#!/bin/bash
+
+# slack.sh - Slack module for Toolbox
+# Copyright (C) 2022 Matthias Kruk
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+__init() {
+       if ! include "json"; then
+               return 1
+       fi
+
+       return 0
+}
+
+slack_chat_post_message() {
+       local token="$1"
+       local recipient="$2"
+       local text="$3"
+
+       local json
+       local resp
+
+       json=$(json_object "channel" "$recipient" \
+                          "text"    "$text")
+
+       if ! resp=$(curl -X POST --data "$json"                                   \
+                        --header "Content-Type: application/json; charset=UTF-8" \
+                        --header "Authorization: Bearer $token"                  \
+                        "https://slack.com/api/chat.postMessage" 2>&1); then
+               return 1
+       fi
+
+       return 0
+}
+
+slack_markdown_link() {
+       local url="$1"
+       local title="$2"
+
+       if ! printf '<%s|%s>' "$url" "$title"; then
+               return 1
+       fi
+
+       return 0
+}
+
+slack_markdown_quote() {
+       local quote="$1"
+
+       local line
+
+       if (( $# < 1 )); then
+               quote=$(< /dev/stdin)
+       fi
+
+       while IFS="" read -r line; do
+               if ! printf "> %s\n" "$line"; then
+                       return 1
+               fi
+       done <<< "$quote"
+
+       return 0
+}
+
+slack_markdown_code() {
+       local code="$1"
+
+       if (( $# < 1 )); then
+               code=$(< /dev/stdin)
+       fi
+
+       if ! printf '```\n%s\n```\n' "$code"; then
+               return 1
+       fi
+
+       return 0
+}