alik0211 / mtproto-core

Telegram API JS (MTProto) client library for Node.js and browser
https://mtproto-core.js.org
GNU General Public License v3.0
625 stars 111 forks source link

Facing Invalid Channel Problem #277

Open PBhadoo opened 1 year ago

PBhadoo commented 1 year ago

Describe the bug Unable to do anything with a channel

Code example

const path = require('path');
const MTProto = require('@mtproto/core');

const api_id = 202xxxxx3;
const api_hash = 'c95fbxxxxxxxxxxxx7d9b0f0';
const bottoken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const bot_id = '6140xxxx684';
const access_hash = '4443xxxxxxxxx438';
const channelId = '-100178570xxxx';
const messageId  = '43759';
const server = { webogram: true };

// 1. Create instance
const client = new MTProto({
  api_id,
  api_hash,
  bottoken,
  server,
  storageOptions: {
      path: path.resolve(__dirname, './data/1.json'),
    },
});

async function downloadFileFromChannel(mtproto, channelId, messageId) {
  // Get the message object for the specified message ID
  try {
    const result = await client.call('channels.getMessages', {
      channel: {
        _: 'inputChannel',
        channel_id: channelId,
        access_hash: access_hash
      },
      id: {
        _: 'inputMessageID',
        id: 43759,
      }
    });
    console.log(result)
    const message = result.messages[0];
    if (message.media._ !== 'messageMediaDocument') {
      throw new Error('The message does not contain a document');
    }

    // Get the file location from the message object
    const document = message.media.document;
    const fileLocation = {
      _: 'inputDocumentFileLocation',
      id: document.id,
      access_hash: document.access_hash,
      file_reference: document.file_reference
    };

    // Download the file
    const fileBytes = await downloadFile(mtproto, fileLocation);
    console.log(fileBytes);
  } catch (e) {
    console.log(e)
  }
}

async function importBotAuthorization(mtproto, botToken) {
    try {
      const result = await client.call('auth.importBotAuthorization', {
        api_id: api_id,
        api_hash: api_hash,
        bot_auth_token: botToken
      });
      const { bot } = result;
      console.log(result);
    } catch (e) {
      console.log(e)
    }
}

async function getChannelAccessHash(channelId) {
  try {
    const inputChannel = {
      _: 'inputChannel',
      channel_id: channelId,
      //access_hash: 0
    };

    const chatFull = await client.call('channels.getFullChannel', {
      channel: inputChannel
    });

    const accessHash = chatFull.full_chat.access_hash;
    console.log('Access hash for the channel:', accessHash);

    return accessHash;
  } catch (error) {
    console.error('Error while getting access hash:', error);
  }
}

//importBotAuthorization(mtproto, bottoken);
getChannelAccessHash(channelId)
//downloadFileFromChannel(mtproto, channelId, messageId);

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Context:

Additional context Add any other context about the problem here.