Skip to content

Commit

Permalink
feat: add new gotify-to-telegram plugin (#1)
Browse files Browse the repository at this point in the history
Adds a new Gotify plugin for forwarding messages to Telegram with the
following features:

- Support for forwarding messages to multiple telegram bots/chat ids
- Configurable message formatting options per bot
- Configuration via environment variables or yaml config file via Gotify
UI
  • Loading branch information
0xpetersatoshi authored Dec 27, 2024
1 parent bcbff4c commit 37e0a46
Show file tree
Hide file tree
Showing 26 changed files with 3,570 additions and 132 deletions.
7 changes: 7 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"scope-case": [2, "always", "lower-case"],
"subject-case": [2, "never", ["start-case", "pascal-case", "upper-case"]]
}
}
5 changes: 5 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TG_PLUGIN__GOTIFY_CLIENT_TOKEN=abc123
TG_PLUGIN__GOTIFY_URL="http://localhost:80"
TG_PLUGIN__LOG_LEVEL="debug"
TG_PLUGIN__TELEGRAM_DEFAULT_CHAT_IDS="1234567890,9876543210"
TG_PLUGIN__TELEGRAM_DEFAULT_BOT_TOKEN="1234567890:abcdefghijklmn"
122 changes: 122 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Build and Release

on:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 'stable'

- name: Download tools
run: make download-tools

- name: Run tests
run: make test

