Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Latest commit

 

History

History
521 lines (415 loc) · 12.3 KB

API.md

File metadata and controls

521 lines (415 loc) · 12.3 KB

Methods

invokeApi(method, [params])

Invoke any method from .files/schema.txt

sendCode(phone_number)

Send code by phone number

signIn(phone_number, phone_code_hash, phone_code)

Sign in

signUp(phone_number, phone_code_hash, phone_code, first_name, [last_name])

Sign up

sendMessage(id, message)

Send message

startBot(botName)

Send bot command /start

sendSms(phone_number, phone_code_hash)

Send code via SMS

setConfig(config)

Configure your application

createChat(title, userIDs)

Create telegram chat (By default only creator will admin. In the future it will be changed)

getChatLink(chatID, [force])

Get chat invite link

getUserInfo()

Get self information

getUserPhoto([type], [size])

Get user photo

logOut()

Logout from Telegram

createChannel(title, [about])

Create channel (use carefully)

getHistory(params)

Get chat messages

sendFile(params)

Send file

downloadDocument(doc, [progress], [autosave])

Download Telegram document

joinChat(link)

Join to chat by link or hash

editChatAdmin(chatID, userID, [isAdmin])

Edit chat administrator

editChatTitle(chat_id, title)

Edit chat title

invokeApi(method, [params])

Invoke any method from .files/schema.txt

Param Type Description
method String Method name
[params] Object Parameters

Example

telegramApi.invokeApi('messages.getDialogs', {
    offset_peer: {_: 'inputPeerEmpty'},
    offset_date: 0,
    limit: 20
}).then(function(dialogResult) {
    /* Do something */
});

sendCode(phone_number)

Send code by phone number

Param Type Description
phone_number String Phone number

Example

telegramApi.sendCode('some_phone_number').then(function(sent_code) {
    if (!sent_code.phone_registered) {
        // New user
    }

    // phone_code_hash will need to sign in or sign up
    window.phone_code_hash = sent_code.phone_code_hash;
});

signIn(phone_number, phone_code_hash, phone_code)

Sign in

Param Type Description
phone_number String Phone number
phone_code_hash String Code hash (was received in sendCode method)
phone_code String Code by Telegram

Example

telegramApi.signIn('some_phone_number', window.phone_code_hash, '000000').then(function() {
    // Sign in complete
    delete window.phone_code_hash;
}, function(err) {
    switch (err.type) {
        case 'PHONE_CODE_INVALID':
            // alert "Phone code invalid"
            break;
        case 'PHONE_NUMBER_UNOCCUPIED':
            // User not registered, you should use signUp method
            break;
    }
});

signUp(phone_number, phone_code_hash, phone_code, first_name, [last_name])

Sign up

Param Type Description
phone_number String Phone number
phone_code_hash String Code hash (was received in sendCode method)
phone_code String Code by Telegram
first_name String User first name
[last_name] String User last name

Example

telegramApi.signUp('some_phone_number', window.phone_code_hash, '000000', 'John', 'Doe').then(function() {
    // Sign up complete
    delete window.phone_code_hash;
});

sendMessage(id, message)

Send message

Param Type Description
id Number Peer ID
message String Message text

Example

telegramApi.sendMessage(9999999999, 'Hey man!').then(function(updates) {
    // Do something
});

startBot(botName)

Send bot command /start

Param Type Description
botName String Bot name

Example

telegramApi.startBot('exampleBot').then(function(updates) {
    // Was invoked telegramApi.sendMessage(bot.id, '/start');
});

sendSms(phone_number, phone_code_hash)

Send code via SMS

Param Type Description
phone_number String Phone number
phone_code_hash String Code hash (was received in sendCode method)

Example

telegramApi.sendSms('some_phone_number', window.phone_code_hash).then(function() {
    // Do something
});

setConfig(config)

Configure your application

Param Type Description
config Object Configuration object
config.app.id Number Application ID
config.app.hash String App hash
config.app.version String App version
config.server.test Array.<Object> List test servers
config.server.production Array.<Object> List production servers

Example

telegramApi.setConfig({
    app: {
        id: 0, /* App ID */
        hash: 'qwertyasdfghzxcvbnqwertyasd', /* App hash */
        version: '0.0.0' /* App version */
    },
    server: {
        test: [
            {
                id: 2, /* DC ID */
                host: '0.0.0.0',
                port: 443
            }
        ],
        production: [
            {
                id: 2, /* DC ID */
                host: '0.0.0.0',
                port: 123
            }
        ]
    }
});

