How to receive my own telegram messages in node.js without bot

时间:2017-10-23 20:48:27

标签: node.js client telegram

I would like to have a very simple client in nodejs (an example) that can receive messages from my contacts in telegram. I just searched in internet but I only get bot samples. I want to receive group messages in what I don't have access to give privileges to my bot so I would like to know if I can receive my own messages with no bot as intermediary.

3 个答案:

答案 0 :(得分:28)

嗯...... 其他答案提供了来自 unmaintained 库的示例。因此,您不应该依赖这些库。

请参阅: telegram.link is dead

您应该使用最新的Telegram客户端库 telegram-mtproto

1. 从以下位置获取api_idapi_hash

Telegram Apps

2. 安装所需的客户端库:

npm install telegram-mtproto@beta --save

3. 使用来自Telegram Appsapi_idapi_hash以及phone number初始化您的node.js应用

import MTProto from 'telegram-mtproto'

const phone = {
  num : '+90555555555', // basically it is your phone number
  code: '22222' // your 2FA code
}

const api = {
  layer          : 57,
  initConnection : 0x69796de9,
  api_id         : 111111
}

const server = {
  dev: true //We will connect to the test server.
}           //Any empty configurations fields can just not be specified

const client = MTProto({ server, api })

async function connect(){
  const { phone_code_hash } = await client('auth.sendCode', {
    phone_number  : phone.num,
    current_number: false,
    api_id        : 111111, // obtain your api_id from telegram
    api_hash      : 'fb050b8fjernf323FDFWS2332' // obtain api_hash from telegram
  })
  const { user } = await client('auth.signIn', {
    phone_number   : phone.num,
    phone_code_hash: phone_code_hash,
    phone_code     : phone.code
  })
      console.log('signed as ', user);
    }

    connect();

4. 接收消息(有趣的部分!)

const telegram = require('./init') // take a look at the init.js from the examples repo

const getChat = async () => {
  const dialogs = await telegram('messages.getDialogs', {
    limit: 50,
  })
  const { chats } = dialogs;
  const selectedChat = await selectChat(chats);

  return selectedChat;
}

此外,请查看原始仓库中的示例:

答案 1 :(得分:1)

如果您想在官方应用程序(网站,移动或桌面应用程序等)之外与Telegram数据交互,您将创建应用程序,因此您{{3}和/或使用任何符合您要求的现有应用程序(在您的情况下为机器人)。

让我强调,使用API​​系统似乎很难访问受限制的内容,如果您之前没有授予或添加权限来访问它......没有人希望任何人都可以访问任何数据。 ..

此致

答案 2 :(得分:0)

您可以使用以下库。

它们提供抽象来构建与电报交互的应用程序。有关如何使用telegram-js的示例,您可以使用https://github.com/dot-build/telegram-js/blob/master/sample.js

(谢谢@gokcand的反馈)