create-tag:
needs: test
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
new_version: ${{ steps.tag_version.outputs.new_version }}
tag: ${{ steps.tag_version.outputs.new_tag }}
supported_versions: ${{ steps.versions.outputs.versions }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Read versions file
id: versions
run: |
VERSIONS=$(cat SUPPORTED_GOTIFY_VERSIONS.txt | jq -R -s -c 'split("\n")[:-1]')
echo "versions=$VERSIONS" >> $GITHUB_OUTPUT
- name: Create Tag
id: tag_version
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: patch

build:
needs: create-tag
runs-on: ubuntu-latest
strategy:
matrix:
gotify_version: ${{ fromJson(needs.create-tag.outputs.supported_versions) }}
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 'stable'

- name: Download tools
run: make download-tools

- name: Build plugin
run: >-
make
GOTIFY_VERSION="${{ matrix.gotify_version }}"
FILE_SUFFIX="-v${{ needs.create-tag.outputs.new_version }}-for-gotify-${{ matrix.gotify_version }}"
LD_FLAGS="-X main.Version=${{ needs.create-tag.outputs.new_version }}"
build
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: plugin-${{ matrix.gotify_version }}
path: build/*.so

release:
needs:
- create-tag
- build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- name: Generate version list
id: versions
run: |
version_list=$(cat SUPPORTED_GOTIFY_VERSIONS.txt | sed 's/^/- /')
echo "version_list<<EOF" >> $GITHUB_ENV
echo "$version_list" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Download all artifacts
uses: actions/download-artifact@v3

- name: Display structure of downloaded files
run: ls -R

- name: Create Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ needs.create-tag.outputs.tag }}
name: Release ${{ needs.create-tag.outputs.tag }}
files: plugin-*/gotify-to-telegram-*.so
generate_release_notes: true
body: |
## Supported Gotify Versions
${{ env.version_list }}
## Installation
Download the appropriate plugin file for your architecture and Gotify version:
- AMD64: `gotify-to-telegram-linux-amd64-v${{ needs.create-tag.outputs.new_version }}-for-gotify-*.so`
- ARM64: `gotify-to-telegram-linux-arm64-v${{ needs.create-tag.outputs.new_version }}-for-gotify-*.so`
- ARM7: `gotify-to-telegram-linux-arm-7-v${{ needs.create-tag.outputs.new_version }}-for-gotify-*.so`
57 changes: 57 additions & 0 deletions .github/workflows/pr-title.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: PR Title Lint

on:
pull_request:
types:
- opened
- edited
- synchronize
- reopened

permissions:
pull-requests: read
statuses: write

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
types: |
feat
fix
docs
style
refactor
perf
test
build
ci
chore
revert
# Configure that a scope must always be provided
requireScope: false
# Configure additional validation for the subject based on a regex.
# This example ensures the subject starts with lowercase.
subjectPattern: ^(?![A-Z]).+$
# If `subjectPattern` is configured, you can use this property to override
# the default error message that is shown when the pattern doesn't match.
subjectPatternError: |
The subject "{subject}" found in the pull request title "{title}"
didn't match the configured pattern. Please ensure that the subject
starts with a lowercase character.
# For work-in-progress PRs you can typically use draft pull requests
# from GitHub. However, private repositories on the free plan don't have
# this option and therefore this action allows you to opt-in to using the
# special "[WIP]" prefix to indicate this state. This will avoid the
# validation of the PR title and the pull request checks remain pending.
# Note that a second check will be reported if this is enabled.
wip: true
# When using "Squash and merge" on a PR with only one commit, GitHub
# will suggest using that commit message instead of the PR title for the
# merge commit, and it's easy to commit this by mistake. Enable this option
# to also validate the commit message for one-commit PRs.
validateSingleCommit: true
24 changes: 24 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 'stable'

- name: Download tools
run: make download-tools

- name: Run tests
run: make test

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.idea
vendor
build
*.so
*.so
.DS_Store

.env

9 changes: 9 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MD013:
line_length: 120
heading_line_length: 120
code_block_line_length: 120
code_blocks: true
tables: true
headings: true
strict: false
stern: false
34 changes: 0 additions & 34 deletions .travis.yml

This file was deleted.

62 changes: 57 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
BUILDDIR=./build
GOTIFY_VERSION=master
PLUGIN_NAME=myplugin
PLUGINDIR=./plugins
GOTIFY_VERSION=v2.6.1
PLUGIN_NAME=gotify-to-telegram
PLUGIN_ENTRY=plugin.go
GO_VERSION=`cat $(BUILDDIR)/gotify-server-go-version`
DOCKER_BUILD_IMAGE=gotify/build
DOCKER_WORKDIR=/proj
DOCKER_RUN=docker run --rm -v "$$PWD/.:${DOCKER_WORKDIR}" -v "`go env GOPATH`/pkg/mod/.:/go/pkg/mod:ro" -w ${DOCKER_WORKDIR}
DOCKER_GO_BUILD=go build -mod=readonly -a -installsuffix cgo -ldflags "$$LD_FLAGS" -buildmode=plugin
GOMOD_CAP=go run github.com/gotify/plugin-api/cmd/gomod-cap

download-tools:
GO111MODULE=off go get -u github.com/gotify/plugin-api/cmd/gomod-cap
go install github.com/gotify/plugin-api/cmd/gomod-cap@latest

create-build-dir:
mkdir -p ${BUILDDIR} || true

update-go-mod: create-build-dir
wget -LO ${BUILDDIR}/gotify-server.mod https://raw.githubusercontent.com/gotify/server/${GOTIFY_VERSION}/go.mod
gomod-cap -from ${BUILDDIR}/gotify-server.mod -to go.mod
$(GOMOD_CAP) -from ${BUILDDIR}/gotify-server.mod -to go.mod
rm ${BUILDDIR}/gotify-server.mod || true
go mod tidy

Expand All @@ -35,4 +37,54 @@ build-linux-arm64: get-gotify-server-go-version update-go-mod

build: build-linux-arm-7 build-linux-amd64 build-linux-arm64

.PHONY: build
check-env:
@if [ ! -f .env ]; then \
echo "Creating .env from .example.env..."; \
cp .example.env .env; \
fi

compose-up: check-env
docker compose up -d

compose-down:
docker compose down --volumes

test:
go test -v ./...

create-plugin-dir:
mkdir -p ${PLUGINDIR}

move-plugin-arm64: create-plugin-dir build-linux-arm64
cp ${BUILDDIR}/${PLUGIN_NAME}-linux-arm64${FILE_SUFFIX}.so ${PLUGINDIR}

move-plugin-amd64: create-plugin-dir build-linux-amd64
cp ${BUILDDIR}/${PLUGIN_NAME}-linux-amd64${FILE_SUFFIX}.so ${PLUGINDIR}

setup-gotify: compose-up
@echo "Setting up Gotify..."
@for i in 1 2 3 4 5; do \
echo "Attempt $$i of 5..."; \
sleep 5; \
NEW_TOKEN=$$(curl -s -f -X POST \
-u admin:admin \
-H "Content-Type: application/json" \
-d '{"name":"test-client"}' \
http://localhost:8888/client \
| jq -r '.token'); \
if [ -n "$$NEW_TOKEN" ]; then \
sed -i '' 's/^TG_PLUGIN__GOTIFY_CLIENT_TOKEN=.*/TG_PLUGIN__GOTIFY_CLIENT_TOKEN='$$NEW_TOKEN'/' .env && \
echo "TG_PLUGIN__GOTIFY_CLIENT_TOKEN updated in .env. Restarting gotify..." && \
docker compose down && docker compose up -d && \
exit 0; \
fi; \
echo "Attempt $$i failed. Retrying..."; \
done; \
echo "Failed to get token from Gotify after 5 attempts"; \
exit 1

test-plugin-arm64: move-plugin-arm64 setup-gotify

test-plugin-amd64: move-plugin-amd64 setup-gotify

.PHONY: build check-env compose-up compose-down test
Loading

0 comments on commit 37e0a46

Please sign in to comment.