0

I am new to using Stack Overflow, and I apologize in advance if I make any mistakes or do something incorrectly.

I've been struggling with a problem for the past 3 days and can't figure out how to get photo/images messages from a channel in the form of a URL link. The link looks like this: https://cdn5.telegram-cdn.org/file/.....jpg

So far, I have managed to get regular text messages and identify URLs that contain "cdn5". However, I don't know how to handle private rooms.

Below is a snippet of my code:

const MTProto = require('@mtproto/core');
const getSRPParams = require('@mtproto/core');
const path = require('path');
require('dotenv').config();

const api_id = 'MY API ID';
const api_hash = 'MY API HASH';

const mtproto = new MTProto({
    api_id,
    api_hash,
    storageOptions: {
        path: path.resolve(__dirname, './data/1.json'),
      },
});

function startListener() {
    console.log('[+] starting listener');
    mtproto.updates.on('updates', async ({ updates }) => {
        console.log('updates = ', updates)
        const newChannelMessages = updates.filter((update) => update._ === 'updateNewChannelMessage').map(({ message }) => message)
        for (const message of newChannelMessages) {
            console.log('message = ', message)
            if(message.message !== '') {
            
                webhookClient.send({
                    content: message.message,
                    username: process.env.NAME_BOT,
                    avatarURL: process.env.AVATAR_BOT
                });
                console.log(formattedDate + ' — ' + message.message);
            }
        }
    });
}

mtproto
    .call('users.getFullUser', {
        id: {
            _: 'inputUserSelf',
        },
    })
    .then(startListener)
    .catch(async error => {

        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');
                startListener()
            });
    })

And here is an example of the console.log output:

[+] starting listener

updates =  [
  {
    _: 'updateReadChannelInbox',
    flags: 0,
    channel_id: '17518-----',
    max_id: 205,
    still_unread_count: 0,
    pts: 206
  },
  {
    _: 'updateNewChannelMessage',
    message: {
      _: 'message',
      flags: 17922,
      out: true,
      mentioned: false,
      media_unread: false,
      silent: false,
      post: true,
      from_scheduled: false,
      legacy: false,
      edit_hide: false,
      pinned: false,
      noforwards: false,
      id: 205,
      peer_id: [Object],
      date: 1689558480,
      message: '',
      media: [Object],
      views: 1,
      forwards: 0
    },
    pts: 206,
    pts_count: 1
  }
]

message =  {
  _: 'message',
  flags: 17922,
  out: true,
  mentioned: false,
  media_unread: false,
  silent: false,
  post: true,
  from_scheduled: false,
  legacy: false,
  edit_hide: false,
  pinned: false,
  noforwards: false,
  id: 205,
  peer_id: { _: 'peerChannel', channel_id: '17518-----' },
  date: 1689558480,
  message: '',
  media: {
    _: 'messageMediaPhoto',
    flags: 1,
    spoiler: false,
    photo: {
      _: 'photo',
      flags: 0,
      has_stickers: false,
      id: '61722004653417-----',
      access_hash: '43389132237044-----',
      file_reference: [Uint8Array],
      date: 1689558480,
      sizes: [Array],
      dc_id: 5
    }
  },
  views: 1,
  forwards: 0
}

Thank you very much for your help.

I want to extract image messages in the form of URL links like https://cdn5.telegram-cdn.org/file/... in order to use them elsewhere.

0 Answers0