Shipra
Shipra

Reputation: 1299

Display ANSI colours in a Slack Snippet

I am using Slack and Hubot to run a few commands on our servers, the stdout and stderr of these commands is uploaded to a channel as a snippet.

Some of these commands return colour outputs and are displayed in the snippet as ANSI colour codes (example: [32mHello World![0m).

Is there a way to display these colours in a Slack Snippet?

Upvotes: 8

Views: 730

Answers (1)

Carlos Gomez
Carlos Gomez

Reputation: 1

def enviarNotificacionSlack(mensaje, estado) {
    // Convert ANSI color codes to Slack message formatting
    def formattedMensaje = mensaje.replaceAll('\u001B\\[39m', '*')
                                   .replaceAll('\u001B\\[90m', '`')
                                   .replaceAll('\u001B\\[32m', '*')
                                   .replaceAll('\u001B\\[31m', '`')

    // Construct the Slack message payload
    def slackMessage = [
        channel: '#channel',
        attachments: [
            [
                fallback: mensaje,
                color: estado == 'success' ? 'good' : 'danger',
                text: formattedMensaje
            ]
        ]
    ]

    // Send the Slack notification
    slackSend(slackMessage)
}

Upvotes: 0

Related Questions