创建将侦听电报频道的应用程序

时间:2017-12-14 09:03:48

标签: node.js telegram

首先请注意这不是关于创建机器人

我的目标是创建一个应用程序,它只会收听我将提供给它的帐户的任意数量的电报频道,并检索发送到这些频道的所有消息(就像我是普通用户一样)。我的猜测是我需要

  • 使用我的帐户的电话号码验证自己
  • 能够为每个频道设置回调侦听器或为所有传入消息设置一般侦听器

我已经在telegram api环顾了几天了,我对它的运作方式感到非常困惑。在放弃它之后,我开始研究现成的实现,主要是NodeJS,但仍然无法找到具体的解决方案。我正在使用telegram-js api对某些内容进行测试,但使用node直接运行它并不起作用。是否需要在浏览器中运行?有没有更精简的方法呢?最好是带有良好文档的东西。

PS:我精通Java和Javascript,因此我根据这些语言优先考虑了库。

编辑:

这是我编写的代码(基本上是复制了一个例子)

var { Telegram } = require("../libs/telegram");
var TypeLanguage = require("telegram-tl-node") ;
var MTProto = require("telegram-mt-node");

var schema = require("../libs/api-schema.json");

const APP_ID = "111111";
const APP_HASH = "fb6da8f6abdf876abd6a9d7bf6";
const SERVER = { host: "111.111.111.11", port: "443" };
const config = {
  id: APP_ID,
  hash: APP_HASH,
  version: '0.0.1',
  lang_code: 'en',
  authKey: null
};

let telegram = new Telegram(MTProto, TypeLanguage);
telegram.useSchema(schema);
addPublicKeys(telegram);

let connection = new MTProto.net.HttpConnection(SERVER);

let client = telegram.createClient();
client.setConnection(connection);

connection.connect(function() {
  let ready = client.setup(config);
  ready.then(function(client) {
    // it never resolves this promise
    function callback(response) {
      console.log(response);
    }
    client.callApi("help.getConfig").then(callback, callback);
  });
});

它使用这两个库: telegram-mt-node telegram-tl-node

1 个答案:

答案 0 :(得分:4)

最新答案,但可能会对其他人有所帮助。

您可以利用mtproto-core来通过常规电报帐户进行身份验证并收听更新(或者实际上可以使用电报客户端执行任何操作)

这是我编写的示例脚本,用于侦听用户订阅的频道/超级组的新消息:

std::string

您可以从https://my.telegram.org中找到double gpa; std::string name; while (file >> gpa && std::getline(file, name)) { std::cout << gpa << ":" << name << "\n"; } const { MTProto, getSRPParams } = require('@mtproto/core'); const prompts = require('prompts'); const api_id = ...; // insert api_id here const api_hash = ' ... '; // insert api_hash here async function getPhone() { return (await prompts({ type: 'text', name: 'phone', message: 'Enter your phone number:' })).phone } async function getCode() { // you can implement your code fetching strategy here return (await prompts({ type: 'text', name: 'code', message: 'Enter the code sent:', })).code } async function getPassword() { return (await prompts({ type: 'text', name: 'password', message: 'Enter Password:', })).password } const mtproto = new MTProto({ api_id, api_hash, }); function startListener() { console.log('[+] starting listener') mtproto.updates.on('updates', ({ updates }) => { const newChannelMessages = updates.filter((update) => update._ === 'updateNewChannelMessage').map(({ message }) => message) // filter `updateNewChannelMessage` types only and extract the 'message' object for (const message of newChannelMessages) { // printing new channel messages console.log(`[${message.to_id.channel_id}] ${message.message}`) } }); } // checking authentication status mtproto .call('users.getFullUser', { id: { _: 'inputUserSelf', }, }) .then(startListener) // means the user is logged in -> so start the listener .catch(async error => { // The user is not logged in console.log('[+] You must log in') const phone_number = await getPhone() mtproto.call('auth.sendCode', { phone_number: phone_number, settings: { _: 'codeSettings', }, }) .catch(error => { if (error.error_message.includes('_MIGRATE_')) { const [type, nextDcId] = error.error_message.split('_MIGRATE_'); mtproto.setDefaultDc(+nextDcId); return sendCode(phone_number); } }) .then(async result => { return mtproto.call('auth.signIn', { phone_code: await getCode(), phone_number: phone_number, phone_code_hash: result.phone_code_hash, }); }) .catch(error => { if (error.error_message === 'SESSION_PASSWORD_NEEDED') { return mtproto.call('account.getPassword').then(async result => { const { srp_id, current_algo, srp_B } = result; const { salt1, salt2, g, p } = current_algo; const { A, M1 } = await getSRPParams({ g, p, salt1, salt2, gB: srp_B, password: await getPassword(), }); return mtproto.call('auth.checkPassword', { password: { _: 'inputCheckPasswordSRP', srp_id, A, M1, }, }); }); } }) .then(result => { console.log('[+] successfully authenticated'); // start listener since the user has logged in now startListener() }); }) 的值。

第一次运行时,脚本会提示用户输入电话号码,代码和密码。

api_id

以及身份验证结束后 样本运行输出:

api_hash

我检查身份验证状态的方式为from here

值得一试的替代库,它们在撰写本文时就处于活动状态(并可用于创建相同的行为):Airgram(tdlib)和GramJs

相关问题