mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-28 03:28:35 +08:00
feat(route/telegram): add telegram session scripts and logics
This commit is contained in:
@@ -286,6 +286,7 @@ export type Config = {
|
||||
};
|
||||
telegram: {
|
||||
token?: string;
|
||||
session?: string;
|
||||
};
|
||||
tophub: {
|
||||
cookie?: string;
|
||||
|
||||
@@ -92,7 +92,13 @@ For backward compatibility reasons, invalid \`routeParams\` will be treated as \
|
||||
`,
|
||||
},
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requireConfig: [
|
||||
{
|
||||
name: 'TELEGRAM_SESSION',
|
||||
optional: true,
|
||||
description: '',
|
||||
},
|
||||
],
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
@@ -110,13 +116,12 @@ For backward compatibility reasons, invalid \`routeParams\` will be treated as \
|
||||
handler,
|
||||
description: `
|
||||
:::tip
|
||||
Due to Telegram restrictions, some channels involving pornography, copyright, and politics cannot be subscribed. You can confirm by visiting \`https://t.me/s/:username\`.
|
||||
Due to Telegram restrictions, some channels involving pornography, copyright, and politics cannot be subscribed. You can confirm by visiting \`https://t.me/s/:username\`, it's recommended to deploy your own instance with configs (create your telegram via \`https://core.telegram.org/api/obtaining_api_id\`, run this command \`node ./lib/routes/telegram/scripts/get-telegram-session.mjs\` to get \`TELEGRAM_SESSION\` and set it as Environment Variable).
|
||||
:::`,
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const useWeb = ctx.req.param('routeParams') || !config.telegram.session;
|
||||
if (!useWeb) {
|
||||
if (ctx.req.param('routeParams') && config.telegram.session) {
|
||||
return tglibchannel(ctx);
|
||||
}
|
||||
|
||||
@@ -191,6 +196,10 @@ async function handler(ctx) {
|
||||
: $('.tgme_widget_message_wrap:not(.tgme_widget_message_wrap:has(.service_message,.tme_no_messages_found))'); // also exclude service messages
|
||||
|
||||
if (list.length === 0 && $('.tgme_channel_history').length === 0) {
|
||||
if (config.telegram.session) {
|
||||
return tglibchannel(ctx);
|
||||
}
|
||||
|
||||
throw new Error(`Unable to fetch message feed from this channel. Please check this URL to see if you can view the message preview: ${resourceUrl}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { TelegramClient } from 'telegram';
|
||||
import { StringSession } from 'telegram/sessions/index.js';
|
||||
import readline from 'readline';
|
||||
|
||||
function userInput(question) {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
return new Promise((resolve) => {
|
||||
rl.question(question, (answer) => {
|
||||
rl.close();
|
||||
resolve(answer);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getSessionString() {
|
||||
const apiId = parseInt(await userInput('Please enter your API ID: '));
|
||||
const apiHash = await userInput('Please enter your API Hash: ');
|
||||
const stringSession = new StringSession('');
|
||||
const client = new TelegramClient(stringSession, apiId, apiHash, {
|
||||
connectionRetries: 5,
|
||||
});
|
||||
await client.start({
|
||||
phoneNumber: async () => await userInput('Please enter your phone number: '),
|
||||
password: async () => await userInput('Please enter your password: '),
|
||||
phoneCode: async () => await userInput('Please enter the code you received: '),
|
||||
onError: (err) => console.log(err),
|
||||
});
|
||||
|
||||
console.log('You are now connected.');
|
||||
const sessionString = client.session.save();
|
||||
|
||||
console.log('Your session string is:', sessionString);
|
||||
await client.disconnect();
|
||||
return sessionString;
|
||||
}
|
||||
|
||||
// Run the function
|
||||
getSessionString().catch(console.error);
|
||||
@@ -117,6 +117,10 @@ export default async function handler(ctx) {
|
||||
const chat = await client.getInputEntity(ctx.req.param('username'));
|
||||
const channelInfo = await client.getEntity(chat);
|
||||
|
||||
if (channelInfo.className !== 'Channel') {
|
||||
throw new Error(`${ctx.req.param('username')} is not a channel`);
|
||||
}
|
||||
|
||||
let attachments = [];
|
||||
const messages = await client.getMessages(chat, { limit: 50 });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user