zerobias / telegram-mtproto

Telegram client api (MTProto) library
MIT License
617 stars 137 forks source link

How to add bot to the chat and/or start bot with api? #15

Closed FragsterAt closed 7 years ago

FragsterAt commented 7 years ago
bot = {user_id: 120593101, access_hash: ''};
peerSelf = new telegram.schema.type.InputPeerSelf();
userBot = new telegram.schema.type.InputUser({props: bot});
    return client.callApi('messages.startBot', {bot: userBot, peer: peerSelf, random_id:123456, start_param:'start'});

returns error "error_code": 400, "error_message": "BOT_INVALID" I tried peer self, or peer with existing chat - no difference

when i try to add bot to existing chat: return client.callApi('messages.addChatUser', {chat_id: chat, user_id: userBot, fwd_limit:1}); it returns "error_message": "USER_ID_INVALID"

id is bot_id from contacts.search '@chgk_bot' (I also try some other bots, with same result)

FragsterAt commented 7 years ago

when we use bots, access_hash is important. this code works:

    return client.callApi('contacts.search', {q: '@botname_bot', limit: 10});
}).then(result => {
    let userBot = telegramHelper.createObject({user_id: result.users.list[0].id, access_hash: result.users.list[0].access_hash}, 'InputUser');
    let peerChat = telegramHelper.createObject({chat_id: chat}, 'InputPeerChat');
    return client.callApi('messages.addChatUser', {chat_id: chat, user_id: userBot, fwd_limit:0});
}).then(result => {
    var randomID = Math.floor(Math.random() * 0xFFFFFFFF).toString();
    return client.callApi('messages.startBot', {bot: userBot, peer: peerChat, random_id:randomID, start_param:'start'});
....