name: on-publish

on:
  release:
    types: [ published ]


jobs:
  on-publish:
    name: On publish
    runs-on: ubuntu-latest
    steps:
      - name: Send Telegram Message
        run: |
          msg_text='${{ github.actor }} published a new release:
            Release: ${{ github.event.release.tag_name }}
            ${{ github.event.release.body }}
            https://github.com/zaneschepke/wgtunnel/releases/tag/${{ github.event.release.tag_name }}'
          curl -s -X POST 'https://api.telegram.org/bot${{ secrets.TELEGRAM_TOKEN }}/sendMessage' \
          -d "chat_id=${{ secrets.TELEGRAM_TO }}&text=${msg_text}&message_thread_id=${{ vars.TELEGRAM_RELEASE_TOPIC }}"

      - name: Send Matrix Message
        run: |
            msg_text='${{ github.actor }} published a new release:
              Release: ${{ github.event.release.tag_name }}
              ${{ github.event.release.body }}
              https://github.com/zaneschepke/wgtunnel/releases/tag/${{ github.event.release.tag_name }}'
            # Escape newlines and quotes for JSON
            formatted_msg=$(echo -n "$msg_text" | sed ':a;N;$ba;s/\n/\\n/g' | sed 's/"/\\"/g')
            curl -s -X POST \
              -H "Authorization: Bearer ${{ secrets.MATRIX_TOKEN }}" \
              -H "Content-Type: application/json" \
              -d '{
                "msgtype": "m.text",
                "body": "'"$formatted_msg"'"
              }' \
              "https://matrix.org/_matrix/client/v3/rooms/${{ vars.MATRIX_RELEASE_TOPIC }}/send/m.room.message/$(date +%s)"
