From 1582e47c6daaf19e671a1ee6147f446d27ec55b6 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Wed, 16 Feb 2022 13:07:07 +0900 Subject: [PATCH] include/slack.sh: Add module for interacting with Slack 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 | 5 ++- include/slack.sh | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100755 include/slack.sh diff --git a/Makefile b/Makefile index b48929a..97a7db4 100644 --- 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 index 0000000..cbe1551 --- /dev/null +++ b/include/slack.sh @@ -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 . + +__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 +} -- 2.47.3