UNPKG

13.7 kBMarkdownView Raw
1<header>
2
3<div align="center">
4<img src="docs/assets/logo.svg" alt="logo" height="90" align="center">
5<h1 align="center">telegraf.js</h1>
6
7<p>Modern Telegram Bot API framework for Node.js</p>
8
9<a href="https://core.telegram.org/bots/api">
10 <img src="https://img.shields.io/badge/Bot%20API-v6.2-f36caf.svg?style=flat-square" alt="Bot API Version" />
11</a>
12<a href="https://packagephobia.com/result?p=telegraf,node-telegram-bot-api">
13 <img src="https://flat.badgen.net/packagephobia/install/telegraf" alt="install size" />
14</a>
15<a href="https://github.com/telegraf/telegraf">
16 <img src="https://img.shields.io/github/languages/top/telegraf/telegraf?style=flat-square&logo=github" alt="GitHub top language" />
17</a>
18<a href="https://telegram.me/TelegrafJSChat">
19 <img src="https://img.shields.io/badge/English%20chat-grey?style=flat-square&logo=telegram" alt="English chat" />
20</a>
21</div>
22
23</header>
24
25## For 3.x users
26
27- [3.x docs](https://telegraf.js.org/v3)
28- [4.0 release notes](https://github.com/telegraf/telegraf/releases/tag/v4.0.0)
29
30## Introduction
31
32Bots are special [Telegram](https://telegram.org) accounts designed to handle messages automatically.
33Users can interact with bots by sending them command messages in private or group chats.
34These accounts serve as an interface for code running somewhere on your server.
35
36Telegraf is a library that makes it simple for you to develop your own Telegram bots using JavaScript or [TypeScript](https://www.typescriptlang.org/).
37
38### Features
39
40- Full [Telegram Bot API 6.2](https://core.telegram.org/bots/api) support
41- [Excellent TypeScript typings](https://github.com/telegraf/telegraf/releases/tag/v4.0.0)
42- [Lightweight](https://packagephobia.com/result?p=telegraf,node-telegram-bot-api)
43- [AWS **λ**](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html)
44 / [Firebase](https://firebase.google.com/products/functions/)
45 / [Glitch](https://glitch.com/edit/#!/dashing-light)
46 / [Fly.io](https://fly.io/docs/languages-and-frameworks/node)
47 / Whatever ready
48- `http/https/fastify/Connect.js/express.js` compatible webhooks
49- Extensible
50
51### Example
52
53```js
54const { Telegraf } = require('telegraf');
55
56const bot = new Telegraf(process.env.BOT_TOKEN);
57bot.start((ctx) => ctx.reply('Welcome'));
58bot.help((ctx) => ctx.reply('Send me a sticker'));
59bot.on('sticker', (ctx) => ctx.reply('👍'));
60bot.hears('hi', (ctx) => ctx.reply('Hey there'));
61bot.launch();
62
63// Enable graceful stop
64process.once('SIGINT', () => bot.stop('SIGINT'));
65process.once('SIGTERM', () => bot.stop('SIGTERM'));
66```
67
68```js
69const { Telegraf } = require('telegraf');
70
71const bot = new Telegraf(process.env.BOT_TOKEN);
72bot.command('oldschool', (ctx) => ctx.reply('Hello'));
73bot.command('hipster', Telegraf.reply('λ'));
74bot.launch();
75
76// Enable graceful stop
77process.once('SIGINT', () => bot.stop('SIGINT'));
78process.once('SIGTERM', () => bot.stop('SIGTERM'));
79```
80
81For additional bot examples see the new [`docs repo`](https://github.com/feathers-studio/telegraf-docs/).
82
83### Resources
84
85- [Getting started](#getting-started)
86- [API reference](https://telegraf.js.org/modules.html)
87- Telegram groups (sorted by number of members):
88 * [English](https://t.me/TelegrafJSChat)
89 * [Uzbek](https://t.me/botjs_uz)
90 * [Ethiopian](https://t.me/telegraf_et)
91 * [Russian](https://t.me/telegrafjs_ru)
92- [GitHub Discussions](https://github.com/telegraf/telegraf/discussions)
93- [Dependent repositories](https://libraries.io/npm/telegraf/dependent_repositories)
94
95## Getting started
96
97### Telegram token
98
99To use the [Telegram Bot API](https://core.telegram.org/bots/api),
100you first have to [get a bot account](https://core.telegram.org/bots)
101by [chatting with BotFather](https://core.telegram.org/bots#6-botfather).
102
103BotFather will give you a *token*, something like `123456789:AbCdefGhIJKlmNoPQRsTUVwxyZ`.
104
105### Installation
106
107```shellscript
108$ npm install telegraf
109```
110or
111```shellscript
112$ yarn add telegraf
113```
114or
115```shellscript
116$ pnpm add telegraf
117```
118
119### `Telegraf` class
120
121[`Telegraf`] instance represents your bot. It's responsible for obtaining updates and passing them to your handlers.
122
123Start by [listening to commands](https://telegraf.js.org/classes/Telegraf-1.html#command) and [launching](https://telegraf.js.org/classes/Telegraf-1.html#launch) your bot.
124
125### `Context` class
126
127`ctx` you can see in every example is a [`Context`] instance.
128[`Telegraf`] creates one for each incoming update and passes it to your middleware.
129It contains the `update`, `botInfo`, and `telegram` for making arbitrary Bot API requests,
130as well as shorthand methods and getters.
131
132This is probably the class you'll be using the most.
133
134
135<!--
136TODO: Verify and update list
137Here is a list of
138
139#### Known middleware
140
141- [Internationalization](https://github.com/telegraf/telegraf-i18n)—simplifies selecting the right translation to use when responding to a user.
142- [Redis powered session](https://github.com/telegraf/telegraf-session-redis)—store session data using Redis.
143- [Local powered session (via lowdb)](https://github.com/RealSpeaker/telegraf-session-local)—store session data in a local file.
144- [Rate-limiting](https://github.com/telegraf/telegraf-ratelimit)—apply rate limitting to chats or users.
145- [Bottleneck powered throttling](https://github.com/KnightNiwrem/telegraf-throttler)—apply throttling to both incoming updates and outgoing API calls.
146- [Menus via inline keyboards](https://github.com/EdJoPaTo/telegraf-inline-menu)—simplify creating interfaces based on menus.
147- [Stateless Questions](https://github.com/EdJoPaTo/telegraf-stateless-question)—create stateless questions to Telegram users working in privacy mode.
148- [Natural language processing via wit.ai](https://github.com/telegraf/telegraf-wit)
149- [Natural language processing via recast.ai](https://github.com/telegraf/telegraf-recast)
150- [Multivariate and A/B testing](https://github.com/telegraf/telegraf-experiments)—add experiments to see how different versions of a feature are used.
151- [Powerfull bot stats via Mixpanel](https://github.com/telegraf/telegraf-mixpanel)
152- [statsd integration](https://github.com/telegraf/telegraf-statsd)
153- [and more...](https://www.npmjs.com/search?q=telegraf-)
154-->
155
156#### Shorthand methods
157
158```js
159import { Telegraf } from 'telegraf';
160
161const bot = new Telegraf(process.env.BOT_TOKEN);
162
163bot.command('quit', async (ctx) => {
164 // Explicit usage
165 await ctx.telegram.leaveChat(ctx.message.chat.id);
166
167 // Using context shortcut
168 await ctx.leaveChat();
169});
170
171bot.on('text', async (ctx) => {
172 // Explicit usage
173 await ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`);
174
175 // Using context shortcut
176 await ctx.reply(`Hello ${ctx.state.role}`);
177});
178
179bot.on('callback_query', async (ctx) => {
180 // Explicit usage
181 await ctx.telegram.answerCbQuery(ctx.callbackQuery.id);
182
183 // Using context shortcut
184 await ctx.answerCbQuery();
185});
186
187bot.on('inline_query', async (ctx) => {
188 const result = [];
189 // Explicit usage
190 await ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, result);
191
192 // Using context shortcut
193 await ctx.answerInlineQuery(result);
194});
195
196bot.launch();
197
198// Enable graceful stop
199process.once('SIGINT', () => bot.stop('SIGINT'));
200process.once('SIGTERM', () => bot.stop('SIGTERM'));
201```
202
203## Production
204
205### Webhooks
206
207```TS
208import { Telegraf } from "telegraf";
209
210const bot = new Telegraf(token);
211
212bot.on("text", ctx => ctx.reply("Hello"));
213
214// Start webhook via launch method (preferred)
215bot.launch({
216 webhook: {
217 // Public domain for webhook; e.g.: example.com
218 domain: webhookDomain,
219
220 // Port to listen on; e.g.: 8080
221 port: port,
222
223 // Optional path to listen for.
224 // `bot.secretPathComponent()` will be used by default
225 hookPath: webhookPath,
226
227 // Optional secret to be sent back in a header for security.
228 // e.g.: `crypto.randomBytes(64).toString("hex")`
229 secretToken: randomAlphaNumericString,
230 },
231});
232```
233
234Use `createWebhook()` if you want to attach Telegraf to an existing http server.
235
236<!-- global bot, tlsOptions -->
237
238```TS
239const { createServer } from "http";
240
241createServer(await bot.createWebhook({ domain: "example.com" })).listen(3000);
242```
243
244```TS
245const { createServer } from "https";
246
247createServer(tlsOptions, await bot.createWebhook({ domain: "example.com" })).listen(8443);
248```
249
250- [AWS Lambda example integration](https://github.com/feathers-studio/telegraf-docs/tree/master/examples/functions/aws-lambda)
251- [Google Cloud Functions example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/functions/google-cloud-function.ts)
252- [`express` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/express.ts)
253- [`fastify` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/fastify.ts)
254- [`koa` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/koa.ts)
255- [NestJS framework integration module](https://github.com/bukhalo/nestjs-telegraf)
256- [Cloudflare Workers integration module](https://github.com/Tsuk1ko/cfworker-middware-telegraf)
257- Use [`bot.handleUpdate`](https://telegraf.js.org/classes/Telegraf-1.html#handleupdate) to write new integrations
258
259### Error handling
260
261If middleware throws an error or times out, Telegraf calls `bot.handleError`. If it rethrows, update source closes, and then the error is printed to console and process [hopefully](https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode) terminates. If it does not rethrow, the error is swallowed.
262
263Default `bot.handleError` always rethrows. You can overwrite it using `bot.catch` if you need to.
264
265⚠️ Always rethrow `TimeoutError`!
266
267⚠️ Swallowing unknown errors might leave the process in invalid state!
268
269ℹ️ In production, `systemd` or [`pm2`](https://www.npmjs.com/package/pm2) can restart your bot if it exits for any reason.
270
271## Advanced topics
272
273### Working with files
274
275Supported file sources:
276
277- `Existing file_id`
278- `File path`
279- `Url`
280- `Buffer`
281- `ReadStream`
282
283Also, you can provide an optional name of a file as `filename` when you send the file.
284
285<!-- global bot, fs -->
286
287```js
288bot.on('message', async (ctx) => {
289 // resend existing file by file_id
290 await ctx.replyWithSticker('123123jkbhj6b');
291
292 // send file
293 await ctx.replyWithVideo({ source: '/path/to/video.mp4' });
294
295 // send stream
296 await ctx.replyWithVideo({
297 source: fs.createReadStream('/path/to/video.mp4')
298 });
299
300 // send buffer
301 await ctx.replyWithVoice({
302 source: Buffer.alloc()
303 });
304
305 // send url via Telegram server
306 await ctx.replyWithPhoto('https://picsum.photos/200/300/');
307
308 // pipe url content
309 await ctx.replyWithPhoto({
310 url: 'https://picsum.photos/200/300/?random',
311 filename: 'kitten.jpg'
312 });
313})
314```
315
316### Middleware
317
318In addition to `ctx: Context`, each middleware receives `next: () => Promise<void>`.
319
320As in Koa and some other middleware-based libraries,
321`await next()` will call next middleware and wait for it to finish:
322
323```TS
324import { Telegraf } from 'telegraf';
325
326const bot = new Telegraf(process.env.BOT_TOKEN);
327
328bot.use(async (ctx, next) => {
329 console.time(`Processing update ${ctx.update.update_id}`);
330 await next() // runs next middleware
331 // runs after next middleware finishes
332 console.timeEnd(`Processing update ${ctx.update.update_id}`);
333})
334
335bot.on('text', (ctx) => ctx.reply('Hello World'));
336bot.launch();
337
338// Enable graceful stop
339process.once('SIGINT', () => bot.stop('SIGINT'));
340process.once('SIGTERM', () => bot.stop('SIGTERM'));
341```
342
343With this simple ability, you can:
344- extract information from updates and then `await next()` to avoid disrupting other middleware,
345- like [`Composer`] and [`Router`], `await next()` for updates you don't wish to handle,
346- like [`session`] and [`Scenes`], [extend the context](#extending-context) by mutating `ctx` before `await next()`,
347- [intercept API calls](https://github.com/telegraf/telegraf/discussions/1267#discussioncomment-254525),
348- reuse [other people's code](https://www.npmjs.com/search?q=telegraf-),
349- do whatever **you** come up with!
350
351[`Telegraf`]: https://telegraf.js.org/classes/Telegraf-1.html
352[`Composer`]: https://telegraf.js.org/classes/Composer.html
353[`Context`]: https://telegraf.js.org/classes/Context.html
354[`Router`]: https://telegraf.js.org/classes/Router.html
355[`session`]: https://telegraf.js.org/modules.html#session
356[`Scenes`]: https://telegraf.js.org/modules/Scenes.html
357
358### Usage with TypeScript
359
360Telegraf is written in TypeScript and therefore ships with declaration files for the entire library.
361Moreover, it includes types for the complete Telegram API via the [`typegram`](https://github.com/KnorpelSenf/typegram) package.
362While most types of Telegraf's API surface are self-explanatory, there's some notable things to keep in mind.
363
364#### Extending `Context`
365
366The exact shape of `ctx` can vary based on the installed middleware.
367Some custom middleware might register properties on the context object that Telegraf is not aware of.
368Consequently, you can change the type of `ctx` to fit your needs in order for you to have proper TypeScript types for your data.
369This is done through Generics:
370
371```ts
372import { Context, Telegraf } from 'telegraf';
373
374// Define your own context type
375interface MyContext extends Context {
376 myProp?: string
377 myOtherProp?: number
378}
379
380// Create your bot and tell it about your context type
381const bot = new Telegraf<MyContext>('SECRET TOKEN');
382
383// Register middleware and launch your bot as usual
384bot.use((ctx, next) => {
385 // Yay, `myProp` is now available here as `string | undefined`!
386 ctx.myProp = ctx.chat?.first_name?.toUpperCase();
387 return next();
388});
389// ...
390```