Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ License: MIT
const TelegramBot = require("node-telegram-bot-api");
const fetch = require("node-fetch");

const fullURLRegex = /https?:\/\/(([^\s]*)\.)?amazon\.([a-z.]{2,5})(\/d\/([^\s]*)|\/([^\s]*)\/?(?:dp|o|gp|-)\/)(aw\/d\/|product\/)?(B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(?:X|[0-9]))([^\s]*)/gi;
const fullURLRegex = /https?:\/\/(([^\s]*)\.)?amazon\.([a-z.]{2,5}){1,2}(\/d\/([^\s]*)|\/([^\s]*)\/?(?:dp|o|gp|-)\/)(aw\/d\/|product\/)?(B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(?:X|[0-9]))([^\s]*)/gi;
const shortURLRegex = /https?:\/\/(([^\s]*)\.)?amzn\.to\/([0-9A-Za-z]+)/gi;
const URLRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi;

function regExpEscape(literal_string) {
return literal_string.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
}

if (!process.env.TELEGRAM_BOT_TOKEN) {
console.log("Missing TELEGRAM_BOT_TOKEN env variable");
process.exit(1);
Expand Down Expand Up @@ -121,7 +125,7 @@ async function shortenURL(url) {
}

function buildAmazonUrl(asin) {
return `https://www.amazon.${amazon_tld}/dp/${asin}?tag=${amazon_tag}`;
return `https://www.amazon.${regExpEscape(amazon_tld)}/dp/${asin}?tag=${amazon_tag}`;
}

function buildRawAmazonUrl(element) {
Expand Down