Skip to content

Commit

Permalink
fix: failures of preview-links in Node.js runtime
Browse files Browse the repository at this point in the history
* Only send one response in all cases.
* Process card click events before matched URL to avoid unreachable code.
  • Loading branch information
pierrick committed Feb 17, 2024
1 parent d57a9d0 commit 5a4783a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions node/preview-link/attach-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports.onMessage = (req, res) => {
if (req.method === 'GET' || !req.body.message) {
res.send(
'Hello! This function is meant to be used in a Google Chat Space.');
return;
}

// Checks for the presence of event.message.matchedUrl and attaches a card
Expand Down Expand Up @@ -97,6 +98,7 @@ exports.onMessage = (req, res) => {
},
],
});
return;
}

// If the Chat app doesn’t detect a link preview URL pattern, it says so.
Expand Down
15 changes: 9 additions & 6 deletions node/preview-link/preview-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ exports.onMessage = (req, res) => {
if (req.method === 'GET' || !req.body.message) {
res.send(
'Hello! This function is meant to be used in a Google Chat Space.');
}

// Checks for the presence of event.message.matchedUrl and attaches a card
// if present
if (req.body.message.matchedUrl) {
res.json(createMessage());
return;
}

// Respond to button clicks on attached cards
Expand All @@ -45,9 +40,17 @@ exports.onMessage = (req, res) => {

if (req.body.action.actionMethodName === 'assign') {
res.json(createMessage(actionResponseType, 'You'));
return;
}
}

// Checks for the presence of event.message.matchedUrl and attaches a card
// if present
if (req.body.message.matchedUrl) {
res.json(createMessage());
return;
}

// If the Chat app doesn’t detect a link preview URL pattern, it says so.
res.json({'text': 'No matchedUrl detected.'});
};
Expand Down
2 changes: 2 additions & 0 deletions node/preview-link/simple-text-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports.onMessage = (req, res) => {
if (req.method === 'GET' || !req.body.message) {
res.send(
'Hello! This function is meant to be used in a Google Chat Space.');
return;
}

// Checks for the presence of event.message.matchedUrl and responds with a
Expand All @@ -35,6 +36,7 @@ exports.onMessage = (req, res) => {
'text': 'req.body.message.matchedUrl.url: ' +
req.body.message.matchedUrl.url,
});
return;
}

// If the Chat app doesn’t detect a link preview URL pattern, it says so.
Expand Down
1 change: 1 addition & 0 deletions node/preview-link/update-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports.onMessage = (req, res) => {
if (req.method === 'GET' || !req.body.message) {
res.send(
'Hello! This function is meant to be used in a Google Chat Space.');
return;
}

// Respond to button clicks on attached cards
Expand Down

0 comments on commit 5a4783a

Please sign in to comment.