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
633 stars 118 forks source link

Question: Where find `access_hash` for create `inputChannel` #159

Closed alexpts closed 3 years ago

alexpts commented 3 years ago
const { MTProto } = require('@mtproto/core');

const api_id = xxx;
const api_hash = 'xxx';

const mtproto = new MTProto({api_id, api_hash});
mtproto.updateInitConnectionParams({app_version: '10.0.0'});

(async () => {
    let {user} = await mtproto.call('auth.importBotAuthorization', {
        bot_auth_token: "xxxx"
    });

    try {
        result = await mtproto.call('channels.getFullChannel', {
            channel: {
                _: "inputChannel",
                channel_id: -1001421468496,
                access_hash: +user.access_hash,
            }
        });
    } catch (error) {
        console.error(error);
        return;
    }
})();

I have error:

{
  "_": "mt_rpc_error",
  "error_code": 400,
  "error_message": "CHANNEL_INVALID"
}

How I cant get all users ids from supergroup via MTproto an bot token?

gino8080 commented 3 years ago

same question

alexpts commented 3 years ago

up

alik0211 commented 3 years ago

Why are you passing the user's access_hash? I think you need to use the access_hash of the channel

alexpts commented 3 years ago

@alik0211 Where I can find access_hash of the channel via bot account?

atiquefiroz commented 3 years ago

Please use this method "messages.getAllChats" or corresponding bot method. messages.getAllChats From there you will get all the access_hash corresponding to each channels, you can save and reuse when needed.

@alik0211 Where I can find access_hash of the channel via bot account?

alexpts commented 3 years ago

@atiquefiroz, @alik0211 My code:

try {
        result = await mtproto.call('messages.getAllChats', {
            except_ids: []
        });
    } catch (error) {
        console.error(error);
        return;
    }

I have error on method messages.getAll.Chats

{
  "_": "mt_rpc_error",
  "error_code": 400,
  "error_message": "BOT_METHOD_INVALID"
}

This method not available for bot

atiquefiroz commented 3 years ago

@atiquefiroz, @alik0211 My code:

try {
        result = await mtproto.call('messages.getAllChats', {
            except_ids: []
        });
    } catch (error) {
        console.error(error);
        return;
    }

I have error on method messages.getAll.Chats

{
  "_": "mt_rpc_error",
  "error_code": 400,
  "error_message": "BOT_METHOD_INVALID"
}

This method not available for bot

I dont see any methods on telegram official website, i guess you will have to use update hook to read incoming chat, and you can reply on that through bot.

alexpts commented 3 years ago

@atiquefiroz Bot API don`t have access_hash on incoming webhook message - https://core.telegram.org/bots/api#chat.

I want get all members in group/supergroup.

Similar case -> https://docs.madelineproto.xyz/getPwrChat.html. I have supergroup scalar id only for fetch all members (https://docs.madelineproto.xyz/API_docs/types/InputChannel.html).

alexpts commented 3 years ago

I get access_hash via https://docs.madelineproto.xyz/getPwrChat.html. But it not work too:

Full code without real secret:

const {MTProto} = require('@mtproto/core');
const {tempLocalStorage} = require('@mtproto/core/src/storage/temp');

const api_id = 11111;
const api_hash = 'xxxxx';

const mtproto = new MTProto({
    api_id,
    api_hash,
    customLocalStorage: tempLocalStorage,
});

mtproto.updateInitConnectionParams({
    app_version: '10.0.0',
});

// 3. Get the user country code
(async () => {
    let user = await mtproto.call('auth.importBotAuthorization', {
        bot_auth_token: "1111111:xxxxxxxxxx"
    });

    try {
        let result = await mtproto.call('channels.getFullChannel', {
            channel: {
                _: "inputChannel",
                channel_id: -1001421468496,
                access_hash: 2400495907992991377 // get via madelineproto for chat -1001421468496
            }
        });
    } catch (error) {
        console.error(error);
        return;
    }
})();

I have error:

{
  "_": "mt_rpc_error",
  "error_code": 400,
  "error_message": "CHANNEL_INVALID"
}
alexpts commented 3 years ago

Any ideas how debug it?

alik0211 commented 3 years ago

@alexpts try use contacts.resolveUsername for get information about the channel

Example - https://mtproto-core.js.org/docs/call-the-telegram-methods#parse-all-messages-from-channel

alexpts commented 3 years ago

I can't get the list of users in supergroup through the bot account on version 6.0.1.

let peer = await api.call('contacts.resolveUsername', {
    username: 'my_some_bot',
});

I added my bot to group and to supergoup. Peer have not any group/supergoup. Property peer.chats is empty array.

alexpts commented 3 years ago

I see implementation of MadelineProto.

Method https://docs.madelineproto.xyz/getPwrChat.html can return full info by channel_id only.

In code MadelineProto send access_hash = 0 - https://github.com/danog/MadelineProto/blob/8b15b79d8312aa6456e69417450e647b7d7fc1ef/src/danog/MadelineProto/MTProtoTools/PeerHandler.php#L587 and it work

iamDonkey commented 3 years ago

@alexpts

I can't get the list of users in supergroup through the bot account on version 6.0.1.

let peer = await api.call('contacts.resolveUsername', {
    username: 'my_some_bot',
});

I added my bot to group and to supergoup. Peer have not any group/supergoup. Property peer.chats is empty array.

you should resolve username of group chat not the bot username. and about madeline proto it is a heavy and buggy library please don't expect this project to be of same kind.

alexpts commented 3 years ago

Group is private and have not username

iamDonkey commented 3 years ago

your bot is already a member of the group?

IRGC commented 3 years ago

if its already member of group/channel use updates handler to get access_hash https://mtproto-core.js.org/docs/setup-handle-updates

alexpts commented 3 years ago

if its already member of group/channel use updates handler to get access_hash https://mtproto-core.js.org/docs/setup-handle-updates

@IRGC, it is webhooks for bot? I will try it.


I found problem with error CHANNEL_INVALID. Channel_id is not equal chat.id from webhook bot api. (https://github.com/danog/MadelineProto/blob/8b15b79d8312aa6456e69417450e647b7d7fc1ef/src/danog/MadelineProto/MTProtoTools/PeerHandler.php#L63)

I will post full work code here.

IRGC commented 3 years ago

if its already member of group/channel use updates handler to get access_hash https://mtproto-core.js.org/docs/setup-handle-updates

@IRGC, it is webhooks for bot? I will try it.

No. its for getting updates using this library similar to what we receive in webhook.

I found problem with error CHANNEL_INVALID. Channel_id is not equal chat.id from webhook bot api. (https://github.com/danog/MadelineProto/blob/8b15b79d8312aa6456e69417450e647b7d7fc1ef/src/danog/MadelineProto/MTProtoTools/PeerHandler.php#L63)

I will post full work code here.

for channel_id remove prefix -100 from chat.id received in webhook.

alexpts commented 3 years ago

No. its for getting updates using this library similar to what we receive in webhook.

What native methods it use? Where I can read more details about?

for channel_id remove prefix -100 from chat.id received in webhook. Thanks. It more simple that in MadelineProto. MadelineProto code is:

let newId = -id - Math.pow(Math.floor(Math.log10(-id)));