Great catch! Splunk SPL does not support urlencode() natively, and you can't use { directly in SPL without a command. Let's clarify how to do this in Splunk for a Google Chat webhook.
1. Build the JSON payload as a string
You can use eval to build the JSON string. You must escape double quotes inside the string with a backslash (\). You can use single quotes for the outer SPL string, and double quotes inside the JSON.
Example SPL:
| makeresults
| eval payload="{\"text\": \"Alert from Splunk\", \"cards\": [{\"sections\": [{\"widgets\": [{\"buttons\": [{\"textButton\": {\"text\": \"View in Splunk\", \"onClick\": {\"openLink\": {\"url\": \"https://your-splunk-instance/app/search/search?q=your-search-query\"}}}}]}]}]}]}"
- Replace
https://your-splunk-instance/app/search/search?q=your-search-query with your actual URL.
- If you need to encode the URL, do it outside of SPL (manually or with a script), since SPL doesn't have a built-in
urlencode().
2. Set the HTTP Header
Set the header as:
{"Content-Type": "application/json"}
3. Send the payload
Configure your alert action to use the payload field as the body of the HTTP POST.
Summary
- SPL can't do
urlencode() or use { as a command.
- Build the JSON as a string with
eval.
- Escape double quotes with
\.
- Use your webhook and header as you described.
Full Example
| makeresults
| eval payload="{\"text\": \"Alert from Splunk\", \"cards\": [{\"sections\": [{\"widgets\": [{\"buttons\": [{\"textButton\": {\"text\": \"View in Splunk\", \"onClick\": {\"openLink\": {\"url\": \"https://your-splunk-instance/app/search/search?q=your-search-query\"}}}}]}]}]}]}"
This will send a card with a clickable button to Google Chat.
If you want to dynamically insert a field (like a result URL), you can do:
| makeresults
| eval myurl="https://your-splunk-instance/app/search/search?q=your-search-query"
| eval payload="{\"text\": \"Alert from Splunk\", \"cards\": [{\"sections\": [{\"widgets\": [{\"buttons\": [{\"textButton\": {\"text\": \"View in Splunk\", \"onClick\": {\"openLink\": {\"url\": \"" . myurl . "\"}}}}]}]}]}]}"
Let me know if you need to insert dynamic fields or have more questions!