A
A
Anton Anton2017-02-24 12:45:17
Node.js
Anton Anton, 2017-02-24 12:45:17

How to run a bot via telegram api?

I automate human actions in the telegram client. The task is this: create a group with a list of contacts, add a bot to it and run it. Everything worked out except for the last points (well, still make the bot an admin, but this is impossible without adding the bot to the group, and this does not work :) ) I use https://github.com/zerobias/telegram-mtproto
as a library that provides API

bot = {user_id: 120593101, access_hash: ''}; // это бот chgk_bot, но и с другими не работает
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_code": 400, "error_message": "BOT_INVALID", whether I'm trying to start a bot in an existing chat, or for myself
, trying to add a bot to an existing chat also fails:
return client.callApi('messages.addChatUser', {chat_id: chat, user_id: userBot, fwd_limit:1});

error "error_message": "USER_ID_INVALID"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2017-03-16
@Fragster

The point is that your access_hash is empty. Try contacts.resolveUsername method

const { InputUser, InputPeerSelf } = telegram.schema.type
const peerSelf = new InputPeerSelf()
const user = client.callApi('contacts.resolveUsername', { username: 'chgk_bot' })
const startBot = user.then(({ users, peer }) => {
  const access_hash = users.list[0].access_hash
  const user_id = peer.user_id
  const userBot = new InputUser({ props: { user_id, access_hash } })
  return client.callApi('messages.startBot', {bot: userBot, peer: peerSelf, random_id:123456, start_param:'start'})
})
startBot.then(result => console.log(result))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question