Modern Telegram Bot Framework for Node.js
MIT License
Bot releases are hidden (Show)
Published by MKRhere over 2 years ago
Published by MKRhere over 2 years ago
ctx.tg
; use ctx.telegram
insteadtype MiddlewareObj
Published by MKRhere over 2 years ago
testEnv
as an option to Telegraf / ClientPublished by MKRhere over 2 years ago
types.js
so importing "telegraf/types"
does not cause an eslint import resolution errorPublished by MKRhere over 2 years ago
Markup.button.webApp
helperctx.webAppData
shorthand to retrieve web_app_data
more ergonomicallytelegraf/types
to be imported directly without relying on a separate dependency on typegram. The export interface is not stable. It may change at a later date. Feedback welcomePublished by MKRhere over 2 years ago
ctx.approveChatJoinRequest
and ctx.declineChatJoinRequest
now implicitly use ctx.chat.id
instead of expecting chatId
as first parameter.Published by MKRhere almost 3 years ago
Composer.spoiler
and Composer#spoiler
methods.Published by MKRhere almost 3 years ago
banChatSenderChat
and unbanChatSenderChat
now infer this.chat.id
instead of taking it as first parameter.Published by MKRhere almost 3 years ago
Published by MKRhere almost 3 years ago
kickChatMember
in favour of banChatMember
Published by wojpawlik over 3 years ago
express
(#1477)Types.Markup
class; fixes #1414hookPath
deterministic (#1386)MountMap
Published by wojpawlik over 3 years ago
src/telegram-types.ts
as Types
(#1380)Triggers
in some Composer
methods (#1404)Context
generic,NarrowedContext
,Composer::start
require at least one middleware (https://github.com/telegraf/telegraf/issues/1292#issuecomment-759454750).I have no more features planned, except #1267 for v5.0.
Published by wojpawlik over 3 years ago
session
(#1373)dropPendingUpdates
to Telegraf::launch
(#1366)apiMode
option (#1334)deunionize
,NarrowedContext
,Telegram
,TelegramError
Published by KnorpelSenf almost 4 years ago
Scenes
AbortController
attachmentAgent
option to provide an agent that is used for fetching files from the web before they are sent to Telegram (previously done through the same agent
as used to connect to Telegram itself)Apart from updating Telegraf to full Telegram Bot API 5.0 support, the most significant improvement is that we now have a code base that is written 100 % in TypeScript, which more or less required a rewrite of the complete library.
Previously, the library was written in JavaScript and had a few type annotations to the side, but they were often incorrect or missing.
In working on the 4.0 release, we created type coverage of the complete Telegram Bot API.
The benefit is that your editor can now autocomplete accross every single field in every single method or entity.
Those who ever tried it out can confirm that this improves the development experience by an order of magnitude.
Note that this update brings 9 months worth of improvements, so while we did our best to summarise all important changes, it's possible that we forgot some!
Composer.guard
static Composer.mount
, prefer static Composer.on
Composer.entity
and Composer.match
privateComposer.catchAll
and Composer.safePassThru
Composer.fork
on('forward_date')
, no longer on('forward')
ctx.botInfo
required, i.e. is is always availablectx.state
readonly
ctx.senderChat
, ctx.unpinAllChatMessages
, and ctx.copyMessage
ctx.deleteMessage
no longer works for edited messages, channel posts, or edited channel postsctx.updateSubTypes
Extra
is removed entirely, see #1076.
You can now just specify any options directly, without having to create an Extra
instance before.
Markup
is largely reworked.
Please check out the new documentation about Markup
, this is much easier than trying to understand the differences between the old and the new version.
The TypeScript autocompletion should help you a lot in case you have to change something about your code.
The main thing to watch out for is that all buttons (keyboard buttons and inline buttons) are now created via Markup.button.url
and its siblings, no longer via Markup.urlButton
.
The integration with Extra
is removed.
Remove the ability to specify a custom property on the context object under which the session lives, in other words, the session data can only be available under ctx.session
.
Note that this also applies to scene sessions, they now exclusively live under ctx.scene.session
.
When no session data is stored, ctx.session
is now undefined
instead of {}
.
Bots should now always be started using bot.launch
with the corresponding configuration for either long polling (default) or webhooks.
startPolling
and startWebhook
are now privateTimeoutError
is now thrown if update takes longer than handlerTimeout
to processhandlerTimeout
defaults to 90 secondsstop
returns void
and takes reason
instead of callback as as an argumentchannelMode
is removedretryAfter
option (used in an error case), always using 5 seconds nowtimeout
option, always using 50 seconds nowlimit
option, always fetching 100 updates nowstopCallback
optionNaturally, if your editor validates your code against the TypeScript types, you may find a few bugs here and there that you were not aware of.
Here are a few things you might come across and that we'd like to explain.
Property X is not available
for messages, callback queries, etcThis happens in various different forms, one example is this:
bot.on("message", (ctx) => {
const text = ctx.message.text;
// ^ error here!
// Property 'text' does not exist on type ...
});
We are listening for message
s here, but not all messages are text messages!
As a result, the text property might be absent, for example for photo messages.
Here is how you solve it:
bot.on("message", (ctx) => {
if ("text" in message) {
// now we know that we have a text message!
const text = ctx.message.text; // works!
console.log(text);
}
});
Remember that you can always just do this instead:
bot.on("text", (ctx) => {
// we already know that we have a text message!
const text = ctx.message.text; // works!
console.log(text);
});
ctx.session
or ctx.scene
or ctx.wizard
typeIf you are using sessions, scenes, wizards, or even yet another of the many modules that work with Telegraf, you have to define your own context object.
This context object must specify which types your middleware registers on the context.
We cannot infer this automatically.
This is best explained by an example bot.
Here is one that's relevant: https://github.com/telegraf/telegraf/blob/develop/docs/examples/example-bot.ts
Scenes have various examples here: https://github.com/telegraf/telegraf/tree/develop/docs/examples/scenes
Middleware in Telegraf is very flexible, so we have to do some really fancy stuff under the hood when we infer the types for you.
Whenever you type bot.on(['callback_query', 'text'], (ctx) => ctx.|
, and autocomplete shows up, we have to look at the first argument of bot.on
, iterate through the array, look at each string in it, and then do a type transformation on the complete Bot API surface to filter out which options are even available on the relevant context objects at this point.
All of this computation happens on the type level.
(Fun fact: in the first iteration of this feature it took up to 20 seconds for autocomplete to show up.)
The point is: types are hard.
It was our main priority to make the types correct, and then we focused on making the type inference fast.
We would love to make TypeScript print more readable type errors, and we are actively looking trying a few things to do this without too much black magic.
Until then, don't give up!
Here's what you can do:
Published by dotcypress over 6 years ago
πTypeScript typings added.
Published by dotcypress over 6 years ago
Markup.selective()
helperPublished by dotcypress over 6 years ago
Published by dotcypress over 6 years ago
Published by dotcypress over 6 years ago
Reply by Webhook
feature.