To achieve this, you'll need to use a combination of onChangeAction and a backend service to dynamically update the button's URL when the button is clicked. Google Chat cards don't support dynamically updating URLs based on input values directly within the card, so you'll need a backend to handle this.
Here's a step-by-step solution:
-
Card JSON: Use onChangeAction to send the comment to your backend when the text input changes.
-
Backend Service: Create a backend service that receives the comment, constructs the full URL, and returns an updated card with the new URL.
-
Updated Card: When the button is clicked, it will use the updated URL from the backend.
Step 1: Card JSON
{
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": "An alert has been triggered. Click below to investigate."
}
},
{
"buttonList": {
"buttons": [
{
"text": "View in Splunk",
"type": "FILLED",
"onClick": {
"action": {
"function": "openSplunkLink",
"parameters": [
{
"key": "comment",
"value": "{{comment}}"
}
],
"loadIndicator": "LOAD_INDICATOR_SPINNER"
}
}
}
]
}
},
{
"textInput": {
"name": "comment",
"label": "Komentarz",
"validation": {
"inputType": "TEXT"
},
"onChangeAction": {
"function": "updateComment",
"parameters": [
{
"key": "comment",
"value": "{{comment}}"
}
],
"loadIndicator": "LOAD_INDICATOR_SPINNER"
}
}
}
]
}
]
}
Step 2: Backend Service
You need a backend service to handle the updateComment and openSplunkLink functions. Here's a simple example using Node.js and Express:
const express = require('express');
const app = express();
app.use(express.json());
app.post('/updateComment', (req, res) => {
const comment = req.body.formInput.comment.value;
// Store the comment in a session or database
// For simplicity, we'll just return it in the response
res.json({
"cards": [
{
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": "Comment updated. Click the button to investigate."
}
},
{
"buttonList": {
"buttons": [
{
"text": "View in Splunk",
"type": "FILLED",
"onClick": {
"openLink": {
"url": `https://your-splunk-instance/app/search/search?q=your-search-query&comment=${encodeURIComponent(comment)}`
}
}
}
]
}
}
]
}
]
}
]
});
});
app.post('/openSplunkLink', (req, res) => {
const comment = req.body.formInput.comment.value;
res.json({
"cards": [
{
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": "Redirecting to Splunk..."
}
}
]
}
]
}
],
"actionResponse": {
"type": "NEW_MESSAGE",
"url": `https://your-splunk-instance/app/search/search?q=your-search-query&comment=${encodeURIComponent(comment)}`
}
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Step 3: Configure Webhook URLs
- For
updateComment, use https://your-backend-url/updateComment
- For
openSplunkLink, use https://your-backend-url/openSplunkLink
Explanation
onChangeAction: When the user types in the comment, the onChangeAction sends the comment to the /updateComment endpoint.
- Backend: The backend receives the comment, constructs the full URL, and returns an updated card with the new button URL.
onClick: When the user clicks the button, the onClick action sends the comment to the /openSplunkLink endpoint, which redirects the user to the constructed URL.
This way, the comment is appended to the URL when the button is clicked, achieving the desired behaviour.