npm -g install tgbgit clone https://github.com/Daeren/tgb.git
await "T" "sendmessage" 0 "+" proxyawait
> tgb-cli --polling TOKEN --echo -j
- QoS: dev
- WebHook: +
- Spy: +
- Download: +
- Proxy: +
- File as Buffer: +
- Abort/Pause: +
- TgUpload: +
- Markup: +
- Extensibility: +
- CLI: +
- Entities: +
- Redirect: +
- HashTable, Array and Map as a data source: +
- Without Dependencies: +
const tgb = ; const token = processenvTELEGRAM_BOT_TOKEN;const polling entities = tgb; //----------------------------------------------------- ; // send: tg @gamebot /start x http://db.gg // tgb.polling(token, onMsg(data, bot)).stop().start().stopped;// tgb.polling(token, options{proxy, limit, timeout, interval}, onMsg(data, bot));// tgb.polling(bot, onMsg(data)); // https://core.telegram.org/bots/api#getupdates// https://core.telegram.org/bots/api#messageentity
WebHook
const tgb = ; const bot = ;const webhook = tgb; //----------------------------------------------------- void { const wh = await ; const url = await wh;}; void { const wh = await ; // srv|cl errors const url = await wh; // url = https://db.gg:8443/tg_bot_<sha256(token)>}; // wh = await webhook(1490); // *:1490// wh = await webhook({host: "localhost", port: 1490}); // url = await wh.bind(otherBot, "666.io:88", cb); // with api.setWebhook// wh = wh.set(otherBot2, url, cb); // with no api.setWebhook // url = await wh.unbind(otherBot); // with api.deleteWebhook// wh = wh.delete(otherBot2); // with no api.deleteWebhook // await wh.close(); // wh.bind(bot, options[url|objOpt.setWebhook], onMessage)// wh.unbind(bot)// wh.set(bot, url, onMessage)// wh.delete(url) // https://core.telegram.org/bots/api#setwebhook// https://core.telegram.org/bots/self-signed
Spy
const bot = ;const webhook spy entities = tgb; //----------------------------------------------------- void { const watch = ; const wh = await ; const url = await wh; { const message = data; ifmessage const es = ; forlet type of Object estype; ifes delete datamessage; // ^---| Mutation: bot_command, hashtag, etc watch; } // Second ; // <|- // | // Third v---| Nested objects | ; // | // | // Second | ; // <|- // v---| Base: always first ; // ^---| Scope // v---| Scope |-----v ; ; // ^---| Base: always first ; // ^---| Scope /******************************************************* Nested objects: sort by depth |---v [message[...], bot_command/start, hashtag#win] ^---| The Spy calls only the first available scope | The Spy calls all listeners synchronously in the order in which they were registered***/}; void { const watch = ; // Change the sort order ; // Third ; // Second ; // Base: always first ;}; // EventEmitter => spy // w = spy();// w(type, listener(val, bot, data)); // set: listener.destroy()// w.on(type, listener(val, bot, data));// w.update(data[, bot]); // https://core.telegram.org/bots/api#update
Download
const tgb = ; const bot = ;const download = tgb; const fileId = "AgADAgAD36gxGwWj2EuIQ9vvX_3kbh-cmg4ABDhqGLqV07c_phkBAAEC"; //----------------------------------------------------- void { await ;}; // await tgb.download(token, fileId[, dir = "./", filename = ""]); // https://core.telegram.org/bots/api#file
Proxy
const bot = ; void { botproxy = "127.0.0.1:1337"; // Only HTTPS try await bot; await bot; catche console; };
Abort/Pause/Resume
void { const bot = ; const params = "chat_id": "0" "audio": "O://1.mp3"; const res req = bot; ; ; ; console; // | If a request has been aborted, this value is the time when the request was aborted, // | in milliseconds since 1 January 1970 00:00:00 UTC. // +-----------v console; // response was received -^ ^- request has been sent};
Tg Upload
const bot = ; // Youbot; // Tgbot; // https://core.telegram.org/bots/api#sending-files
Markup
const bot = ;const markup = tgb; // Setbot; // Removebot; // .removeKeyboard() // Replybot; // .forceReply() // markup(message).[METHOD] // markup.keyboard(kb, oneTime[, resize = true, selective = false])// markup.removeKeyboard([selective = false])// markup.forceReply([selective = false])// markup.inlineLink(text, url) // or [text, url], ... // https://core.telegram.org/bots/api#replykeyboardmarkup
Extensibility
const tgb = ;const Client = tgb; { supertoken; thismsg = msg; } { return this; } { return super; } const bot = processenvTELEGRAM_BOT_TOKEN "v=vc-PJPrueXY";const res req = bot; console;
File as Buffer
const fs = ;const bot = ; bot;
CLI
> SET TELEGRAM_BOT_TOKEN=1:XXXX > tgb-cli --method sendPhoto --dchat_id 0 --dphoto "J://test.jpg"> tgb-cli --method getMe -j > tgb-cli --method getMe --token 0:XXXX> tgb-cli --method getMe --token 0:XXXX --proxy "127.0.0.1:1337" > tgb-cli --method sendMessage --data "{\"chat_id\":0,\"text\":\"Hi yo\"}"> tgb-cli --method sendMessage --dchat_id 0 --data "{\"chat_id\":1,\"text\":\"Hi yo\"}" > tgb-cli --download TOKEN --name x --id "AgADAgAD36gxGwWj2EuIQ9vvX_3kbh-cmg4ABDhqGLqV07c_phkBAAEC"> tgb-cli --download --dir "./temp/" --id "AgADAgAD36gxGwWj2EuIQ9vvX_3kbh-cmg4ABDhqGLqV07c_phkBAAEC"
QoS (Future development plans)
Considering Telegram's Bot documentation, currently the maximum amount of messages being sent by bots is limited to 30 messages/second for all ordinary messages and 20 messages/minute for group messages. When your bot hits spam limits, it starts to get 429 errors from Telegram API. And assuming that error handling in such case usually is coded as simple retrials, the running machine would spend a lot of CPU time retrying (or got locked down, depending on bot implementation details). And constantly retrying to send messages while ignoring API errors could result in your bot being banned for some time.
That means, if you're making a production-ready bot, which should serve numerous users it's always a good idea to use throughput limiting mechanism for messages being sent. This way you could be sure that all messages would be delivered to end-users as soon as possible in ordered way.
const tgb = ;const markup qos = tgb; const bot = ;botqos = ; const res req = bot; // https://core.telegram.org/bots/faq#broadcasting-to-users// https://en.wikipedia.org/wiki/Quality_of_service
Misc
/* All methods in the Bot API are case-insensitive (.buffer, .json, .require) message: buffer, stream, string location|venue|contact: buffer, stream, string photo|audio|voice|video|document|sticker|video_note: buffer, stream, filepath, url, file_id certificate: buffer, stream, filepath, url - tgb.buffer(proxy, token, method, data, callback(error, buf, res)) tgb.json(proxy, token, method, data, callback(error, json, res)) - x = tgb(token); x.token; // Read|Write x.proxy; // Read|Write x.url; // Read (webhook.bind) await x.method([data, proxy]); await x(method[, data, proxy]); - client = await tgb(token, method[, data, proxy]); [client, request] = tgb(token, method[, data, proxy]); client.request === request; request.pause(); await client; ~~~ error.response = response; error.data = data; error.code = data.error_code; error.retryAfter = data.parameters.retry_after; error.migrateToChatId = data.parameters.migrate_to_chat_id; https://core.telegram.org/bots/api#responseparameters ~~~ Sys Code | EBADPROXY EBADREQUEST EBADDATA EWCMAXSIZE EWCLONGREDIRECT ~~~ Tg Code | tgb.ERR_INTERNAL_SERVER tgb.ERR_NOT_FOUND tgb.ERR_FORBIDDEN tgb.ERR_MESSAGE_LIMITS tgb.ERR_USED_WEBHOOK tgb.ERR_INVALID_TOKEN*/
Me? Him? Me? You? Me? ... Him? Me ... npm -g i tgb
.... Whaat is Love ♫•¨•.¸¸♪
Goals:
- High stability;
- Low memory usage;
- Maximum performance;
- Flexibility.
License
MIT