from slack_sdk.web.async_client import AsyncWebClient
import structlog
from src.mcp.tools.verify_claim import verify_claim
from src.integrations.slack.blocks import format_verification_blocks
from src.config import get_settings
logger = structlog.get_logger()
settings = get_settings()
async def handle_message_event(client: AsyncWebClient, event: dict):
"""Handle Slack message events."""
event_type = event.get("type")
if event_type == "app_mention":
await handle_app_mention(client, event)
elif event_type == "message":
await handle_channel_message(client, event)
async def handle_app_mention(client: AsyncWebClient, event: dict):
"""Handle when bot is mentioned."""
channel = event.get("channel")
text = event.get("text", "")
user = event.get("user")
# Remove bot mention from text
cleaned_text = text.split(">", 1)[-1].strip()
logger.info("app_mention", user=user, text=cleaned_text)
# Check if this looks like a claim
if any(keyword in cleaned_text.lower() for keyword in ['verify', 'check', 'claim']):
# Extract the claim
claim_text = cleaned_text
for prefix in ['verify', 'check', 'claim']:
claim_text = claim_text.lower().replace(prefix, '').strip()
if claim_text:
# Send "processing" message
await client.chat_postMessage(
channel=channel,
text=f"🔍 Verifying claim: _{claim_text}_"
)
# Verify the claim
result = await verify_claim({'claim_text': claim_text})
# Format and send result
blocks = format_verification_blocks(result)
await client.chat_postMessage(
channel=channel,
text=f"Verification complete: {result.get('verdict')}",
blocks=blocks
)
else:
await client.chat_postMessage(
channel=channel,
text="Hi! I can verify claims against CME prediction market data. Try mentioning me with a claim to verify!"
)
async def handle_channel_message(client: AsyncWebClient, event: dict):
"""Handle messages in monitored channels."""
channel = event.get("channel")
# Only process in verification channel
if channel != settings.SLACK_VERIFICATION_CHANNEL.lstrip('#'):
return
text = event.get("text", "")
# Skip bot messages
if event.get("bot_id"):
return
logger.info("channel_message", channel=channel, text=text)