Edits history of script submission #22229 for ' Send message to Slack (slack)'

  • python3
    from typing import TypedDict
    
    from slack_sdk import WebClient
    from slack_sdk.errors import SlackApiError
    
    class Slack(TypedDict):
        token: str
    
    def send_message_to_slack(channel_id: str, text: str, slack: Slack):
        client = WebClient(token=slack['token'])
        try:
            response = client.chat_postMessage(
                channel=channel_id,
                text=text
            )
            print(f"Message sent: {response['ts']}")
        except SlackApiError as e:
            print(f"Error sending message: {e.response['error']}")
                    
        except Exception as e:
            print(f"Failed to send Slack message: {type(e).__name__}: {str(e)}")

    Submitted by rasul kireev249 238 days ago