npm -g install tgb
git clone https://github.com/Daeren/tgb.git
await require("tgb")("TK", "sendmessage", [0, "+"], proxy)
await require("tgb").sendMessage("TK", {chat_id: 0, text: "+"}, proxy)
Full Bot API 3.5
- 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, path, url, file_id
- certificate: buffer, stream, path, url
Goals:
- High stability;
- Low memory usage;
- Maximum performance;
- Flexibility.
const tgb = require("tgb");
const {polling} = tgb;
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
polling(
bot.token,
function({message}) {
bot.sendMessage([message.from.id, `Hi: ${message.text}`]);
})
.catch(function(error) {
if(error.code === bot.ERR_INVALID_TOKEN) {
this.stop();
console.log("There's a problem with the token...");
}
else {
delete error.response;
console.log(error);
}
});
Proxy
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const proxy = "127.0.0.1:1337";
bot.proxy = proxy;
void async function () {
try {
console.log(await bot.getMe());
console.log(await bot.getMe(null, "0.0.0.0:1337"));
} catch(e) {
console.log(e);
}
}();
Abort/Pause/Resume
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
void async function () {
const [res, req] = bot.sendAudio({"chat_id": "0", "audio": "O://1.mp3"});
setTimeout(() => {
req.pause();
}, 500);
setTimeout(() => {
req.resume();
}, 2500);
setTimeout(() => {
req.abort();
}, 4500);
console.log(await res);
console.log(req.ended, req.aborted, req.paused);
}();
Tg Upload
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
bot.sendPhoto({
"chat_id": "0",
"photo": "https://avatars0.githubusercontent.com/u/5007624"
});
bot.sendPhoto({
"chat_id": "0",
"photo": "tg+https://avatars0.githubusercontent.com/u/5007624"
});
const tgb = require("tgb");
void async function () {
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const fileId = "AgADAgAD36gxGwWj2EuIQ9vvX_3kbh-cmg4ABDhqGLqV07c_phkBAAEC";
console.log(await tgb.download(bot.token, fileId));
}();
File as Buffer
const fs = require("fs");
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
bot.sendMediaGroup({
"chat_id": "59725308",
"media": [
{"type": "photo","media": "O://test.jpg"},
{"type": "photo","media": fs.createReadStream("O://test.jpg")},
{"type": "photo","media": ["test.jpg", fs.readFileSync("O://test.jpg")]},
{"type": "photo","media": "AgADAgAD36gxGwWj2EuIQ9vvX_3kbh-cmg4ABDhqGLqV07c_phkBAAEC"},
{"type": "photo","media": "tg+https://avatars0.githubusercontent.com/u/5007624"},
{"type": "photo","media": "https://avatars0.githubusercontent.com/u/5007624"},
]
});
Misc
/*
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 ♫•¨•.¸¸♪
License
MIT
@ Daeren
@ Telegram