【发布时间】:2018-04-04 12:39:04
【问题描述】:
我想在 nodejs 中有一个非常简单的客户端(一个示例),它可以接收来自我的电报联系人的消息。我只是在互联网上搜索,但我只得到机器人样本。我想在我无权授予我的机器人特权的情况下接收群组消息,因此我想知道我是否可以在没有机器人作为中介的情况下接收自己的消息。
【问题讨论】:
我想在 nodejs 中有一个非常简单的客户端(一个示例),它可以接收来自我的电报联系人的消息。我只是在互联网上搜索,但我只得到机器人样本。我想在我无权授予我的机器人特权的情况下接收群组消息,因此我想知道我是否可以在没有机器人作为中介的情况下接收自己的消息。
【问题讨论】:
如果您想在官方应用程序之一(网站、移动或桌面应用程序等...)之外与 Telegram 数据进行交互您将必须创建一个应用程序,因此您 will be required to generate API key 和/或使用任何现有的符合您要求的应用程序(在您的情况下为机器人)。
让我强调一下,使用 API 系统似乎很难访问受限制的内容,如果您之前没有授予或添加访问权限...没有人希望任何人都可以访问任何数据...
问候
【讨论】:
您可以使用以下库。
它们提供抽象来构建应用程序以与电报交互。有关如何使用 telegram-js 的示例,您可以使用https://github.com/dot-build/telegram-js/blob/master/sample.js。
(感谢@gokcand 的反馈)
【讨论】:
嗯... 其他答案给出了 unmaintained 库中的示例。因此,您不应依赖这些库。
您应该使用最新的 Telegram 客户端库 telegram-mtproto
api_id 和api_hash:
npm install telegram-mtproto@beta --save
api_id 和 api_hash 以及您的 phone number: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();
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;
}
【讨论】: