Well...其他答案给出了例子无人维护图书馆。因此,您不应该依赖这些库。
See: telegram.link 已失效 https://github.com/enricostara/telegram.link/issues/36
您应该使用最新的 Telegram 客户端库,它是电报 mtproto https://github.com/zerobias/telegram-mtproto
1. 获取您的api_id
and api_hash
from:
电报应用程序 https://core.telegram.org/api/obtaining_api_id
2. 安装所需的客户端库:
npm install telegram-mtproto@beta --save
3. 初始化您的 Node.js 应用程序 with api_id
and api_hash
你从电报应用程序 https://my.telegram.org/auth?to=apps和你的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();
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;
}
此外,看一下原始存储库中的示例:
-
使用示例 https://github.com/zerobias/telegram-mtproto/tree/master/examples
- 读取聊天记录 https://github.com/zerobias/telegram-mtproto/blob/master/examples/chat-history.js
- 更新个人资料信息 https://github.com/zerobias/telegram-mtproto/blob/master/examples/update-profile.js