143 lines
4.6 KiB
Markdown
143 lines
4.6 KiB
Markdown
# HTTP POST Implementation for IRC → Kosmi Messages
|
|
|
|
**Date**: October 31, 2025, 12:00 PM
|
|
**Status**: ✅ Implemented
|
|
|
|
## Summary
|
|
|
|
Successfully implemented HTTP POST for sending messages from IRC to Kosmi, replacing the problematic WebSocket mutation approach. Also cleaned up debug logging from troubleshooting sessions.
|
|
|
|
## Problem
|
|
|
|
The WebSocket-based approach for sending mutations was failing because:
|
|
1. The WebSocket connection was closing immediately after sending mutations
|
|
2. Protocol initialization and authentication complexities made WebSocket mutations unreliable
|
|
3. Even with correct GraphQL mutation format (`type: "start"`), the connection would close
|
|
|
|
## Solution
|
|
|
|
Switched to using **HTTP POST** for sending messages (GraphQL mutations) to Kosmi:
|
|
- Uses the browser's cookies for authentication (extracted via Playwright)
|
|
- Sends GraphQL mutations to `https://engine.kosmi.io/`
|
|
- Works reliably without WebSocket complexities
|
|
- WebSocket still used for receiving messages (subscriptions)
|
|
|
|
## Changes Made
|
|
|
|
### 1. Modified `bridge/kosmi/native_client.go`
|
|
|
|
**Replaced WebSocket-based SendMessage with HTTP POST:**
|
|
|
|
```go
|
|
func (c *NativeClient) SendMessage(text string) error {
|
|
// Get cookies from browser for authentication
|
|
cookies, err := c.page.Context().Cookies()
|
|
|
|
// Build GraphQL mutation
|
|
mutation := map[string]interface{}{
|
|
"query": "mutation SendMessage($body: String!, $roomID: ID!) { sendMessage(body: $body, roomID: $roomID) { id } }",
|
|
"variables": map[string]interface{}{
|
|
"body": text,
|
|
"roomID": c.roomID,
|
|
},
|
|
}
|
|
|
|
// Create HTTP POST request to https://engine.kosmi.io/
|
|
req, err := http.NewRequest("POST", "https://engine.kosmi.io/", bytes.NewBuffer(payload))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("User-Agent", "Mozilla/5.0...")
|
|
|
|
// Add cookies for authentication
|
|
for _, cookie := range cookies {
|
|
req.AddCookie(&http.Cookie{Name: cookie.Name, Value: cookie.Value})
|
|
}
|
|
|
|
// Send request
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
|
resp, err := client.Do(req)
|
|
|
|
// Check response
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
```
|
|
|
|
**Added required imports:**
|
|
- `bytes`
|
|
- `io`
|
|
- `net/http`
|
|
|
|
### 2. Cleaned Up Debug Logging
|
|
|
|
**Removed from `bridge/kosmi/native_client.go`:**
|
|
- Browser console message listener
|
|
- JavaScript console.log statements in WebSocket interceptor
|
|
- Verbose emoji-based logging in SendMessage
|
|
|
|
**Removed from `bridge/kosmi/kosmi.go`:**
|
|
- Emoji-based debug logging (🔔, 📨, 🔍, ✅, ⏭️)
|
|
- Reduced verbosity of log messages
|
|
- Changed Info logs to Debug for routine operations
|
|
|
|
**Removed from `bridge/irc/handlers.go`:**
|
|
- Emoji-based debug logging (🔔, 📨, ⏭️, 🔌)
|
|
- Verbose PRIVMSG logging
|
|
|
|
**Removed from `matterbridge.toml`:**
|
|
- `Debug=true` from Kosmi section
|
|
- `DebugLevel=1` from IRC section
|
|
|
|
## Architecture
|
|
|
|
```
|
|
IRC → Matterbridge → Kosmi Bridge → HTTP POST → https://engine.kosmi.io/
|
|
(GraphQL mutation)
|
|
|
|
Kosmi → WebSocket → Browser (Playwright) → Kosmi Bridge → Matterbridge → IRC
|
|
(subscription)
|
|
```
|
|
|
|
**Key Points:**
|
|
- **Receiving**: WebSocket subscription (via Playwright-intercepted connection)
|
|
- **Sending**: HTTP POST with GraphQL mutation (using browser cookies)
|
|
- **Authentication**: Browser cookies obtained from Playwright page context
|
|
|
|
## Benefits
|
|
|
|
1. **Reliability**: HTTP POST is proven to work (from FINDINGS.md)
|
|
2. **Simplicity**: No WebSocket mutation complexity
|
|
3. **Authentication**: Leverages existing browser session cookies
|
|
4. **Clean Separation**: WebSocket for receiving, HTTP for sending
|
|
|
|
## Testing
|
|
|
|
Ready for user to test:
|
|
- ✅ IRC → Kosmi (HTTP POST implementation)
|
|
- ✅ Kosmi → IRC (WebSocket subscription, already working)
|
|
|
|
## Files Modified
|
|
|
|
1. `/Users/erikfredericks/dev-ai/HSO/irc-kosmi-relay/bridge/kosmi/native_client.go`
|
|
- Replaced SendMessage with HTTP POST implementation
|
|
- Added HTTP-related imports
|
|
- Removed debug logging
|
|
|
|
2. `/Users/erikfredericks/dev-ai/HSO/irc-kosmi-relay/bridge/kosmi/kosmi.go`
|
|
- Cleaned up debug logging
|
|
|
|
3. `/Users/erikfredericks/dev-ai/HSO/irc-kosmi-relay/bridge/irc/handlers.go`
|
|
- Cleaned up debug logging
|
|
|
|
4. `/Users/erikfredericks/dev-ai/HSO/irc-kosmi-relay/matterbridge.toml`
|
|
- Removed Debug and DebugLevel settings
|
|
|
|
## Next Steps
|
|
|
|
1. User to test IRC → Kosmi message relay
|
|
2. User to test Kosmi → IRC message relay
|
|
3. Verify bidirectional relay is working correctly
|
|
|