createChat(title, userIDs)

Create telegram chat (By default only creator will admin. In the future it will be changed)

Param Type Description
title String Chat title
userIDs Array.<Number> User ids list

Example

telegramApi.createChat('Chat title', [123456789]).then(function(updates) {
    // If you want all users to be administrators, use it
    return telegramApi.invokeApi('messages.toggleChatAdmins', {
        chat_id: updates.chats[0].id,
        enabled: false
    });
});

getChatLink(chatID, [force])

Get chat invite link

Param Type Description
chatID Number | String Chat id
[force] Boolean Force generate

Example

telegramApi.getChatLink(12456789, true).then(function(link) {
    // Do something
});

getUserInfo()

Get self information

Example

telegramApi.getUserInfo().then(function(user) {
    if (user.id) {
        // You have already signed in
    } else {
        // Open log in page
    }
});

getUserPhoto([type], [size])

Get user photo

Param Type Description
[type] String Photo type (values: byteArray (default), base64, blob)
[size] String Photo size (values: big (default), small)

Example

telegramApi.getUserPhoto('base64', 'small').then(function(base64) {
    $('img#avatar').attr('src', base64);
});

logOut()

Logout from Telegram

Example

telegramApi.logOut().then(function() {
    setTimeout(function() {
        // Do something after logouts
        // Use setTimeout (It will be fixed)
    }, 1500);
});

createChannel(title, [about])

Create channel (use carefully)

Param Type Description
title String Channel title
[about] String About text

Example

telegramApi.createChannel('New channel', 'This is example channel').then(function(updates) {
    var channel = updates.chats[0];

    /**
     * WARNING!
     * If you often call this method, you will receive a reply FLOOD_WAIT_{seconds}
     */
});

getHistory(params)

Get chat messages

Param Type Description
params Object Parameters
params.id Number Chat ID
[params.take] Number How much messages you will receive (default: 15)
[params.skip] Number How much messages you will skip (default: 0)
[params.type] String Chat type (for chat and channel use 'chat' (default))

Example

telegramApi.getHistory({
    id: 12345678,
    take: 50,
    type: 'user'
}).then(function(data) {
    var totalCount = data.count || data.messages.length;

    data.messages.forEach(function(message) {
        /**
         * message.from_id - Sender ID
         * message.date - Date
         * message.media - If message is Document or Photo
         * message.message - Message text
         */
    });
});

sendFile(params)

Send file

Param Type Description
params Object Parameters
params.id Number Peer ID
params.type String Chat type (for chat and channel use 'chat' (default))
params.file File File
[params.caption] String File caption

Example

telegramApi.sendFile({
    id: 123456789,
    type: 'user',
    file: $('input[type=file]').val(),
    caption: 'This is file'
}).then(function(updates) {
    // Do something
});

downloadDocument(doc, [progress], [autosave])

Download Telegram document

Param Type Description
doc Object Telegram document
[progress] function Progress callback
[autosave] Boolean Save file on device

Example

telegramApi.getHistory({
    id: 123456789,
    type: 'user',
    take: 1,
    skip: 0
}).then(function(data) {
    var message = data.messages[0];
    var doc = message.media.document;

    telegramApi.downloadDocument(doc, function(downloaded, total) {
        console.log('Loaded ' + downloaded + ' bytes. Total ' + total + ' bytes');
    }).then(function(result) {
        /**
         * result.bytes - file data
         * result.fileName - file name
         * result.type - file MIME-type
         */
    });
});

joinChat(link)

Join to chat by link or hash

Param Type Description
link String Chat invite link or hash

Example

telegramApi.joinChat('https://telegram.me/joinchat/some-hash').then(function(updates) {
    // Do something
});

editChatAdmin(chatID, userID, [isAdmin])

Edit chat administrator

Param Type Description
chatID Number Chat ID
userID Number User ID
[isAdmin] Boolean Admin status (default: true)

Example

telegramApi.editChatAdmin(123456789, 987654321, false).then(function() {
    // Do something
});

editChatTitle(chat_id, title)

Edit chat title

Param Type Description
chat_id Number Chat ID
title String New title

Example

telegramApi.editChatTitle(123456789, 'New title').then(function() {
    // Do something
});

To be continued