📄 test_telegram_integration.py 3,390 bytes Apr 23, 2026 📋 Raw

import asyncio
import sys
sys.path.insert(0, '.')

from telegram_incident import handle_telegram_command, log_incident_handler, parse_incident_response, preview_incident

Mock Telegram update

mock_update = {
"message": {
"text": '/logincident "Cloudflare Error 1033" cloudflared,uvicorn,systemd "Error 1033: Can\'t reach origin"',
"chat": {"id": 8386527252},
"from": {"id": 8386527252, "username": "Hoffmann_Matthew"}
}
}

async def test_telegram_command():
"""Test the full Telegram command flow."""
print("=" * 70)
print("TELEGRAM BOT INTEGRATION TEST")
print("=" * 70)
print()

print("Command: /logincident")
print('  Title: "Cloudflare Error 1033"')
print('  Systems: cloudflared, uvicorn, systemd')
print('  Error: "Error 1033: Can\'t reach origin"')
print()

# Test command parser
print("1. Testing command parser...")
result = await handle_telegram_command(mock_update)
print(f"   ✓ Parsed successfully")
print()

# Show what would be sent to user
print("2. Bot response to user:")
print("   " + "-" * 66)
for line in result.split('\n')[:15]:
    print(f"   {line}")
if len(result.split('\n')) > 15:
    print(f"   ... ({len(result.split('\n')) - 15} more lines)")
print()

# Test response parsing
print("3. Testing response parsing...")
test_response = """Attempts:
  1. What I tried: Checked uvicorn health with curl
    What happened: Running but bound to 127.0.0.1
  2. What I tried: Added systemd drop-in for 0.0.0.0
    What happened: Still Error 1033

Fix: Removed stale override.conf
Reflection: Should have run systemctl cat first
Hours spent: 2.5
Sleep lost: yes"""

parsed = parse_incident_response(test_response)
if parsed:
    print(f"   ✓ Parsed successfully")
    print(f"     Attempts: {len(parsed['attempts'])}")
    print(f"     Fix: {parsed['fix'][:50]}...")
    print(f"     Hours: {parsed['hours']}")
    print(f"     Sleep lost: {parsed['sleep_lost']}")
else:
    print("   ✗ Parsing failed")
print()

# Test preview generation
print("4. Testing preview generation...")
preview = preview_incident({
    'title': 'Cloudflare Error 1033',
    'date': '2026-04-23T01:55:33Z',
    'systems': ['cloudflared', 'uvicorn', 'systemd'],
    'error': 'Error 1033: Cannot reach origin',
    'attempts': [
        {'attempt': 'Checked uvicorn health', 'result': 'Running but bound to 127.0.0.1'},
        {'attempt': 'Added systemd drop-in', 'result': 'Still Error 1033'},
    ],
    'fix': 'Removed stale override.conf',
    'reflection': 'Should have run systemctl cat first',
    'hours': 2.5,
    'sleep_lost': True
})
print("   " + "-" * 66)
for line in preview.split('\n'):
    print(f"   {line}")
print()

print("=" * 70)
print("INTEGRATION READY")
print("=" * 70)
print()
print("To activate in production:")
print("  1. Add webhook handler to your Telegram bot")
print("  2. Route /logincident to handle_telegram_command()")
print("  3. Store incidents in ~/.openclaw/workspace-socrates/incidents/")
print("  4. Cron job: auto-convert incidents to content briefs weekly")

if name == "main":
asyncio.run(test_telegram_command())