npm -g install tgb
git clone https:
await require("tgb")("T", "sendmessage", [0, "+"], proxy)
await require("tgb").sendMessage("T", {chat_id: 0, text: "+"}, proxy)
> tgb-cli --polling TOKEN --echo -j
const tgb = require("tgb");
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const {polling, entities} = tgb;
polling(
bot.token,
function({message}) {
bot.sendMessage([message.from.id, entities(message)]);
})
.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);
}
});
WebHook
const tgb = require("tgb");
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const {webhook} = tgb;
void async function NGINX() {
const wh = await webhook({host: "localhost", port: 1490});
const url = await wh.bind(bot, "db.gg:88/custom", ({message}) => {});
}();
void async function HTTPS() {
const wh = (await webhook(8443, {
"ssl": {
"key": "./db.gg.key",
"cert": "./db.gg.crt"
}
})).catch(function(error) {});
const url = await wh.bind(bot, "db.gg", function({message}, bot) {
this.sendMessage([message.from.id, `Hi: ${message.text}`]);
});
}();
Spy
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const {webhook, spy, entities} = tgb;
void async function Webhook() {
const watch = spy();
const wh = await webhook({host: "localhost", port: 1490});
const url = await wh.bind(bot, "db.gg:88/custom", watch.update);
watch("message.bot_command/start", function(val, bot) {});
watch("message.hashtag#win", function(val, bot) {});
watch("message.text", function(val, bot, message) {
if(val === "die") {
this.destroy();
}
});
watch("message.from.id", function() {});
watch("message", function(val) {
const es = entities(val);
for(let type of Object.keys(es || {})) {
es[type].forEach((e) => val[`${type}${e}`] = e);
}
if(es) {
delete val.text;
}
});
}();
void async function Polling() {
const watch = spy({desc: true});
polling(bot.token, (data) => watch.update(data, bot));
watch("message.text", function(val, bot) {});
watch("message.from.id", function(val, bot) {});
}();
Download
const tgb = require("tgb");
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const {download} = tgb;
const fileId = "AgADAgAD36gxGwWj2EuIQ9vvX_3kbh-cmg4ABDhqGLqV07c_phkBAAEC";
void async function() {
await download(bot.token, fileId);
}();
Proxy
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
void async function() {
bot.proxy = "127.0.0.1:1337";
try {
await bot.getMe();
await bot.getMe(null, "0.0.0.0:1337");
} catch(e) {
console.log(e);
}
}();
Abort/Pause/Resume
void async function() {
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const params = {"chat_id": "0", "audio": "O://1.mp3"};
const [res, req] = bot.sendAudio(params);
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"
});
Markup
const bot = tgb(process.env.TELEGRAM_BOT_TOKEN);
const {markup} = tgb;
bot.sendMessage(markup({
"chat_id": "0",
"text": "Hi"
}).keyboard(`A|B|C\nX|Y\nO`));
bot.sendMessage(markup({
"chat_id": "0",
"text": "Hi"
}).keyboard());
bot.sendMessage(markup({
"chat_id": "0",
"text": "Hi"
}).reply());
Extensibility
const tgb = require("tgb");
const {Client} = tgb;
class MyBot extends Client {
constructor(token, msg) {
super(token);
this.msg = msg;
}
send(id) {
return this.sendMessage(id, this.msg);
}
sendMessage(cid, text) {
return super.sendMessage([cid, `watch?${text}`]);
}
}
const bot = new MyBot(process.env.TELEGRAM_BOT_TOKEN, "v=vc-PJPrueXY");
const [res, req] = bot.send("0");
console.log(await res);
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"},
]
});
CLI
> SET TELEGRAM_BOT_TOKEN=1:XXXX
> tgb-cli --method sendPhoto --d.chat_id 0 --d.photo "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 --d.chat_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"
Misc
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
@ Daeren
@ Telegram