Skip to content

Commit

Permalink
Allow passive messages to work on link attachments in addition to the…
Browse files Browse the repository at this point in the history
… raw message body
  • Loading branch information
AstroCB committed Dec 31, 2020
1 parent 3f3bfba commit 7d95f9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function handleMessage(err, message, external = false, api = gapi) { // New mess
const m = message.body;
const attachments = message.attachments;
// Handle message body
if (m) {
if (m || attachments) {
// Pass to commands testing for trigger word
const cindex = m.toLowerCase().indexOf(config.trigger);
if (cindex > -1) { // Trigger command mode
Expand Down
32 changes: 25 additions & 7 deletions src/passive.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,38 @@ const passiveTypes = [
];

exports.handlePassive = (messageObj, groupInfo, api) => {
const message = messageObj.body;

getPassiveTypes(message, type => {
getPassiveTypes(messageObj, (type, match) => {
// Call generic handler and pass in all message info (handler can
// decide whether they want to use it selectively via parameters)
const match = message.match(type.regex);
type.handler(match, groupInfo, messageObj, type.regex, api);
});
};

function getPassiveTypes(text, cb) {
function getPassiveTypes(msg, cb) {
const { body, attachments } = msg;
passiveTypes.forEach(type => {
if (text.match(type.regex)) {
cb(type);
const match = body.match(type.regex);
if (match) {
cb(type, match);
} else {
// If there isn't a match in the body, we can also check the attachments
attachments
.filter(attachment => attachment.type === "share" && attachment.url)
.map(attachment => {
// Sometimes, Facebook futzes with the URL for redirects,
// so we need to decode it before checking for a match
const encodedURLMatch = attachment.url.match(/l\.facebook\.com\/l\.php\?u=([^&]+)/i);
if (encodedURLMatch) {
return { ...attachment, "url": decodeURIComponent(encodedURLMatch[1]) };
}
return attachment;
})
.forEach(attachment => {
const attachmentMatch = attachment.url.match(type.regex);
if (attachmentMatch) {
cb(type, attachmentMatch);
}
});
}
});
}
Expand Down

0 comments on commit 7d95f9d

Please sign in to comment.