name: on-issue

on:
  issues:
    types: [ opened, closed, reopened ]


jobs:

  on-issue:
    name: On new issue
    runs-on: ubuntu-latest
    steps:
      - name: Send Telegram Message
        run: |
          msg_text='${{ github.actor }} updated an issue:
            status: ${{ github.event.issue.state }} - #${{ github.event.issue.number }} ${{ github.event.issue.title }}
            https://github.com/zaneschepke/wgtunnel/issues/${{ github.event.issue.number }}'
          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_ACTIVITY_TOPIC }}"

      - name: Send Matrix Message
        run: |
          msg_text='${{ github.actor }} updated an issue:
            status: ${{ github.event.issue.state }} - #${{ github.event.issue.number }} ${{ github.event.issue.title }}
            https://github.com/zaneschepke/wgtunnel/issues/${{ github.event.issue.number }}'
          # 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_ACTIVITY_TOPIC }}/send/m.room.message/$(date +%s)"
