Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
teleapiwrapper
Advanced tools
An unofficial JS-wrapper for the Telegram bot API
Supports ALL methods in the bot API as of 2016-12-04
npm: https://www.npmjs.com/package/teleapiwrapper
github: https://github.com/Suppen/Telegram-API-wrapper-for-JS
Official API-documentation: https://core.telegram.org/bots/api
Documentation for this package: https://doc.suppen.no/teleapiwrapper
bot.sendMessage(123456789, "Cake")
will no longer work. The way to do it now is bot.sendMessage({chat_id: 123456789, text: "Cake"})
bot.sendMessage({chat_id: 123456789, text: "Cake"}, cb)
const BotAPI = require("teleapiwrapper").BotAPI;
let bot = new BotAPI(botToken);
All methods in the API have the signature methodName(args, cb)
. The arguments to the method are given as an object, and an optional callback function can be called when the API call is completed.
All methods return promises, which can be used instead of callbacks.
// Get bot info using callback
bot.getMe({}, (err, res) => {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
// Get bot info using promise
bot.getMe().then(res => {
console.log(res);
}).catch(err => {
console.log(err);
});
// Send a message
// Arguments are provided as an object to the method
bot.sendMessage({
chat_id: chatId,
text: text
});
// Send a message with a reply-keyboard
let keyboard = {/* reply_markup */
keyboard: [
['yes', 'no'],
[ 'cancel'],
],
resize_keyboard: true
};
bot.sendMessage({
chat_id: chatId,
text: text,
reply_markup: keyboard
});
There are a few ways you can send files with your bot. I will demonstrate these ways with the sendPhoto
method, but all methods which can send files do it this way.
This one sends any readable stream as a file. Easiest method to send a file from your file system
bot.sendPhoto({
chat_id: chatId,
photo: fs.createReadStream("some_photo.jpg"),
caption: "This is a really nice photo"
});
If the readable stream is an instance of fs.ReadStream
, like in the example above, the file name will be extracted from the stream, so the receivers will see it named "some_photo.jpg". Other streams don't have the file name in them, and will therefore be named "Some file" if you do not explicitly give it a name with the InputFile
class described further down
Much the same as sending with a stream, you can send a buffer containing a file your file system
let fileBuffer = fs.readFileSync("some_photo.jpg");
bot.sendPhoto({
chat_id: chatId,
photo: fileBuffer
caption: "This is a really nice photo"
});
Note, however, that the receiver(s) will see the file name as "Some file". See the section about the InputFile
class to change this.
When you send a file to Telegram, you will get the sent message in response. This message contains the ID Telegram assigned to the file. Using this ID, you can resend the file without having to upload it again
let photoId = "Adrgvmercfiawejdatruotseafasert";
bot.sendPhoto({
chat_id: chatId,
photo: photoId,
caption: "This is a really nice photo"
});
InputFile
classThe InputFile
class is what the above methods are converted to under the hood, but you can also use it explicitly to give a file a specific name.
To use it, first require it:
const InputFile = require("teleapiwrapper").InputFile;
The InputFile
constructor takes two arguments: data
and filename
. The data
argument can be a readable stream, a Buffer
or a String
, with the same results as the examples above. The filename
option lets you set a name the receiver(s) of the file will see.
let file = new InputFile(fs.createReadStream("some_photo.jpg"), "Very nice photo.jpg");
bot.sendPhoto({
chat_id: chatId,
photo: file,
caption: "This is a really nice photo"
});
Users can send any file to the bot. Telegram allows bots to download files up to 20 MB. If someone has sent a file to your but, you will find a file_id
in the corresponding update
object. Use the getFile
method to get data about the file.
Unfortunately, the getFile
method does not actually get the file, just data about it, including an URL where you can actually download it. teleapiwrapper therefore has a helper method, named helperGetFileStream
, which gives you a download stream for that file
let file_id = update.message.document.file_id;
bot.getFile({file_id})
.then(file => {
// The `file` object does not contain the actual file, just data about it, like a download path, the size and a file name. Pass it on to the `helperGetFileStream` method
return bot.helperGetFileStream(file);
}).then(stream => {
// Now you have a stream containing the actual file. Do whatever you want with it, like saving it to disk:
stream.pipe(fs.createWriteStream("some_file"));
}).catch(err => {
// Something went very wrong when fetching the file
console.log(err);
});
Everything in the wrapper is documented with JSDoc. The documentation is available in node_modules/teleapiwrapper/docs/index.html
. Use it well.
They are also readable on https://doc.suppen.no/teleapiwrapper
requestTimeout
propertyhelperDownloadFile
work againhelperDownloadFile
method also return a promiseFAQs
An unofficial JS-wrapper for the Telegram bot API.
The npm package teleapiwrapper receives a total of 16 weekly downloads. As such, teleapiwrapper popularity was classified as not popular.
We found that teleapiwrapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.