Skip to content

Instantly share code, notes, and snippets.

@steinbring
Created June 16, 2024 23:52
Show Gist options
  • Save steinbring/0d09e8d337107aeacf96f35d8a7a7848 to your computer and use it in GitHub Desktop.
Save steinbring/0d09e8d337107aeacf96f35d8a7a7848 to your computer and use it in GitHub Desktop.
The script handling replies on @joe@jws.app
const axios = require('axios');
const schedule = require('node-schedule');
const { instanceUrl, account, checkInterval, mentions, accessToken } = require('./config');
const fetchMentions = async () => {
try {
const response = await axios.get(`${instanceUrl}/api/v1/notifications`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return response.data;
} catch (error) {
console.error('Error fetching mentions:', error);
return [];
}
};
const postReply = async (statusId, reply) => {
try {
await axios.post(
`${instanceUrl}/api/v1/statuses`,
{ status: reply, in_reply_to_id: statusId },
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
console.log(`Replied to status ${statusId} with "${reply}"`);
} catch (error) {
console.error('Error posting reply:', error);
}
};
const processMentions = async () => {
const mentionsData = await fetchMentions();
for (const mention of mentionsData) {
if (mention.type === 'mention' && mention.account.acct !== account) {
const mentionText = mention.status.content.trim().toLowerCase();
const noReplies = mention.status.replies_count === 0;
for (const { trigger, reply } of mentions) {
if (mentionText.includes('</span> '+trigger+'</p>') && noReplies) {
console.log(mentionText);
await postReply(mention.status.id, reply);
break;
}
}
}
}
};
schedule.scheduleJob(checkInterval, processMentions);
console.log('Mention checking scheduled every 5 minutes');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment