raclen / raclen.github.io

清晨好文章
http://raclen.win
MIT License
0 stars 0 forks source link

使用nodejs抓取某个telegram频道的数据 #20

Open raclen opened 1 week ago

raclen commented 1 week ago

在 Node.js 中抓取 Telegram 频道的数据可以通过使用 Telethon 的 JavaScript 等效库 telegramgrammy 实现。这些库允许我们与 Telegram 的 API 交互,获取频道的消息。下面是使用 telegramgrammy 的详细步骤。

方法一:使用 telegram

telegram 库提供了对 Telegram API 的高级接口,适合用来进行各种交互操作。

  1. 安装 telegram

    npm install telegram
  2. 获取 API ID 和 API Hash

    • 访问 my.telegram.org 并登录你的 Telegram 账户。
    • 创建一个新的应用程序以获取 API ID 和 API Hash。
  3. 使用 telegram 库登录并获取频道数据

    创建一个新的文件(例如 fetchTelegramData.js),并添加以下代码:

    const { TelegramClient } = require('telegram');
    const { StringSession } = require('telegram/sessions');
    const input = require('input'); // 工具库,用于获取用户输入
    
    const apiId = 'YOUR_API_ID'; // 替换为你的 API ID
    const apiHash = 'YOUR_API_HASH'; // 替换为你的 API Hash
    const stringSession = new StringSession(''); // 可以为空
    
    (async () => {
     const client = new TelegramClient(stringSession, apiId, apiHash, {
       connectionRetries: 5,
     });
    
     await client.start({
       phoneNumber: async () => await input.text('请输入你的电话号码:'),
       password: async () => await input.text('请输入你的密码:'),
       phoneCode: async () => await input.text('请输入你收到的验证码:'),
       onError: (err) => console.log(err),
     });
    
     console.log('你已成功登录!');
    
     // 替换为你的频道用户名或 URL
     const channel = 'YOUR_CHANNEL_USERNAME';
    
     const result = await client.getMessages(channel, { limit: 10 });
    
     result.forEach((message) => {
       console.log(message.message);
     });
    
     await client.disconnect();
    })();
  4. 运行脚本: 在命令行中运行以下命令:

    node fetchTelegramData.js

    按提示输入你的电话号码、密码和验证码。登录后,你将会看到从指定频道获取的最新消息。

方法二:使用 grammy

grammy 库是一个用于与 Telegram Bot API 交互的现代库。适用于通过 Bot 获取频道消息。

  1. 安装 grammy

    npm install grammy
  2. 创建一个 Telegram Bot

    • 使用 @BotFather 在 Telegram 中创建一个新 Bot,并获取 API Token。
  3. 将 Bot 添加到频道

    • 确保你的 Bot 被添加到目标频道,并且具备读取消息的权限。
  4. 使用 grammy 库获取频道数据

    创建一个新的文件(例如 fetchChannelMessages.js),并添加以下代码:

    const { Bot } = require('grammy');
    
    const botToken = 'YOUR_BOT_API_TOKEN'; // 替换为你的 Bot API Token
    const bot = new Bot(botToken);
    
    // 替换为你的频道 ID(负数)或用户名
    const channelId = '@YOUR_CHANNEL_USERNAME';
    
    (async () => {
     // 获取最近的 10 条消息
     const messages = await bot.api.getUpdates({ limit: 10 });
    
     messages.forEach((update) => {
       if (update.message && update.message.chat && update.message.chat.username === channelId) {
         console.log(update.message.text);
       }
     });
    })();
  5. 运行脚本: 在命令行中运行以下命令:

    node fetchChannelMessages.js

    脚本将会输出频道中的最新消息。

注意事项

这两种方法都可以有效地帮助你抓取 Telegram 频道的数据,根据你的需求选择最适合的方案。

raclen commented 6 days ago
//使用代理
const proxy = require('socks-proxy-agent');
let SocksProxyAgent = proxy.SocksProxyAgent

// console.log('SocksProxyAgent',SocksProxyAgent);
// SOCKS5 代理配置
const socksHost = 'socks5://127.0.0.1';  // 代理服务器地址,例如 'socks5://127.0.0.1'
const socksPort = 10808;  // 代理服务器端口

// 创建 SOCKS5 代理 agent
const agent = new SocksProxyAgent(`${socksHost}:${socksPort}`);

(async () => {
  const client = new TelegramClient(stringSession, apiId, apiHash, {
    connectionRetries: 5,
    requestOptions: {
        agent: agent
      }
  });

  await client.start({
    phoneNumber: async () => await input.text('请输入你的电话号码:'),
    password: async () => await input.text('请输入你的密码:'),
    phoneCode: async () => await input.text('请输入你收到的验证码:'),
    onError: (err) => console.log(err),
  });

请注意,申请API ID 和 API Hash的时候,需要手机号地区的VPN才行,比如我的是波兰手机号,必须要用波兰的VPN 申请后,也必须要用波兰的代理才能访问,否则服务起不来,会报错 [Connecting to 149.154.167.91:80/TCPFull...]