0.2.17 ā€¢ Published 6 years ago

tbn v0.2.17

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

install

npm install tbn

tbn object

method

  • setToken(token) : set token

    • token : string
  • setDefaultChatCookie(cookie) : set default chat cookie

    • cookie : object
  • setDefaultFromCookie(cookie) : set default from cookie

    • cookie : object
  • polling() : start polling mode

  • webhook(url, key, cert) : start webhook mode

    • url : string
    • key : string(file path)
    • cert : string(file path)

please refer to here to check available bot-method list

  • command(bot-method, parameters,, fail callback) : send a command to the bot
    • bot-method : string
    • parameters : object
    • callback : function(resJson)
      • resJson : object

ā€» resJson contains error_code and description when error occurs(if it can not receive the result from telegram server for whatever reason, error_code is 700)

ā€» inofficially, resJson contains name of method(resJson.command) and parameters(resJson.args)

make parameters by init callback

'use strict';

const tbn = require('tbn');
const fs = require('fs');
const https = require('https');

const status = {
    DEFAULT: 0,
    BUSY: 1
};

tbn.setToken(fs.readFileSync('./token').toString());
tbn.setDefaultFromCookie({status: status.DEFAULT});
tbn.on('message', (update) => {
    if (update.message.from.cookie.status === status.DEFAULT) {
        update.message.from.cookie.status = status.BUSY;

        tbn.command('sendDocument', {
            init: (ok, no) => {
                https.get('https://telegram.org', (res) => {
                    res.file_name = 'telegram.html';

                    let args = {
                        chat_id: update.message.chat.id,
                        reply_to_message_id: update.message.message_id,
                        document: res
                    };

                    ok(args);
                }).on('error', (e) => {
                    no(e);
                });
            }
        }, () => {
            update.message.from.cookie.status = status.DEFAULT;
        }, () => {
            tbn.command('sendMessage', {
                chat_id: update.message.chat.id,
                reply_to_message_id: update.message.message_id,
                text: 'error...'
            });

            update.message.from.cookie.status = status.DEFAULT;
        });
    }
});
tbn.polling();

inofficial bot-method

  • getFileStream : get readable stream of the file
    • file_path : string
    • return resJson containing {file_stream : readable stream} as result

property

  • retry : count number to retry when error occurs on telegram side or network (default: 3)
  • waitBeforeRetry : wait time(ms) before to retry (default: 2000)

event

please refer to here to check available event list

  • message(update)
  • edited_message(update)
  • channel_post(update)
  • edited_channel_post(update)
  • inline_query(update)
  • chosen_inline_result(update)
  • callback_query(update)
  • shipping_query(update)
  • pre_checkout_query(update)
  • exit()

use cookie and local

update.event.chat.cookie : persisted data by chat_id and user_id

update.event.from.cookie : persisted data by user_id

update.event.local : local data for this event only

ā€» if chat_id equals user_id, update.event.chat.cookie equals update.event.from.cookie

logging

fhrj package for logging

examples

echo text

'use strict';

const tbn = require('tbn');
const fs = require('fs');

tbn.setToken(fs.readFileSync('./token').toString());
tbn.on('message', (update) => {
    tbn.command('sendMessage', {
        chat_id: update.message.chat.id,
        reply_to_message_id: update.message.message_id,
        text: `[echo] ${update.message.text}`
    });
});
tbn.polling();

send document

'use strict';

const tbn = require('tbn');
const fs = require('fs');

tbn.setToken(fs.readFileSync('./token').toString());
tbn.on('message', (update) => {
    let rs = fs.createReadStream('./afile');

    rs.file_name = 'a.txt';

    tbn.command('sendDocument', {
        chat_id: update.message.chat.id,
        document: rs
    });
});
tbn.polling();

use getfilestream

'use strict';

const tbn = require('tbn');
const fs = require('fs');

