79456715

Date: 2025-02-21 08:23:27
Score: 0.5
Natty:
Report link

Sure, you can, This is a Python Code for same:

    from slack_bolt import App
    from slack_sdk import WebClient
    from slack_bolt.adapter.socket_mode import SocketModeHandler
    import json
    import os
    from slack_sdk.errors import SlackApiError

    @app.event("app_mention")
    def handle_mention(event, say):
        """
        Handles mentions of the bot and sends a table message to the Slack channel.
        Parameters:
        - event (dict): The event data.
        - say (function): The function to send a message to the channel.
        """
        # Example data
        headers = ["Name", "Age", "Gender"]
        rows = [["Yagna","22","Male"], ["Ray","22","Male"] ]
        # Create the payload
        payload = {
            "blocks": [
                {
                    "type": "table",
                    "rows": [
                        [
                            {
                                "type": "rich_text",
                                "elements": [
                                    {
                                        "type": "rich_text_section",
                                        "elements": [
                                            {
                                                "type": "text",
                                                "text": header,
                                                "style": {
                                                    "bold": True
                                                }
                                            }
                                        ]
                                    }
                                ]
                            } for header in headers
                        ]
                    ] + [
                        [
                            {
                                "type": "rich_text",
                                "elements": [
                                    {
                                        "type": "rich_text_section",
                                        "elements": [
                                            {
                                                "type": "text",
                                                "text": cell
                                            }
                                        ]
                                    }
                                ]
                            } for cell in row
                        ] for row in rows
                    ]
                }
            ]
        }
        # Send the message to the Slack channel
        try:
            response = say(blocks=payload["blocks"])
            print("Message sent successfully:", response)
        except SlackApiError as e:
            print(f"Error sending message: {e.response['error']}")

Also, You can visit this website(Slack Block-Kit), for better understanding: https://app.slack.com/block-kit-builder/T03P1RF97KL#%7B%22blocks%22:%5B%7B%22type%22:%22table%22,%22rows%22:%5B%5B%7B%22type%22:%22rich_text%22,%22elements%22:%5B%7B%22type%22:%22rich_text_section%22,%22elements%22:%5B%7B%22type%22:%22text%22,%22text%22:%22Header%201%22,%22style%22:%7B%22bold%22:true%7D%7D%5D%7D%5D%7D,%7B%22type%22:%22rich_text%22,%22elements%22:%5B%7B%22type%22:%22rich_text_section%22,%22elements%22:%5B%7B%22type%22:%22text%22,%22text%22:%22Header%202%22,%22style%22:%7B%22bold%22:true%7D%7D%5D%7D%5D%7D%5D,%5B%7B%22type%22:%22rich_text%22,%22elements%22:%5B%7B%22type%22:%22rich_text_section%22,%22elements%22:%5B%7B%22type%22:%22text%22,%22text%22:%22Datum%201%22%7D%5D%7D%5D%7D,%7B%22type%22:%22rich_text%22,%22elements%22:%5B%7B%22type%22:%22rich_text_section%22,%22elements%22:%5B%7B%22type%22:%22text%22,%22text%22:%22Datum%202%22%7D%5D%7D%5D%7D%5D%5D%7D%5D%7D

Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Yagna Raval