diff --git a/app/main.py b/app/main.py index 806a274..899208f 100644 --- a/app/main.py +++ b/app/main.py @@ -23,16 +23,14 @@ from collections import defaultdict import json from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Form -# from fastapi.responses import FileResponse, HTMLResponse from fastapi.exceptions import HTTPException from fastapi.staticfiles import StaticFiles from starlette.middleware.cors import CORSMiddleware -# Change from dict[str, WebSocket] to dict[str, set[WebSocket]] active_connections: dict[str, set[WebSocket]] = defaultdict(set) app = FastAPI( - version="1.1.0-beta-v3", + version="1.1.0-beta-v4", docs_url=None, # Disable Swagger UI redoc_url=None # Disable ReDoc ) diff --git a/static/index.html b/static/index.html index 88f7b6b..04f9771 100644 --- a/static/index.html +++ b/static/index.html @@ -89,16 +89,6 @@ opacity: 1; } - .copy-button svg { - width: 1.25rem; - height: 1.25rem; - fill: currentColor; - } - - .copy-button.copied svg { - fill: #4ade80; - } - /* JSON Editor styles */ .jsoneditor { border: none !important; @@ -115,27 +105,31 @@ -
-Instant notification delivery via WebSocket connections
Token-based authentication for secure communications
Compatible with all Ko-fi event types
Simple to integrate with any WebSocket client
@@ -182,15 +181,11 @@Connect to the WebSocket endpoint using your verification token:
-{{ WSprotocol }}//{{ HOSTNAME }}/ws/{verification_token}
- In your Ko-fi settings, set your Webhook URL to:
-{{ HTTPprotocol }}//{{ HOSTNAME }}/webhook
- Connect to the WebSocket using your verification token:
-const ws = new WebSocket('{{ WSprotocol }}//{{ HOSTNAME }}/ws/YOUR_VERIFICATION_TOKEN');
- Example code to handle Ko-fi notifications:
-ws.onmessage = (event) => {
- const data = JSON.parse(event.data);
- switch(data.type) {
- case "Donation":
- console.log(`New donation from ${data.from_name}: ${data.amount} ${data.currency}`);
- break;
- case "Subscription":
- console.log(`New subscriber: ${data.from_name}`);
- break;
- case "Shop Order":
- console.log(`New shop order from ${data.from_name}`);
- break;
- }
-};
-
- Here's a complete, fully functional, example that connects on page load:
-<!-- Add this to your HTML -->
-<div id="kofi-notifications"></div>
-
-<!-- Add this to your JavaScript file or <script> tag -->
-<script>
- document.addEventListener('DOMContentLoaded', () => {
- class KofiWebSocket {
- constructor(token) {
- this.token = token;
- this.connect();
- }
-
- connect() {
- this.ws = new WebSocket(`{{ WSprotocol }}//{{ HOSTNAME }}/ws/${this.token}`);
-
- this.ws.onopen = () => {
- console.log('Connected to Ko-fi WebSocket');
- this.showNotification('Connected to Ko-fi WebSocket', 'success');
- };
-
- this.ws.onmessage = (event) => {
- // Create and dispatch a custom event
- const kofiEvent = new CustomEvent('onEventReceived', {
- detail: {
- event: JSON.parse(event.data)
- }
- });
- switch (kofiEvent.detail.event.type) {
- case "Donation":
- kofiEvent.detail.listener = "kofi-donation-latest";
- break;
- case "Subscription":
- kofiEvent.detail.listener = "kofi-subscription-latest";
- break;
- case "Shop Order":
- kofiEvent.detail.listener = "kofi-order-latest";
- break;
- default:
- kofiEvent.detail.listener = `kofi-${eventData.type.toLowerCase()}-latest`;
- }
- document.dispatchEvent(kofiEvent);
- };
-
- this.ws.onclose = () => {
- console.log('Disconnected from Ko-fi WebSocket');
- this.showNotification('Connection lost, reconnecting...', 'error');
- // Reconnect after 5 seconds
- setTimeout(() => this.connect(), 5000);
- };
-
- this.ws.onerror = (error) => {
- console.error('WebSocket error:', error);
- this.showNotification('Connection error', 'error');
- };
- }
-
- showNotification(message, type = 'info') {
- const notification = document.createElement('div');
- notification.className = `kofi-notification ${type}`;
- notification.innerHTML = `
- <p>${message}</p>
- `;
-
- document.getElementById('kofi-notifications').appendChild(notification);
- setTimeout(() => notification.remove(), 5000);
- }
- }
-
- // Initialize on page load with your token
- const kofi = new KofiWebSocket('YOUR_VERIFICATION_TOKEN');
-
- // Handle Ko-fi events
- document.addEventListener('onEventReceived', (event) => {
- const data = event.detail.event;
- let message = '';
- console.log(data);
- switch (event.detail.listener) {
- case "kofi-donation-latest":
- message = `${data.from_name} donated ${data.amount} ${data.currency} <br> ${data.message}`;
- break;
- case "kofi-subscription-latest":
- if (data.is_first_subscription_payment) {
- message = `${data.from_name} subscribed! <br> ${data.message}`;
- }
- else {
- message = `${data.from_name} renewed their ${data.tier_name} tier subscription!`;
- }
- break;
- case "kofi-order-latest":
- message = `${data.from_name} placed a ${data.amount} ${data.currency} order, what a champ!`;
- break;
- default:
- message = `New event from ${data.from_name}`;
- }
-
- kofi.showNotification(message, 'info');
- });
- });
-</script>
-
-