tbn

0.2.17 • Public • Published

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][, success callback],[, 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 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');

Readme

Keywords

Package Sidebar

Install

npm i tbn

Weekly Downloads

1

Version

0.2.17

License

MIT

Unpacked Size

29.2 kB

Total Files

3

Last publish

Collaborators

  • lifili