예제 #1
0
        def __call__(*args, **kwargs):
            func = args[0]
            if func is not None:
                if isinstance(func, Callable):
                    name = (
                        func.__name__ if hasattr(func, "__name__") else
                        f"{func.__class__.__module__}.{func.__class__.__name__}"
                    )
                    inspect_result: inspect.FullArgSpec = inspect.getfullargspec(
                        func)
                    if inspect_result is not None and len(
                            inspect_result.args) != 2:
                        actual_args = ", ".join(inspect_result.args)
                        error = f"The listener '{name}' must accept two args: client, event (actual: {actual_args})"
                        raise SlackClientError(error)

                    def new_message_listener(_self, event: dict):
                        actual_event_type = event.get("type")
                        if event.get("bot_id") == self.bot_id:
                            # SKip the events generated by this bot user
                            return
                        # https://github.com/slackapi/python-slack-sdk/issues/533
                        if event_type == "*" or (actual_event_type is not None
                                                 and actual_event_type
                                                 == event_type):
                            func(_self, event)

                    self.message_listeners.append(new_message_listener)
                else:
                    error = f"The listener '{func}' is not a Callable (actual: {type(func).__name__})"
                    raise SlackClientError(error)
            # Not to cause modification to the decorated method
            return func
예제 #2
0
 def send(self, payload: Union[dict, str]) -> None:
     if payload is None:
         return
     if self.current_session is None or not self.current_session.is_active(
     ):
         raise SlackClientError(
             "The RTM client is not connected to the Slack servers")
     if isinstance(payload, str):
         self.current_session.send(payload)
     else:
         self.current_session.send(json.dumps(payload))
예제 #3
0
def send(
    webhook_url: str,
    header: Optional[str],
    message: Optional[str],
    fields: Sequence[str],
    footer: Optional[str],
    color: Optional[str],
) -> None:
    webhook = WebhookClient(url=webhook_url)
    attachments = Attachments(
        color=color,
        header=header,
        message=message,
        fields=fields,
        footer=footer,
    )
    response = webhook.send(attachments=attachments.to_payload())
    status_code, body = response.status_code, response.body
    if status_code != 200 or body != "ok":
        raise SlackClientError(
            f"Webhook request was failed - status: {status_code}, body: {body}"
        )