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

3

20

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.

Crumple answered 23/10, 2017 at 20:48 Comment(0)
F
44

Well... Other answers give examples from unmaintained libraries. Hence, you should not rely on these libraries.

See: telegram.link is dead


You should use the newest Telegram client library which is telegram-mtproto

1. Obtain your api_id and api_hash from:

Telegram Apps

2. Install the required client library:

npm install telegram-mtproto@beta --save

3. Initialize your node.js application with api_id and api_hash you got from Telegram Apps and with your 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. Receive messages (The fun part! πŸ‘¨πŸ»β€πŸ’»)

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;
}

Furthermore, Take a look at the examples from the original repo:

Frustration answered 5/1, 2018 at 23:1 Comment(2)
why 2FA code is here ..can you explain authentication flow ? – Rolland
@GauravAgrawal Please look at this example: github.com/zerobias/telegram-mtproto/blob/develop/examples/… – Frustration
S
1

If you want to interact with Telegram data outside one of the official applications (website, mobile or desktop app etc...) you will have to create an App, so you will be required to generate API key and/or use any already existing App which fit your requirement (bots in your case).

Let me underline that with API system it appear difficult to get access to something which is restricted, if you don't have previously grant or add privileges to access it ... Nobody want that anyone could access any data...

Regards

Sprage answered 30/12, 2017 at 17:24 Comment(0)
E
0

You can use the following libraries.

They provide abstractions to build applications to interact with telegram. for an example on how to use telegram-js, you can use https://github.com/dot-build/telegram-js/blob/master/sample.js.

(Thank you @gokcand for the feedback)

Erme answered 5/1, 2018 at 9:49 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.