tgfancy
A Fancy, Higher-Level Wrapper for Telegram Bot API
Built on top of node-telegram-bot-api.
:construction: Work In Progress :construction:

installation:
$ npm install tgfancy --save
sample usage:
const Tgfancy = require("tgfancy");
const bot = new Tgfancy(token, {
tgfancy: {
option: "value",
},
});
bot.sendMessage(chatId, "text message");
introduction:
tgfancy is basically node-telegram-bot-api on steroids.
Therefore, you MUST know how to work with node-telegram-bot-api
before using this wrapper. tgfancy is a drop-in replacement!
tgfancy provides ALL the methods exposed by TelegramBot
from node-telegram-bot-api. This means that all the methods from
TelegramBot are available on Tgfancy. This also includes the
constructor.
fanciness:
Here comes the fanciness
tgfancy adds the following fanciness:
Have a look at the API Reference.
feature options:
Most of the features are enabled by default. Such a feature (enabled by
default) is similar to doing something like:
const bot = new Tgfancy(token, {
tgfancy: {
feature: true,
},
});
Such a feature can be disabled like so:
const bot = new Tgfancy(token, {
tgfancy: {
feature: false,
},
});
If a feature allows more options, you may pass an object, instead of true
,
like:
const bot = new Tgfancy(token, {
tgfancy: {
feature: {
key: "value",
},
},
});
See example at example/feature-toggled.js
.
Ordered sending:
Using an internal queue, we can ensure messages are sent, to a specific
chat, in order without having to implement the
wait-for-response-to-send-next-message logic.
Feature option: orderedSending
(see above)
For example,
bot.sendMessage(chatId, "first message");
bot.sendMessage(chatId, "second message");
With tgfancy, you are guaranteed that "first message"
will be sent
before "second message"
.
Fancied functions: [ "sendAudio", "sendDocument", "sendGame", "sendInvoice", "sendLocation", "sendMessage", "sendPhoto", "sendSticker", "sendVenue", "sendVideo", "sendVideoNote", "sendVoice", ]
An earlier discussion on this feature can be found here.
See example at example/queued-up.js
.
Text paging:
The Tgfancy#sendMessage(chatId, message)
automatically pages messages,
that is, if message
is longer than the maximum limit of 4096 characters,
the message
is split into multiple parts. These parts are sent serially,
one after the other.
The page number, for example [01/10]
, is prefixed to the text.
Feature option: textPaging
(see above)
For example,
bot.sendMessage(chatId, veryLongText)
.then(function(messages) {
console.log("message has been sent in multiple pages");
}).catch(function(error) {
console.error(error);
});
Note: We do not support sending messages that'd result into more
than 99 parts.
See example at example/paging-text.js
.
chat ID resolution:
Usernames are automatically resolved to the target's corresponding
unique identifier. By default, this resolution uses the
PWRTelegram API.
Feature option: chatIdResolution
(see above)
For example,
bot.sendMessage("@gochomugo", "Message sent using username");
However, it is possible to define a custom function used to perform
these resolutions. The function must have the signature,
resolve(token, chatId, callback)
. The callback
must
have the signature callback(error, target)
, where target
is
an object representing the target entity.
const bot = new Tgfancy(token, {
tgfancy: {
chatIdResolution: {
resolve(token, chatId, callback) {
return callback(null, user);
},
},
},
});
Fancied functions: [ "deleteChatPhoto", "deleteChatStickerSet", "deleteMessage", "exportChatInviteLink", "forwardMessage", "getChat", "getChatAdministrators", "getChatMember", "getChatMembersCount", "getUserProfilePhotos", "kickChatMember", "leaveChat", "pinChatMessage", "promoteChatMember", "restrictChatMember", "sendAudio", "sendChatAction", "sendContact", "sendDocument", "sendGame", "sendInvoice", "sendLocation", "sendMediaGroup", "sendMessage", "sendPhoto", "sendSticker", "sendVenue", "sendVideo", "sendVideoNote", "sendVoice", "setChatDescription", "setChatPhoto", "setChatStickerSet", "setChatTitle", "setGameScore", "unbanChatMember", "unpinChatMessage", ]
See example at example/resolve-chatid.js
.
Note: The Chat ID is resolved before the request
is queued. Consider this, if order of messages gets messed
up, when using this resolution.
If you want to manually resolve a chat ID, take a look at
Tgfancy#resolveChatId. For example,
bot.resolveChatId(chatId)
.then(function(result) {
console.log(result);
});
Rate-Limiting:
Any request that encounters a 429
error i.e. rate-limiting error
will be retried after some time (as advised by the Telegram API or
1 minute by default).
The request will be retried for a number of times, until it succeeds or
the maximum number of retries has been reached
Feature option: ratelimiting
(see above)
For example,
const bot = new Tgfancy(token, {
tgfancy: {
ratelimiting: {
maxRetries: 10,
timeout: 1000 * 60,
notify(methodName, ...args) {
},
maxBackoff: 1000 * 60 * 5,
},
},
});
Fancied functions: [ "addStickerToSet", "answerCallbackQuery", "answerInlineQuery", "answerPreCheckoutQuery", "answerShippingQuery", "createNewStickerSet", "deleteChatPhoto", "deleteChatStickerSet", "deleteMessage", "deleteStickerFromSet", "downloadFile", "editMessageCaption", "editMessageLiveLocation", "editMessageReplyMarkup", "editMessageText", "exportChatInviteLink", "forwardMessage", "getChat", "getChatAdministrators", "getChatMember", "getChatMembersCount", "getFile", "getFileLink", "getGameHighScores", "getStickerSet", "getUpdates", "getUserProfilePhotos", "kickChatMember", "leaveChat", "pinChatMessage", "promoteChatMember", "restrictChatMember", "sendAudio", "sendChatAction", "sendContact", "sendDocument", "sendGame", "sendInvoice", "sendLocation", "sendMediaGroup", "sendMessage", "sendPhoto", "sendSticker", "sendVenue", "sendVideo", "sendVideoNote", "sendVoice", "setChatDescription", "setChatPhoto", "setChatStickerSet", "setChatTitle", "setGameScore", "setStickerPositionInSet", "setWebHook", "stopMessageLiveLocation", "unbanChatMember", "unpinChatMessage", "uploadStickerFile", ]
An earlier discussion on this feature can be found here.
See example at example/ratelimited.js
.
Emojification:
Any Github-flavoured Markdown emoji, such as :heart:
can be replaced
automatically with their corresponding Unicode values. By default,
uses the node-emoji library (Go give a star!).
Disabled by default.
Feature option: emojification
(see above)
For example,
const bot = new Tgfancy(token, {
tgfancy: {
emojification: true,
},
});
bot.sendMessage(chatId, "Message text with :heart: emoji")
.then(function(msg) {
console.log(msg.text);
});
However, it is possible to define a custom function used to perform
emojification. The function must have the signature,
emojify(text)
and return the emojified text.
const bot = new Tgfancy(token, {
tgfancy: {
emojification: {
emojify(text) {
return emojifiedText;
},
},
},
});
Fancied functions: ["sendMessage", "editMessageText"]
See example at example/emojified.js
.
Fetching Updates via WebSocket:
In addition to polling and web-hooks, this introduces another mechanism
for fetching your updates: WebSocket. While currently it is not
officially supported by Telegram, we have a bridge up and running
that you can connect to for this purpose. Disabled by default.
Feature option: webSocket
(see above)
For example,
const bot = new Tgfancy(token, {
tgfancy: {
webSocket: true,
},
});
The current default bridge is at
wss://telegram-websocket-bridge-qalwkrjzzs.now.sh and is being run by
@GingerPlusPlus.
You can specify more options as so:
const bot = new Tgfancy(token, {
tgfancy: {
webSocket: {
url: "wss://telegram-websocket-bridge-qalwkrjzzs.now.sh",
autoOpen: true,
},
},
});
See example at example/web-socket.js
.
Kick-without-Ban:
You can kick a user without banning them, that is,
they will be able to rejoin the group using invite links, etc.
By default, Tgfancy kicks the user through Tgfancy#kickChatMember()
using the default API method kickChatMember
. Passing false
as the last argument to Tgfancy#kickChatMember()
will make
Tgfancy executes the API method unbanChatMember()
right after
kicking the chat member, effectively kicking the user, without
banning them.
Feature option: kickWithoutBan
(see above)
For example,
bot.kickChatMember(chatId, userId, false);
See example at example/kick-only.js
.
Openshift WebHook:
It is easier to set up webhook for your bot on Openshift.
Enabling this feature allows automatic detection if running on Openshift
and setting up web-hook for the bot instance.
Feature option: openshiftWebHook
(see above)
For example,
const bot = new Tgfancy(token, {
tgfancy: {
openshiftWebHook: true,
},
});
Note that polling and fetching updates via WebSocket are automatically disabled if the web-hook is set.
This allows you to use polling or WebSocket locally, but use a web-hook on Openshift.
For example,
const bot = new Tgfancy(token, {
polling: true,
tgfancy: {
openshiftWebHook: true,
},
});
You may also define default web-hook parameters to be used,
if the bot is not running on Openshift. For example,
const bot = new Tgfancy(token, {
webHook: { },
tgfancy: {
openshiftWebHook: true,
},
});
license:
The MIT License (MIT)
Copyright (c) 2016 GochoMugo (www.gmugo.in)