skip to Main Content

I want to send the contents of a file after a given line number to Slack using curl. I’d like it to be formated as code after some message. ChatGPT is failing to help here. The code I’m using is:

log_text=$(awk -v n="$some_number" 'NR >= n' "$log_file" )

# Handle line breaks and special characters:
#log_text=$(printf '%s' "$log_text" | jq -Rs .) # Doesn't work
#log_text=$(printf '%s' "$log_text" | sed 's/"/\"/g; s/$/\n/' | tr -d 'n')  # Also doesn't work
log_text=$(echo "$log_text" | jq -R -s -c)  # Doesn't work as well
echo "============================"
echo "$log_text"
echo "============================"
curl -s -f  --retry 2 -X POST -H 'Content-type: application/json' --data "{"text": " some text here  ```$log_text``` "}" 
"$slack_hook" || echo "Failed to send message"

This always fails to send the message. If I set log_text="" then it works correctly, so I’m assuming it’s some problem with the formatting of the content in log_text. The file has several special characters line breaks and so on, but I’d like to send it neatly on the json so it can be read on Slack. What is going on here?

3

Answers


  1. Chosen as BEST ANSWER

    What ended up working (not sure why):

    First duplicate slashes, then replace all newline characters with 'n':

    log_text="$(printf '%s' "$log_text" | sed 's/\/\\/g' | sed ':a;N;$!ba;s/n/\n/g' )"
    

    Then use a variable to create the whole payload, so it's easier to debug:

    json_string="{"text": " some text \n ```$log_text``` "}"
    

    Finally send the message:

    curl -s -f  --retry 2 -X POST -H 'Content-type: application/json' --data "$json_string"
    

  2. I would recommend avoiding using a template builder within the curl command and instead using a single argument.

    Instead:

    curl -s -f  --retry 2 -X POST -H 'Content-type: application/json' --data "{"text": " some text here  ```$log_text``` "}"
    

    You can do something like:

    curl -s -f  --retry 2 -X POST -H 'Content-type: application/json' --data "$log_text_2"
    

    In this case, you can print $log_text_2 to gain some insights on what went wrong. I think there may be some escape characters causing the issue.

    Login or Signup to reply.
  3. Something ChatGPT will never do for you is write working code with explanations so you learn something

    cat sendlog.sh:

    #!/usr/bin/env sh
    
    # Configures Slack API URL here
    SLACK_API=https://example.com/
    
    # Creates markdown code block from standard input
    # $1 : Optional language code
    # shellcheck disable=SC2120 # Function uses optional arguments
    md_codeBlock() {
      # Inserts content into code block, ensuring it ends with newline
      # shellcheck disable=SC2016 # Literal back-ticks intended
      printf '```%sn%sn```n' "${1:-none}" "$(cat)"
    }
    
    # Turns stdin to Slack's JSON
    slack_toJson() {
      jq -cRs '{"text":.}'
    }
    
    # Starting line as first argument with default 0 if omitted
    start_line=${1:-0}
    
    # Strips out leading non-digit signs (no +, - space...)
    start_line=${start_line##[^[:digit:]]}
    
    # Gets stdin text starting at given line number,
    tail "-n+${start_line}" |
    
      # Turns stdin text into a markdown code block
      md_codeBlock |
    
      # Turns stdin text into Slack's JSON
      slack_toJson |
    
      # Sends the piped-in JSON data to Slack API.
      curl -s -f --retry 2 
        -X POST -H 'Content-type: application/json' 
        --data-binary @- "$SLACK_API"
    

    Example sending content of somefile.log starting at line 42:

    bash sendlog.sh 42 < somefile.log
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search