1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| addEventListener('fetch', event => { event.respondWith(TelgramChannelStarter(event.request)) })
const ChannelNameList = ['thun888sthinks', 'Thun888sDraftArticles', 'thun888sgrocery']; const deniedRegion = [""]; const baseproxyurl = "https://get-tg-channel-api.hzchu.top/?proxy="
async function TelgramChannelStarter(request) { const url = new URL(request.url); const Region = request.headers.get('cf-ipcountry'); let denied = deniedRegion.includes(Region); const proxyUrl = url.searchParams.get('proxy'); let mid = parseInt(url.searchParams.get('mid')); let cid = parseInt(url.searchParams.get('cid')) || 0; let tagname = url.searchParams.get('tag') || "DefaultTag"; let showtag = url.searchParams.get('showtag') || "true"; let usemd = url.searchParams.get('usemd') || "true"; usemd = usemd === 'true'; showtag = showtag === 'true'; let showview = url.searchParams.get('showview'); showview = showview === 'true';
let ChannelName = ChannelNameList[cid]
if (!!proxyUrl) { if (!(proxyUrl.match(/\:\/\/(.*?)\.telegram\.org/g) || proxyUrl.match(/\:\/\/(.*)\.cdn\-telegram\.org/g) || proxyUrl.match(/\:\/\/telegram\.org/g))) { return new Response('Proxy URL is not valid'); } return fetch(proxyUrl); }
const startbefore = url.searchParams.get('startbefore'); const ChannelUrl = new URL(`https://t.me/s/${ChannelName}`);
if (!!startbefore) { ChannelUrl.searchParams.set('before', startbefore); }
let getDataFromTelegram = await fetch(ChannelUrl, { "headers": { "x-requested-with": "XMLHttpRequest" }, "method": "POST" }).then(res => res.text()) .then(res => res .replace(/\\n/g, '') .replace(/\\(.)/g, '$1') .replace(/(^\"|\"$)/g, '') );
if (url.searchParams.get('rawHtml') === 'true') { return new Response(getDataFromTelegram, { headers: { "content-type": "text/html;charset=UTF-8", "Access-Control-Allow-Origin": "*" } }); }
const nextBeforeMatch = [...getDataFromTelegram.matchAll(/data-before="([0-9]+)"/g)]; const nextBefore = nextBeforeMatch.length > 0 ? Number(nextBeforeMatch[0][1]) : 0; const ChannelMessages = ElementSpliter(getDataFromTelegram, '<div class="tgme_widget_message_wrap');
const ChannelMessageData = {}; ChannelMessages.forEach(ChannelMessage => { const MessageIdMatch = ChannelMessage.match(new RegExp(`data-post\="${ChannelName}\/(\\d+)"`)); if (MessageIdMatch) { const MessageId = MessageIdMatch[1];
if (!ChannelMessageData.hasOwnProperty(MessageId)) { const MessageTextMatch = ChannelMessage.match(/<div class="tgme_widget_message_text js-message_text"(.+?)<\/div>/s); const MessagePhotoMatches = [...ChannelMessage.matchAll(/background-image:url\('(.+?)'\)/g)]; const MessageTimeMatch = ChannelMessage.match(/datetime="(.+?)"/); const MessageViewsMatch = ChannelMessage.match(/<span class="tgme_widget_message_views">\s*(\d+)\s*<\/span>/);
let msg_views = MessageViewsMatch ? MessageViewsMatch[1] : "0"; let msg_time = MessageTimeMatch ? Math.round(new Date(MessageTimeMatch[1]).getTime() / 1000) : Math.round(new Date().getTime() / 1000);
let msg_text = MessageTextMatch[0].replace(/<div (.*?)>/g, '').replace(/<\/div>/g, ''); msg_text = msg_text.replace(/href=\"\?q=(.+?)\"/g, function (match, p1) { return 'href="https://t.me/s/' + ChannelName + '/' + p1 + '"'; }); msg_text = msg_text.replace(/<i class="emoji" style="background-image:url\(\'\/\/telegram.org\/img\/emoji\/40\/.+?\)\">/g, '')
if (!showtag) { msg_text = msg_text.replace(/<a href="https:\/\/t\.me\/s\/.+?\/%23.+?">#[^<]+<\/a>/g, ''); }
if (showview) { msg_text = msg_text + '<br>(👀:' + msg_views + ')' }
if (usemd) { msg_text = msg_text.replace(/<a href=\"(.*?)\">(.*?)<\/a>/g, (match, $1, $2) => { if ($2.startsWith('#')) { return `[${$2}](${$1})`; } else { return $2; } }); msg_text = msg_text.replace(/!/g, '!'); msg_text = msg_text.replace(/%/g, '%'); msg_text = msg_text.replace(/<br\/>/g, '\n'); msg_text = msg_text.replace(/</g, '<'); msg_text = msg_text.replace(/>/g, '>'); msg_text = msg_text.replace(/&/g, '&'); msg_text = msg_text.replace(/"/g, '"'); }
if (MessageTextMatch && (!denied && MessageTextMatch[0].includes('#' + tagname))) { let resourceList = MessagePhotoMatches.filter(m => !m[1].includes('//telegram.org/img/emoji/')) .map(m => ({ "createdTs": msg_time, "externalLink": baseproxyurl + m[1], "type": "image/jpeg", "size": 0 }));
ChannelMessageData[MessageId] = { id: MessageId, name: MessageId, rowStatus: "NORMAL", creatorId: 1, content: msg_text, createdTs: msg_time, updatedTs: msg_time, displayTs: msg_time, visibility: "PUBLIC", pinned: false, creatorName: ChannelName, creatorUsername: ChannelName, resourceList: resourceList, relationList: [], views: msg_views, }; } } } });
let sortedMessages = Object.values(ChannelMessageData).sort((a, b) => b.createdTs - a.createdTs); const messagesCountParam = url.searchParams.get('limit'); const messagesCount = messagesCountParam ? parseInt(messagesCountParam, 10) : sortedMessages.length; sortedMessages = sortedMessages.slice(0, messagesCount);
return new Response(JSON.stringify(sortedMessages), { headers: { "content-type": "application/json;charset=UTF-8", "Access-Control-Allow-Origin": "*" } }); }
const ElementSpliter = (html, StartElement) => { const Elements = [] const ElementSpliterOnce = (html, StartElement) => { let ElementName = [...StartElement.matchAll(/\<(?<ELENAME>[a-zA-Z0-9]+)\s/g)][0].groups.ELENAME let ElementContent = StartElement for (let Start = html.indexOf(StartElement) + StartElement.length; Start < html.length; Start++) { ElementContent += html[Start] if (ElementContent.endsWith(`</${ElementName}>`)) { const PrefixCount = Object.keys(ElementContent.match(new RegExp(`\<${ElementName}`, 'g'))).length const SuffixCount = Object.keys(ElementContent.match(new RegExp(`\<\/${ElementName}(.*?)>`, 'g')) || []).length if ( PrefixCount === SuffixCount && PrefixCount !== 0 ) { return ElementContent } }
} } while (1) { if (html.indexOf(StartElement) === -1) break const SplitOnce = ElementSpliterOnce(html, StartElement) Elements.push(SplitOnce) html = html.replace(SplitOnce, '') } return Elements }
|