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84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""
Slack MCP Tool - FREE (Webhook-based)
======================================

Send messages to Slack using incoming webhooks.
FREE - only requires creating a webhook URL in Slack settings.

No paid API access needed!

Setup:
1. Go to your Slack workspace
2. Create an app at https://api.slack.com/apps
3. Enable "Incoming Webhooks"
4. Create a webhook URL for your channel
5. Set SLACK_WEBHOOK_URL environment variable

This is the FREE tier approach - no Bot Token needed.
"""

import os
import json
import logging
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
import requests

logger = logging.getLogger(__name__)


@dataclass
class SlackMessage:
    """A Slack message"""
    text: str
    blocks: Optional[List[Dict[str, Any]]] = None
    attachments: Optional[List[Dict[str, Any]]] = None


class SlackWebhookMCP:
    """
    Slack integration using FREE incoming webhooks.
    
    No paid API subscription required - just create a webhook URL.
    """
    
    def __init__(self, webhook_url: str = None):
        self.webhook_url = webhook_url or os.getenv("SLACK_WEBHOOK_URL", "")
        
    def is_configured(self) -> bool:
        """Check if Slack webhook is configured"""
        return bool(self.webhook_url)
    
    def send_message(
        self, 
        text: str,
        blocks: List[Dict[str, Any]] = None
    ) -> Dict[str, Any]:
        """
        Send a message to Slack via webhook.
        
        Args:
            text: Plain text message (required as fallback)
            blocks: Optional Block Kit blocks for rich formatting
            
        Returns:
            {"success": bool, "error": str if failed}
        """
        
        if not self.is_configured():
            logger.warning("Slack webhook not configured")
            return {
                "success": False, 
                "error": "SLACK_WEBHOOK_URL not set. Please configure it to use Slack integration."
            }
        
        payload = {"text": text}
        
        if blocks:
            payload["blocks"] = blocks
        
        try:
            response = requests.post(
                self.webhook_url,
                json=payload,
                timeout=10
            )
            
            if response.status_code == 200:
                logger.info(f"Slack message sent successfully")
                return {"success": True}
            else:
                logger.error(f"Slack error: {response.status_code} - {response.text}")
                return {"success": False, "error": response.text}
                
        except Exception as e:
            logger.error(f"Slack request error: {e}")
            return {"success": False, "error": str(e)}
    
    def send_alert(
        self,
        title: str,
        message: str,
        severity: str = "info",
        details: Dict[str, Any] = None
    ) -> Dict[str, Any]:
        """
        Send a formatted alert to Slack.
        
        Args:
            title: Alert title
            message: Alert message
            severity: "info", "warning", "error", "success"
            details: Optional key-value details to include
        """
        
        # Color based on severity
        color_map = {
            "info": "#2196F3",
            "warning": "#FFC107",
            "error": "#F44336",
            "success": "#4CAF50"
        }
        color = color_map.get(severity, "#2196F3")
        
        # Emoji based on severity
        emoji_map = {
            "info": "ℹ️",
            "warning": "⚠️",
            "error": "🚨",
            "success": "βœ…"
        }
        emoji = emoji_map.get(severity, "ℹ️")
        
        # Build blocks
        blocks = [
            {
                "type": "header",
                "text": {
                    "type": "plain_text",
                    "text": f"{emoji} {title}",
                    "emoji": True
                }
            },
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": message
                }
            }
        ]
        
        # Add details if provided
        if details:
            fields = []
            for key, value in list(details.items())[:10]:  # Limit to 10 fields
                fields.append({
                    "type": "mrkdwn",
                    "text": f"*{key}:* {value}"
                })
            
            blocks.append({
                "type": "section",
                "fields": fields
            })
        
        # Add divider
        blocks.append({"type": "divider"})
        
        return self.send_message(f"{emoji} {title}: {message}", blocks)
    
    def send_insight(
        self,
        insight_title: str,
        insight_text: str,
        metric_name: str = None,
        metric_value: str = None,
        change_percent: float = None,
        chart_url: str = None
    ) -> Dict[str, Any]:
        """
        Send a business insight to Slack with rich formatting.
        """
        
        blocks = [
            {
                "type": "header",
                "text": {
                    "type": "plain_text",
                    "text": f"πŸ“Š DataVision Insight",
                    "emoji": True
                }
            },
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": f"*{insight_title}*\n\n{insight_text}"
                }
            }
        ]
        
        # Add metric if provided
        if metric_name and metric_value:
            change_emoji = ""
            if change_percent:
                if change_percent > 0:
                    change_emoji = f"πŸ“ˆ +{change_percent:.1f}%"
                else:
                    change_emoji = f"πŸ“‰ {change_percent:.1f}%"
            
            blocks.append({
                "type": "section",
                "fields": [
                    {
                        "type": "mrkdwn",
                        "text": f"*{metric_name}*\n{metric_value}"
                    },
                    {
                        "type": "mrkdwn",
                        "text": f"*Change*\n{change_emoji}" if change_emoji else ""
                    }
                ]
            })
        
        # Add chart if provided
        if chart_url:
            blocks.append({
                "type": "image",
                "image_url": chart_url,
                "alt_text": insight_title
            })
        
        blocks.append({"type": "divider"})
        
        return self.send_message(f"πŸ“Š {insight_title}", blocks)
    
    def send_daily_summary(
        self,
        metrics: List[Dict[str, Any]],
        highlights: List[str],
        alerts: List[str] = None
    ) -> Dict[str, Any]:
        """
        Send a daily business summary to Slack.
        
        Args:
            metrics: List of {"name": str, "value": str, "change": str}
            highlights: List of key highlights
            alerts: Optional list of alerts/warnings
        """
        
        blocks = [
            {
                "type": "header",
                "text": {
                    "type": "plain_text",
                    "text": "πŸ“ˆ Daily Business Summary",
                    "emoji": True
                }
            },
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "*Key Metrics*"
                }
            }
        ]
        
        # Add metrics
        for metric in metrics[:6]:  # Limit to 6
            blocks.append({
                "type": "section",
                "fields": [
                    {
                        "type": "mrkdwn",
                        "text": f"*{metric.get('name', '')}*"
                    },
                    {
                        "type": "mrkdwn",
                        "text": f"{metric.get('value', '')} ({metric.get('change', '')})"
                    }
                ]
            })
        
        # Add highlights
        if highlights:
            highlight_text = "\n".join([f"β€’ {h}" for h in highlights[:5]])
            blocks.append({
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": f"*✨ Highlights*\n{highlight_text}"
                }
            })
        
        # Add alerts
        if alerts:
            alert_text = "\n".join([f"β€’ {a}" for a in alerts[:5]])
            blocks.append({
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": f"*⚠️ Alerts*\n{alert_text}"
                }
            })
        
        blocks.append({"type": "divider"})
        
        return self.send_message("πŸ“ˆ Daily Business Summary from DataVision", blocks)


# MCP Tool Interface
class SlackMCPTool:
    """
    MCP-compatible interface for Slack.
    """
    
    name = "slack"
    description = "Send messages to Slack channels using webhooks (FREE)"
    
    def __init__(self, webhook_url: str = None):
        self.slack = SlackWebhookMCP(webhook_url)
    
    async def execute(self, action: str, params: Dict[str, Any]) -> Dict[str, Any]:
        """
        Execute a Slack action.
        
        Actions:
        - send: Send a simple message
        - alert: Send an alert
        - insight: Send a business insight
        - summary: Send daily summary
        """
        
        if not self.slack.is_configured():
            return {
                "success": False,
                "error": "Slack not configured. Set SLACK_WEBHOOK_URL environment variable.",
                "setup_help": "Go to https://api.slack.com/apps, create an app, enable Incoming Webhooks, and copy the URL."
            }
        
        try:
            if action == "send":
                return self.slack.send_message(
                    text=params.get("text", ""),
                    blocks=params.get("blocks")
                )
            
            elif action == "alert":
                return self.slack.send_alert(
                    title=params.get("title", "Alert"),
                    message=params.get("message", ""),
                    severity=params.get("severity", "info"),
                    details=params.get("details")
                )
            
            elif action == "insight":
                return self.slack.send_insight(
                    insight_title=params.get("title", ""),
                    insight_text=params.get("text", ""),
                    metric_name=params.get("metric_name"),
                    metric_value=params.get("metric_value"),
                    change_percent=params.get("change_percent")
                )
            
            elif action == "summary":
                return self.slack.send_daily_summary(
                    metrics=params.get("metrics", []),
                    highlights=params.get("highlights", []),
                    alerts=params.get("alerts", [])
                )
            
            else:
                return {"error": f"Unknown action: {action}"}
                
        except Exception as e:
            return {"error": str(e)}


# Convenience functions
def send_to_slack(message: str, webhook_url: str = None) -> bool:
    """Quick send to Slack"""
    slack = SlackWebhookMCP(webhook_url)
    result = slack.send_message(message)
    return result.get("success", False)


def send_slack_alert(
    title: str, 
    message: str, 
    severity: str = "info",
    webhook_url: str = None
) -> bool:
    """Send an alert to Slack"""
    slack = SlackWebhookMCP(webhook_url)
    result = slack.send_alert(title, message, severity)
    return result.get("success", False)


# Test
if __name__ == "__main__":
    # Test (requires SLACK_WEBHOOK_URL to be set)
    slack = SlackWebhookMCP()
    
    if slack.is_configured():
        # Test simple message
        result = slack.send_message("πŸš€ DataVision test message!")
        print(f"Simple message: {result}")
        
        # Test alert
        result = slack.send_alert(
            title="Test Alert",
            message="This is a test alert from DataVision",
            severity="info",
            details={"Source": "Test Script", "Time": "Now"}
        )
        print(f"Alert: {result}")
    else:
        print("Slack not configured. Set SLACK_WEBHOOK_URL to test.")
        print("Note: This is FREE - just create an incoming webhook in Slack!")