import os
import requests
from llama_index.core.tools import FunctionTool
LITHOBLOCKS_API_KEY = os.environ.get("LITHOBLOCKS_API_KEY")
BASE_URL = "https://api.lithoblocks.com"
def send_slack_message(template_id: str, payload: dict) -> str:
"""Send a Slack message using a LithoBlocks template. Use when the user wants to notify someone in Slack.
template_id: The LithoBlocks template ID (e.g. from your dashboard).
payload: JSON object matching the template's placeholders (e.g. {"user": {"name": "Alice"}, "message": "Hello"}).
"""
url = f"{BASE_URL}/templates/{template_id}/send"
headers = {
"Authorization": f"Bearer {LITHOBLOCKS_API_KEY}",
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers)
if response.ok:
return f"Message sent successfully: {response.json().get('message_ts', 'ok')}"
return f"Failed to send message: {response.status_code} {response.text}"
send_slack_tool = FunctionTool.from_defaults(
fn=send_slack_message,
name="send_slack_message",
description="Send a Slack message using a LithoBlocks template. Provide template_id and payload (dict matching template placeholders).",
)