Skip to content

Google chat

dadosfera.services.google_chat.send_notification

send_notification(url, title, message)

Send a notification message to Google Chat using a webhook URL.

This function sends a notification message to a Google Chat channel using the Google Chat webhook API. It formats the message using a predefined template and handles the HTTP request and potential errors.

PARAMETER DESCRIPTION
url

The webhook URL for the Google Chat channel. Must be a valid URL obtained from Google Chat channel configuration. Example: "https://chat.googleapis.com/v1/spaces/AAAAA_BBBBB/messages?key=xyz..."

TYPE: str

title

The title of the notification message. Should be concise and descriptive. Will be displayed prominently in the message card. Example: "Deployment Status" or "Alert: System Error"

TYPE: str

message

The main content of the notification message. Can include formatted text according to Google Chat message card format. Will be displayed in the body of the Google Chat notification. Example: "The deployment to production was successful." or "CPU usage above 90%"

TYPE: str

RETURNS DESCRIPTION
None

The function returns nothing on success. Raises an exception on failure.

TYPE: None

RAISES DESCRIPTION
RequestException

When the HTTP request to Google Chat webhook fails. Common failure cases: - Invalid webhook URL - Network connectivity issues - Rate limiting - Server errors The exception includes: - Response status code - Response body - Original error message

Example

webhook_url = "https://chat.googleapis.com/v1/spaces/..." try: ... send_notification( ... url=webhook_url, ... title="Deployment Complete", ... message="Successfully deployed version 1.2.3 to production" ... ) ... except requests.exceptions.RequestException as e: ... print(f"Failed to send notification: {e}")

Notes
  • The function uses a predefined MESSAGE_TEMPLATE which should follow the Google Chat message card format
  • Network connectivity is required to send notifications
  • The function is synchronous and will block until the request completes
  • Messages are sent with UTF-8 encoding
Dependencies
  • requests: For making HTTP requests
  • logging: For operation logging
  • MESSAGE_TEMPLATE: Global constant with message template
See Also
  • Google Chat Webhook Documentation: https://developers.google.com/chat/how-tos/webhooks
  • Message Card Format Reference: https://developers.google.com/chat/api/reference/rest/v1/spaces.messages
Source code in dadosfera/services/google_chat.py
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 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
def send_notification(url: str, title: str, message: str) -> None:
    """Send a notification message to Google Chat using a webhook URL.

    This function sends a notification message to a Google Chat channel using the Google Chat webhook API.
    It formats the message using a predefined template and handles the HTTP request and potential errors.

    Args:
        url (str): The webhook URL for the Google Chat channel.
            Must be a valid URL obtained from Google Chat channel configuration.
            Example: "https://chat.googleapis.com/v1/spaces/AAAAA_BBBBB/messages?key=xyz..."

        title (str): The title of the notification message.
            Should be concise and descriptive.
            Will be displayed prominently in the message card.
            Example: "Deployment Status" or "Alert: System Error"

        message (str): The main content of the notification message.
            Can include formatted text according to Google Chat message card format.
            Will be displayed in the body of the Google Chat notification.
            Example: "The deployment to production was successful." or "CPU usage above 90%"

    Returns:
        None: The function returns nothing on success.
            Raises an exception on failure.

    Raises:
        requests.exceptions.RequestException: When the HTTP request to Google Chat webhook fails.
            Common failure cases:
            - Invalid webhook URL
            - Network connectivity issues
            - Rate limiting
            - Server errors
            The exception includes:
            - Response status code
            - Response body
            - Original error message

    Example:
        >>> webhook_url = "https://chat.googleapis.com/v1/spaces/..."
        >>> try:
        ...     send_notification(
        ...         url=webhook_url,
        ...         title="Deployment Complete",
        ...         message="Successfully deployed version 1.2.3 to production"
        ...     )
        ... except requests.exceptions.RequestException as e:
        ...     print(f"Failed to send notification: {e}")

    Notes:
        - The function uses a predefined MESSAGE_TEMPLATE which should follow the Google Chat message card format
        - Network connectivity is required to send notifications
        - The function is synchronous and will block until the request completes
        - Messages are sent with UTF-8 encoding

    Dependencies:
        - requests: For making HTTP requests
        - logging: For operation logging
        - MESSAGE_TEMPLATE: Global constant with message template

    See Also:
        - Google Chat Webhook Documentation:
          https://developers.google.com/chat/how-tos/webhooks
        - Message Card Format Reference:
          https://developers.google.com/chat/api/reference/rest/v1/spaces.messages
    """
    headers = {
        "Content-Type": "application/json; charset=UTF-8"
    }

    payload = MESSAGE_TEMPLATE.format(
        title=title,
        message=message
    )

    try:
        response = requests.post(url, headers=headers, data=payload)
        response.raise_for_status()
        logger.info("Notificação enviada com sucesso!")
    except requests.exceptions.RequestException as e:
        logger.error("Erro ao enviar notificação")
        raise