Compare commits

...

2 Commits

Author SHA1 Message Date
werewolfkid a3cc0f0711 Added function to fetch links to verify the
Main / build-and-push-docker-image (20.x) (pull_request) Successful in 7m24s
2026-04-14 18:08:21 -04:00
werewolfkid aaac8c3aa7 Added comments. 2026-04-14 18:01:44 -04:00
15 changed files with 99 additions and 24 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
"type": "module",
"version": "3.2.0",
"private": true,
"packageManager": "yarn@4.12.0",
"packageManager": "yarn@4.13.0",
"description": "This grammY powered Telegram bot is designed to delete Twitter/X links and reformat services from whitelisted groups. This one is the main bot for the LCM Telegram groups/communities.",
"imports": {
"#root/*": "./build/src/*"
+1 -1
View File
@@ -32,7 +32,7 @@ feature.hears(
}
await urql
.mutation(increment, { command: true, mutationKey })
.mutation(increment, { command: true, mutationKey }) // Increments the trigger count
.toPromise()
.then(async () => {
if (ctx.msg) {
+1 -1
View File
@@ -32,7 +32,7 @@ feature.hears(
}
await urql
.mutation(increment, { command: true, mutationKey })
.mutation(increment, { command: true, mutationKey }) // Increments the trigger count
.toPromise()
.then(async () => {
if (ctx.msg) {
+20 -16
View File
@@ -2,12 +2,13 @@ import { Composer } from "grammy";
import type { Context } from "#root/bot/context.js";
import { logHandle } from "#root/bot/helpers/logging.js";
import metaLinkCheck from "#root/lib/metaLinkCheck.js";
import twitterLinkCheck from "#root/lib/twitterLinkCheck.js";
import twitterLinkCheck, { twitterRegex } from "#root/lib/twitterLinkCheck.js";
import { urql } from "#root/main.js";
import increment from "#root/lib/graphql/mutations/incrementMutation.js";
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
import tiktokLinkCheck from "#root/lib/tiktokLinkCheck.js";
import fetchLink from "#root/lib/fetchLink.js";
const composer = new Composer<Context>();
@@ -20,8 +21,10 @@ const feature = composer.chatType(["group", "supergroup"]);
feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
// Checks there is a chat and msg property in the context.
if (ctx.chat && ctx.msg) {
const groupName = ctx.chat?.title || "";
const groupID = ctx.chat?.id.toString() || "";
@@ -41,24 +44,25 @@ feature.on("message::url", logHandle("embed-check"), async (ctx: Context) => {
// If the all embeds array isn't empty filter through them to check for every type of disallowed link.
if (allEmbeds.length) {
const metaLinks = allEmbeds.filter(({ url }) => metaLinkCheck(url));
const twitterLinks = allEmbeds.filter(({ url }) => twitterLinkCheck(url));
const tiktokLinks = allEmbeds.filter(({ url }) => tiktokLinkCheck(url));
detectedLinks +=
allEmbeds.filter(({ url }) => metaLinkCheck(url)).length || 0;
// Handles Meta & Facebook Links
if (metaLinks.length) {
detectedLinks += metaLinks.length;
detectedLinks += (
await Promise.all(
allEmbeds.map(async ({ url }) => {
// Checks for a false positive.
if (twitterLinkCheck(url)) {
return await fetchLink(url, twitterRegex);
}
return false;
})
)
).reduce((acc, curr) => {
return (acc += curr ? 1 : 0);
}, 0);
// Handles Twitter/X links.
if (twitterLinks.length) {
detectedLinks += twitterLinks.length;
}
// Handles TikTok links.
if (tiktokLinks.length) {
detectedLinks += tiktokLinks.length;
}
detectedLinks +=
allEmbeds.filter(({ url }) => tiktokLinkCheck(url)).length || 0;
}
if (detectedLinks) {
+3
View File
@@ -18,7 +18,9 @@ feature.hears(/^\/help/, logHandle("help"), async (ctx: Context) => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
const statsSite = process.env.STATS_SITE || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
// Retrieve the command list
const commandsListArr = await getCommandsList(`${statsSite}api/bot`, apiKey);
// const GROUP_IDS = process.env.GROUP_IDS
@@ -67,6 +69,7 @@ feature.hears(/^\/help/, logHandle("help"), async (ctx: Context) => {
}
}
// Create a readable command list string.
const commandsListStr = commandsListArr.reduce((prev, curr) => {
const { command, description, groups } = curr;
+2
View File
@@ -21,8 +21,10 @@ feature.hears(
async (ctx: Context) => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
// Checks there is a chat and msg property in the context.
if (ctx.chat && ctx.msg) {
const username = ctx.msg.from?.username;
+1
View File
@@ -19,6 +19,7 @@ feature.hears(
async (ctx: Context) => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
// Checks if the context includes a message property.
+1
View File
@@ -21,6 +21,7 @@ feature.hears(
async (ctx: Context) => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
if (ctx.chat && ctx.msg) {
+8 -1
View File
@@ -6,6 +6,7 @@ import { urql } from "#root/main.js";
import increment from "#root/lib/graphql/mutations/incrementMutation.js";
import addGroup from "#root/lib/graphql/mutations/addGroupMutation.js";
import incrementGroup from "#root/lib/graphql/mutations/incrementGroupMutation.js";
import fetchLink from "#root/lib/fetchLink.js";
const composer = new Composer<Context>();
@@ -21,8 +22,10 @@ feature.hears(
async (ctx: Context) => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
// Checks there is a chat and msg property in the context.
if (ctx.chat && ctx.msg) {
const username = ctx.msg.from?.username;
@@ -30,7 +33,11 @@ feature.hears(
.mutation(increment, { link: 1, mutationKey })
.toPromise()
.then(async () => {
if (ctx.msg && ctx.chat) {
if (ctx.msg && ctx.chat && ctx.msg.text) {
// Checks for a false positive
const truePositive = await fetchLink(ctx.msg.text, twitterRegex);
if (!truePositive) return;
// Replies to the user informing them of the action.
await ctx.reply(
`@${username} Twitter and X links along with reformatting services for Twitter posts are not allowed here\\. Please consider sharing the media directly or from other social media sources or websites\\. No administration action was taken against you other than the message being deleted\\.`,
+1
View File
@@ -15,6 +15,7 @@ const feature = composer.chatType("private");
feature.command("start", logHandle("command-start"), async ctx => {
const mutationKey = process.env.GRAPHQL_MUTATION_KEY || "";
// Increments the trigger count
await urql.mutation(increment, { trigger: true, mutationKey });
await urql
+49
View File
@@ -0,0 +1,49 @@
/**
* This function will fetch a url to follow all redirects and compare the base url
* with the RegEx to validate false and true positives.
*
* @param message The message that originally triggered the provided RegEx
* @param banRegEx RegEx to check against
* @returns @type boolean
*/
const linkChecker = async (message: string, banRegEx: RegExp) => {
const detectedLinks = message.split(" ").map(subStr => {
if (banRegEx.test(subStr)) {
return subStr;
}
});
if (!detectedLinks || detectedLinks.length === 0) {
return false;
}
const resolvedURLs = detectedLinks.map(async string => {
if (string === undefined) {
return false;
}
const url =
string.startsWith("http://") || string.startsWith("https://")
? string
: `https://${string}`;
const res = await fetch(url, {
headers: {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"
}
});
const resolvedURL = new URL(res.url);
const baseURL = resolvedURL.hostname.split(".").slice(-2).join(".");
if (/^x\.com/i.test(baseURL)) {
return true;
}
});
return (await Promise.all(resolvedURLs)).includes(true);
};
export default linkChecker;
+7
View File
@@ -6,6 +6,13 @@ export interface Commands {
groups: boolean;
private: boolean;
}
/**
* Fetched a list of commands with a provided url and API key.
* @param url The url to query.
* @interface Commands
* @param apiKey The api for the url.
* @returns Array of Commands object.
*/
const fetchCommandsList = async (
url: string,
+1 -1
View File
@@ -6,7 +6,7 @@ const metaRegex =
* This function will check if a url matches Meta services links using regex.
*
* @param linkUrl representing a suspected url on the banlist
* @returns flag
* @returns @type boolean
*/
const metaLinkCheck = (linkUrl: string): boolean => {
let flag = false;
+1 -1
View File
@@ -5,7 +5,7 @@ const tiktokRegex = /(tiktok\.com)/gi;
* This function will check if a url matches TikTok services links using regex.
*
* @param linkUrl representing a suspected url on the banlist
* @return flag
* @returns @type boolean
*/
const tiktokLinkCheck = (linkUrl: string): boolean => {
let flag = false;
+1 -1
View File
@@ -5,7 +5,7 @@ const twitterRegex = /(x\.com|twitter\.com)/gi;
* This function will check if a url matches Twitter/X services links using regex.
*
* @param linkUrl representing a suspected url on the banlist
* @return flag
* @returns @type boolean
*/
const twitterLinkCheck = (linkUrl: string): boolean => {
let flag = false;