tbn.setToken(fs.readFileSync('./token').toString());
tbn.on('message', (update) => {
    if ('document' in update.message === true) {
        new Promise((ok, no) => {
            tbn.command('getFile', {
                file_id: update.message.document.file_id
            }, (resJson) => {
                ok(resJson);
            });
        }).then((resJson) => {
            tbn.command('getFileStream', {
                file_path: resJson.result.file_path
            }, (resJson) => {
                let ws = fs.createWriteStream('./aFile');

                resJson.result.file_stream.pipe(ws).on('finish', () => {
                    tbn.command('sendMessage', {
                        chat_id: update.message.chat.id,
                        reply_to_message_id: update.message.message_id,
                        text: 'download complete'
                    });
                });
            });
        });
    }
});
tbn.polling();

use cookie

'use strict';

const tbn = require('tbn');
const fs = require('fs');

const status = {
    DEFAULT: 0,
    BUSY: 1
};

tbn.setToken(fs.readFileSync('./token').toString());
tbn.setDefaultFromCookie({status: status.DEFAULT, count: 0});
tbn.on('message', (update) => {
    if (update.message.from.cookie.status === status.DEFAULT) {
        update.message.from.cookie.status = status.BUSY;
        update.message.from.cookie.count += 1;

        tbn.command('sendMessage', {
            chat_id: update.message.chat.id,
            text: `[count:${update.message.from.cookie.count}] I will deny your request ${update.message.from.cookie.count * 10} seconds.`
        }, (resJson) => {
            setTimeout(() => {
                update.message.from.cookie.status = status.DEFAULT;
            }, update.message.from.cookie.count * 10000);
        }, () => {
            update.message.from.cookie.status = status.DEFAULT;
        });
    } else {
        tbn.command('sendMessage', {
            chat_id: update.message.chat.id,
            text: 'šŸ˜œ'
        });
    }
});
tbn.polling();

use local

'use strict';

const tbn = require('tbn');
const fs = require('fs');
const token = fs.readFileSync('./token').toString();

tbn.setToken(token);
tbn.on('message', (update) => {
    if ('document' in update.message === true) {
        new Promise((ok, no) => {
            tbn.command('sendMessage', {
                chat_id: update.message.chat.id,
                reply_to_message_id: update.message.message_id,
                text: 'wait...'
            }, (resJson) => {
                update.message.local.message_id = resJson.result.message_id;

                ok();
            });
        }).then(() => {
            return new Promise((ok, no) => {
                tbn.command('getFile', {
                    file_id: update.message.document.file_id
                }, (resJson) => {
                    ok(resJson);
                });
            })
        }).then((resJson) => {
            tbn.command('editMessageText', {
                chat_id: update.message.chat.id,
                message_id: update.message.local.message_id,
                text: `https://api.telegram.org/file/bot${token}/${resJson.result.file_path}`
            });
        });
    }
});
tbn.polling();

webhook

'use strict';

const tbn = require('tbn');
const fs = require('fs');

tbn.setToken(fs.readFileSync('./token').toString());
tbn.on('edited_message', (update) => {
    tbn.command('sendVenue', {
        chat_id: update.edited_message.chat.id,
        reply_to_message_id: update.edited_message.message_id,
        latitude: -90 + Math.random() * 180,
        longitude: -180 + Math.random() * 360,
        title: 'where it is?',
        address: 'i don\'t know either'
    });
});
// ports currently supported for Webhooks: 443, 80, 88, 8443.
tbn.webhook('https://hostname:8443/path', './key', './cert');
0.2.17

6 years ago

0.2.16

6 years ago

0.2.16-dev-0006

6 years ago

0.2.16-dev-0005

6 years ago

0.2.16-dev-0004

6 years ago

0.2.16-dev-0003

6 years ago

0.2.16-dev-0002

6 years ago

0.2.16-dev-0001

6 years ago

0.2.15

7 years ago

0.2.14

7 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.11

9 years ago

0.1.10

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago