@sugansociety/gallagherrich 4.16.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +1024 -0
- package/filters.d.ts +1 -0
- package/filters.js +1 -0
- package/format.d.ts +1 -0
- package/format.js +1 -0
- package/future.d.ts +1 -0
- package/future.js +1 -0
- package/lib/button.js +102 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +583 -0
- package/lib/context.js +1280 -0
- package/lib/core/helpers/args.js +59 -0
- package/lib/core/helpers/check.js +57 -0
- package/lib/core/helpers/compact.js +18 -0
- package/lib/core/helpers/deunionize.js +14 -0
- package/lib/core/helpers/formatting.js +92 -0
- package/lib/core/helpers/util.js +51 -0
- package/lib/core/network/client.js +321 -0
- package/lib/core/network/error.js +22 -0
- package/lib/core/network/multipart-stream.js +62 -0
- package/lib/core/network/polling.js +88 -0
- package/lib/core/network/webhook.js +55 -0
- package/lib/core/types/rich-message.js +288 -0
- package/lib/core/types/typegram.js +29 -0
- package/lib/filters.js +70 -0
- package/lib/filters.js.map +1 -0
- package/lib/format.js +39 -0
- package/lib/format.js.map +1 -0
- package/lib/future.js +167 -0
- package/lib/future.js.map +1 -0
- package/lib/index.js +50 -0
- package/lib/input.js +62 -0
- package/lib/markup.js +112 -0
- package/lib/markup.js.map +1 -0
- package/lib/middleware.js +3 -0
- package/lib/reactions.js +85 -0
- package/lib/router.js +47 -0
- package/lib/scenes/base.js +40 -0
- package/lib/scenes/context.js +105 -0
- package/lib/scenes/index.js +22 -0
- package/lib/scenes/stage.js +50 -0
- package/lib/scenes/wizard/context.js +32 -0
- package/lib/scenes/wizard/index.js +46 -0
- package/lib/scenes.js +18 -0
- package/lib/scenes.js.map +1 -0
- package/lib/session.js +167 -0
- package/lib/session.js.map +1 -0
- package/lib/telegraf.js +273 -0
- package/lib/telegram-types.js +7 -0
- package/lib/telegram.js +1270 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/utils.js +6 -0
- package/lib/utils.js.map +1 -0
- package/markup.d.ts +1 -0
- package/markup.js +1 -0
- package/package.json +136 -0
- package/scenes.d.ts +1 -0
- package/scenes.js +1 -0
- package/session.d.ts +1 -0
- package/session.js +1 -0
- package/src/button.ts +182 -0
- package/src/composer.ts +1008 -0
- package/src/context.ts +1747 -0
- package/src/core/helpers/args.ts +63 -0
- package/src/core/helpers/check.ts +71 -0
- package/src/core/helpers/compact.ts +18 -0
- package/src/core/helpers/deunionize.ts +26 -0
- package/src/core/helpers/formatting.ts +119 -0
- package/src/core/helpers/util.ts +96 -0
- package/src/core/network/client.ts +396 -0
- package/src/core/network/error.ts +29 -0
- package/src/core/network/multipart-stream.ts +45 -0
- package/src/core/network/polling.ts +94 -0
- package/src/core/network/webhook.ts +58 -0
- package/src/core/types/rich-message.ts +375 -0
- package/src/core/types/typegram.ts +55 -0
- package/src/filters.ts +109 -0
- package/src/format.ts +110 -0
- package/src/future.ts +231 -0
- package/src/index.ts +18 -0
- package/src/input.ts +59 -0
- package/src/markup.ts +142 -0
- package/src/middleware.ts +24 -0
- package/src/reactions.ts +118 -0
- package/src/router.ts +55 -0
- package/src/scenes/base.ts +52 -0
- package/src/scenes/context.ts +136 -0
- package/src/scenes/index.ts +21 -0
- package/src/scenes/stage.ts +71 -0
- package/src/scenes/wizard/context.ts +58 -0
- package/src/scenes/wizard/index.ts +63 -0
- package/src/scenes.ts +1 -0
- package/src/session.ts +204 -0
- package/src/telegraf.ts +380 -0
- package/src/telegram-types.ts +256 -0
- package/src/telegram.ts +1676 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/typings/button.d.ts +36 -0
- package/typings/button.d.ts.map +1 -0
- package/typings/composer.d.ts +227 -0
- package/typings/composer.d.ts.map +1 -0
- package/typings/context.d.ts +699 -0
- package/typings/context.d.ts.map +1 -0
- package/typings/core/helpers/args.d.ts +11 -0
- package/typings/core/helpers/args.d.ts.map +1 -0
- package/typings/core/helpers/check.d.ts +56 -0
- package/typings/core/helpers/check.d.ts.map +1 -0
- package/typings/core/helpers/compact.d.ts +4 -0
- package/typings/core/helpers/compact.d.ts.map +1 -0
- package/typings/core/helpers/deunionize.d.ts +18 -0
- package/typings/core/helpers/deunionize.d.ts.map +1 -0
- package/typings/core/helpers/formatting.d.ts +30 -0
- package/typings/core/helpers/formatting.d.ts.map +1 -0
- package/typings/core/helpers/util.d.ts +27 -0
- package/typings/core/helpers/util.d.ts.map +1 -0
- package/typings/core/network/client.d.ts +55 -0
- package/typings/core/network/client.d.ts.map +1 -0
- package/typings/core/network/error.d.ts +16 -0
- package/typings/core/network/error.d.ts.map +1 -0
- package/typings/core/network/multipart-stream.d.ts +20 -0
- package/typings/core/network/multipart-stream.d.ts.map +1 -0
- package/typings/core/network/polling.d.ts +16 -0
- package/typings/core/network/polling.d.ts.map +1 -0
- package/typings/core/network/webhook.d.ts +7 -0
- package/typings/core/network/webhook.d.ts.map +1 -0
- package/typings/core/types/rich-message.d.ts +144 -0
- package/typings/core/types/rich-message.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +46 -0
- package/typings/core/types/typegram.d.ts.map +1 -0
- package/typings/filters.d.ts +18 -0
- package/typings/filters.d.ts.map +1 -0
- package/typings/format.d.ts +22 -0
- package/typings/format.d.ts.map +1 -0
- package/typings/future.d.ts +12 -0
- package/typings/future.d.ts.map +1 -0
- package/typings/index.d.ts +16 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +59 -0
- package/typings/input.d.ts.map +1 -0
- package/typings/markup.d.ts +27 -0
- package/typings/markup.d.ts.map +1 -0
- package/typings/middleware.d.ts +8 -0
- package/typings/middleware.d.ts.map +1 -0
- package/typings/reactions.d.ts +32 -0
- package/typings/reactions.d.ts.map +1 -0
- package/typings/router.d.ts +21 -0
- package/typings/router.d.ts.map +1 -0
- package/typings/scenes/base.d.ts +22 -0
- package/typings/scenes/base.d.ts.map +1 -0
- package/typings/scenes/context.d.ts +36 -0
- package/typings/scenes/context.d.ts.map +1 -0
- package/typings/scenes/index.d.ts +11 -0
- package/typings/scenes/index.d.ts.map +1 -0
- package/typings/scenes/stage.d.ts +24 -0
- package/typings/scenes/stage.d.ts.map +1 -0
- package/typings/scenes/wizard/context.d.ts +29 -0
- package/typings/scenes/wizard/context.d.ts.map +1 -0
- package/typings/scenes/wizard/index.d.ts +16 -0
- package/typings/scenes/wizard/index.d.ts.map +1 -0
- package/typings/scenes.d.ts +2 -0
- package/typings/scenes.d.ts.map +1 -0
- package/typings/session.d.ts +55 -0
- package/typings/session.d.ts.map +1 -0
- package/typings/telegraf.d.ts +119 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +141 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +694 -0
- package/typings/telegram.d.ts.map +1 -0
- package/typings/types.d.ts +3 -0
- package/typings/types.d.ts.map +1 -0
- package/typings/utils.d.ts +2 -0
- package/typings/utils.d.ts.map +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,1024 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
> Developed & maintained by **[@suganzi](https://t.me/suganzi)**
|
|
4
|
+
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
<header>
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
<img src="docs/assets/logo.svg" alt="logo" height="90" align="center">
|
|
11
|
+
<h1 align="center">telegraf.js</h1>
|
|
12
|
+
|
|
13
|
+
<p>Modern Telegram Bot API framework for Node.js</p>
|
|
14
|
+
|
|
15
|
+
<a href="https://core.telegram.org/bots/api">
|
|
16
|
+
<img src="https://img.shields.io/badge/Bot%20API-v10.1-f36caf.svg?style=flat-square" alt="Bot API Version" />
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://packagephobia.com/result?p=telegraf,node-telegram-bot-api">
|
|
19
|
+
<img src="https://flat.badgen.net/packagephobia/install/telegraf" alt="install size" />
|
|
20
|
+
</a>
|
|
21
|
+
<a href="https://github.com/telegraf/telegraf">
|
|
22
|
+
<img src="https://img.shields.io/github/languages/top/telegraf/telegraf?style=flat-square&logo=github" alt="GitHub top language" />
|
|
23
|
+
</a>
|
|
24
|
+
<a href="https://telegram.me/TelegrafJSChat">
|
|
25
|
+
<img src="https://img.shields.io/badge/English%20chat-grey?style=flat-square&logo=telegram" alt="English chat" />
|
|
26
|
+
</a>
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
</header>
|
|
30
|
+
|
|
31
|
+
> **gallagherrich** (`@sugansociety/gallagherrich`) is an up-to-date fork of [telegraf](https://github.com/telegraf/telegraf) — the popular Telegram Bot framework for Node.js.
|
|
32
|
+
> This package tracks the latest Telegram Bot API releases and ships features ahead of the upstream package.
|
|
33
|
+
> It is a drop-in replacement: just swap `npm install telegraf` → `npm install @sugansociety/gallagherrich` and change your imports from `'telegraf'` to `'@sugansociety/gallagherrich'`.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 🆕 Rich Messages — Bot API 10.1
|
|
39
|
+
|
|
40
|
+
> Added in **Bot API 10.1** (June 11, 2026). Rich Messages let bots send highly structured content using HTML or Markdown — headings, lists, tables, media collages, code blocks, math expressions, AI thinking blocks, and more — with streaming support for AI-generated replies.
|
|
41
|
+
|
|
42
|
+
### Table of Contents
|
|
43
|
+
|
|
44
|
+
- [Quick Start](#quick-start)
|
|
45
|
+
- [Sending Methods](#sending-methods)
|
|
46
|
+
- [InputRichMessage Format](#inputrichmessage-format)
|
|
47
|
+
- [RichHTMLBuilder](#richhtmlbuilder)
|
|
48
|
+
- [Text Formatting](#text-formatting-html)
|
|
49
|
+
- [Headings & Paragraphs](#headings--paragraphs)
|
|
50
|
+
- [Lists](#lists-html)
|
|
51
|
+
- [Code Blocks](#code-blocks)
|
|
52
|
+
- [Blockquote & Pull Quote](#blockquote--pull-quote)
|
|
53
|
+
- [Math](#math-html)
|
|
54
|
+
- [Media — Photo, Video, Audio](#media--photo-video-audio)
|
|
55
|
+
- [Collage & Slideshow](#collage--slideshow)
|
|
56
|
+
- [Map](#map)
|
|
57
|
+
- [Table](#table-html)
|
|
58
|
+
- [Details (Expandable)](#details-expandable)
|
|
59
|
+
- [Footnote References](#footnote-references-html)
|
|
60
|
+
- [Anchors & In-document Links](#anchors--in-document-links)
|
|
61
|
+
- [Special Elements](#special-elements)
|
|
62
|
+
- [RichMarkdownBuilder](#richmarkdownbuilder)
|
|
63
|
+
- [Streaming with sendRichMessageDraft](#streaming-with-sendrichmessagedraft)
|
|
64
|
+
- [reply\_markup with Rich Messages](#reply_markup-with-rich-messages)
|
|
65
|
+
- [Inline / Web App Queries](#inline--web-app-queries)
|
|
66
|
+
- [TypeScript](#typescript)
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
### Quick Start
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { Telegraf, RichMessage } from '@sugansociety/gallagherrich'
|
|
74
|
+
const { RichHTMLBuilder: HTML } = RichMessage
|
|
75
|
+
|
|
76
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
77
|
+
|
|
78
|
+
bot.command('hello', async (ctx) => {
|
|
79
|
+
const msg = new HTML()
|
|
80
|
+
.heading(1, 'Hello World!')
|
|
81
|
+
.paragraph(HTML.bold('Rich Messages') + ' are now supported in Bot API 10.1.')
|
|
82
|
+
.divider()
|
|
83
|
+
.ul('Headings', 'Lists', 'Tables', 'Media', 'Math', 'and more')
|
|
84
|
+
.build()
|
|
85
|
+
|
|
86
|
+
await ctx.sendRichMessage(msg)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
bot.launch()
|
|
90
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
91
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### Sending Methods
|
|
97
|
+
|
|
98
|
+
| Method | Description |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `ctx.sendRichMessage(msg, extra?)` | Send a rich message to the current chat |
|
|
101
|
+
| `ctx.replyWithRichMessageContent(msg, extra?)` | Send a rich message quoting the current message |
|
|
102
|
+
| `ctx.sendRichMessageDraft(draftId, msg, extra?)` | Stream a partial draft (private chats only) |
|
|
103
|
+
| `ctx.telegram.sendRichMessage(chatId, msg, extra?)` | Explicit call with chat ID |
|
|
104
|
+
| `ctx.telegram.sendRichMessageDraft(chatId, draftId, msg, extra?)` | Explicit streaming with chat ID |
|
|
105
|
+
|
|
106
|
+
**`extra` options for `sendRichMessage`:**
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
await ctx.sendRichMessage(msg, {
|
|
110
|
+
message_thread_id: 123, // for forum topics
|
|
111
|
+
direct_messages_topic_id: 456, // for DM topics
|
|
112
|
+
disable_notification: true,
|
|
113
|
+
protect_content: true,
|
|
114
|
+
allow_paid_broadcast: false,
|
|
115
|
+
message_effect_id: 'effect_id',
|
|
116
|
+
reply_parameters: { message_id: ctx.message.message_id },
|
|
117
|
+
reply_markup: { inline_keyboard: [[{ text: 'OK', callback_data: 'ok' }]] },
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
### InputRichMessage Format
|
|
124
|
+
|
|
125
|
+
`InputRichMessage` uses **either** `html` or `markdown` — not both:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
// HTML format
|
|
129
|
+
const msg: InputRichMessage = {
|
|
130
|
+
html: '<h1>Title</h1><p>Body text</p>',
|
|
131
|
+
is_rtl: false,
|
|
132
|
+
skip_entity_detection: false,
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Markdown format
|
|
136
|
+
const msg: InputRichMessage = {
|
|
137
|
+
markdown: '# Title\n\nBody text',
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Use the builders — `RichHTMLBuilder` or `RichMarkdownBuilder` — to construct these conveniently.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### RichHTMLBuilder
|
|
146
|
+
|
|
147
|
+
Import and instantiate:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { RichMessage } from '@sugansociety/gallagherrich'
|
|
151
|
+
const { RichHTMLBuilder: HTML } = RichMessage
|
|
152
|
+
|
|
153
|
+
const msg = new HTML()
|
|
154
|
+
.heading(1, 'Title')
|
|
155
|
+
.paragraph('Content')
|
|
156
|
+
.build() // returns InputRichMessage { html: '...' }
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Text Formatting (HTML)
|
|
160
|
+
|
|
161
|
+
Static inline helpers — return strings to embed inside block methods:
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
HTML.bold('bold text') // <b>bold text</b>
|
|
165
|
+
HTML.italic('italic text') // <i>italic text</i>
|
|
166
|
+
HTML.underline('underlined') // <u>underlined</u>
|
|
167
|
+
HTML.strikethrough('crossed out') // <s>crossed out</s>
|
|
168
|
+
HTML.spoiler('hidden until tapped') // <tg-spoiler>hidden</tg-spoiler>
|
|
169
|
+
HTML.code('inline code') // <code>inline code</code>
|
|
170
|
+
HTML.marked('highlighted') // <mark>highlighted</mark>
|
|
171
|
+
HTML.sub('subscript') // <sub>subscript</sub>
|
|
172
|
+
HTML.sup('superscript') // <sup>superscript</sup>
|
|
173
|
+
|
|
174
|
+
HTML.url('https://t.me', 'Telegram') // <a href="...">Telegram</a>
|
|
175
|
+
HTML.email('hi@bot.com', 'Email us') // <a href="mailto:...">Email us</a>
|
|
176
|
+
HTML.phone('+6281234567', 'Call us') // <a href="tel:...">Call us</a>
|
|
177
|
+
HTML.mention(123456789, 'Alice') // <a href="tg://user?id=...">Alice</a>
|
|
178
|
+
HTML.customEmoji('5368324170671202286', '👍') // <tg-emoji emoji-id="...">👍</tg-emoji>
|
|
179
|
+
HTML.time(1647531900, 'wDT', '22:45 tomorrow') // <tg-time unix="..." format="...">...</tg-time>
|
|
180
|
+
HTML.inlineMath('E = mc^2') // $E = mc^2$
|
|
181
|
+
|
|
182
|
+
// Nesting
|
|
183
|
+
HTML.bold(HTML.italic('bold italic'))
|
|
184
|
+
HTML.underline(HTML.spoiler('underlined spoiler'))
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Headings & Paragraphs
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
new HTML()
|
|
191
|
+
.heading(1, 'Main Title')
|
|
192
|
+
.heading(2, 'Subtitle')
|
|
193
|
+
.heading(3, HTML.bold('Bold heading'))
|
|
194
|
+
.heading(4, 'H4')
|
|
195
|
+
.heading(5, 'H5')
|
|
196
|
+
.heading(6, 'H6')
|
|
197
|
+
.paragraph('Normal paragraph text.')
|
|
198
|
+
.paragraph(HTML.bold('Bold') + ' and ' + HTML.italic('italic') + ' combined.')
|
|
199
|
+
.footer('Footer text — smaller and muted')
|
|
200
|
+
.divider() // <hr/>
|
|
201
|
+
.build()
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### Lists (HTML)
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
new HTML()
|
|
208
|
+
// Unordered list
|
|
209
|
+
.ul('First item', 'Second item', HTML.bold('Bold item'))
|
|
210
|
+
|
|
211
|
+
// Ordered list
|
|
212
|
+
.ol('Step one', 'Step two', 'Step three')
|
|
213
|
+
|
|
214
|
+
// Task list (checkboxes)
|
|
215
|
+
.taskList(
|
|
216
|
+
{ text: 'Completed task', checked: true },
|
|
217
|
+
{ text: 'Pending task', checked: false },
|
|
218
|
+
{ text: HTML.bold('Important task'), checked: false },
|
|
219
|
+
)
|
|
220
|
+
.build()
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### Code Blocks
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
new HTML()
|
|
227
|
+
.pre('npm install @sugansociety/gallagherrich', 'bash')
|
|
228
|
+
.pre('SELECT * FROM users WHERE active = 1;', 'sql')
|
|
229
|
+
.pre(
|
|
230
|
+
`const bot = new Telegraf(token)
|
|
231
|
+
bot.launch()`,
|
|
232
|
+
'javascript'
|
|
233
|
+
)
|
|
234
|
+
.pre('plain preformatted block without language')
|
|
235
|
+
.build()
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Supported language identifiers: `javascript`, `typescript`, `python`, `bash`, `sql`, `json`, `html`, `css`, etc.
|
|
239
|
+
|
|
240
|
+
#### Blockquote & Pull Quote
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
new HTML()
|
|
244
|
+
// Standard block quotation
|
|
245
|
+
.blockQuote(HTML.italic('"To be or not to be."'))
|
|
246
|
+
|
|
247
|
+
// Pull quotation with attribution (cite)
|
|
248
|
+
.pullQuote(
|
|
249
|
+
HTML.italic('"Design is not just what it looks like."'),
|
|
250
|
+
'Steve Jobs'
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
// Nested formatting inside quote
|
|
254
|
+
.blockQuote(
|
|
255
|
+
HTML.bold('Telekaf') + ' supports ' + HTML.marked('highlighted') +
|
|
256
|
+
' and ' + HTML.spoiler('spoiler') + ' text inside quotes.'
|
|
257
|
+
)
|
|
258
|
+
.build()
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
#### Math (HTML)
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
new HTML()
|
|
265
|
+
// Inline math (embed inside paragraph)
|
|
266
|
+
.paragraph(
|
|
267
|
+
'The formula is: ' + HTML.inlineMath('a^2 + b^2 = c^2')
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
// Block math expression
|
|
271
|
+
.mathBlock('\\int_{-\\infty}^{\\infty} e^{-x^2} dx = \\sqrt{\\pi}')
|
|
272
|
+
.mathBlock('F(x) = \\int_{-\\infty}^{x} f(t)\\,dt')
|
|
273
|
+
.mathBlock('E = mc^2')
|
|
274
|
+
.build()
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### Media — Photo, Video, Audio
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
new HTML()
|
|
281
|
+
// Photo
|
|
282
|
+
.photo('https://example.com/photo.jpg')
|
|
283
|
+
.photo('https://example.com/photo.jpg', 'Caption text')
|
|
284
|
+
.photo('https://example.com/photo.jpg', 'Spoiler photo', /* spoiler */ true)
|
|
285
|
+
|
|
286
|
+
// Video
|
|
287
|
+
.video('https://example.com/video.mp4')
|
|
288
|
+
.video('https://example.com/video.mp4', 'Caption text')
|
|
289
|
+
.video('https://example.com/video.mp4', 'Spoiler video', /* spoiler */ true)
|
|
290
|
+
|
|
291
|
+
// Audio / Voice note (.ogg for voice)
|
|
292
|
+
.audio('https://example.com/audio.mp3')
|
|
293
|
+
.audio('https://example.com/audio.mp3', 'Audio caption')
|
|
294
|
+
|
|
295
|
+
// With figcaption (HTML figure)
|
|
296
|
+
.raw('<figure><img src="https://example.com/photo.jpg"/><figcaption>Caption <b>bold</b></figcaption></figure>')
|
|
297
|
+
.build()
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
You can also use a Telegram `file_id` in place of a URL once the file is uploaded.
|
|
301
|
+
|
|
302
|
+
#### Collage & Slideshow
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
const img = (src) => `<img src="${src}"/>`
|
|
306
|
+
const vid = (src) => `<video src="${src}"></video>`
|
|
307
|
+
|
|
308
|
+
new HTML()
|
|
309
|
+
// Collage — displays as a grid
|
|
310
|
+
.collage(
|
|
311
|
+
img('https://example.com/photo1.jpg'),
|
|
312
|
+
img('https://example.com/photo2.jpg'),
|
|
313
|
+
vid('https://example.com/clip.mp4'),
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
// Slideshow — swipeable carousel
|
|
317
|
+
.slideshow(
|
|
318
|
+
img('https://example.com/photo1.jpg'),
|
|
319
|
+
img('https://example.com/photo2.jpg'),
|
|
320
|
+
img('https://example.com/photo3.jpg'),
|
|
321
|
+
)
|
|
322
|
+
.build()
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
#### Map
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
new HTML()
|
|
329
|
+
.map(-6.2088, 106.8456) // Jakarta
|
|
330
|
+
.map(48.8584, 2.2945, 16) // Paris, zoom 16
|
|
331
|
+
.map(51.5074, -0.1278, 14, 'Our office in London') // with caption
|
|
332
|
+
.build()
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
#### Table (HTML)
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
new HTML()
|
|
339
|
+
.table(
|
|
340
|
+
[
|
|
341
|
+
['Name', 'Version', 'Downloads'], // header row (hasHeader: true)
|
|
342
|
+
['gallagherrich', '4.16.5', '—' ],
|
|
343
|
+
['telegraf','4.16.3', '~120k/wk' ],
|
|
344
|
+
],
|
|
345
|
+
{ bordered: true, striped: true, hasHeader: true }
|
|
346
|
+
)
|
|
347
|
+
.build()
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
For advanced table markup (colspan, rowspan, alignment), use `.raw()`:
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
new HTML()
|
|
354
|
+
.raw(
|
|
355
|
+
'<table bordered striped>' +
|
|
356
|
+
'<tr><th>Name</th><th colspan="2">Details</th></tr>' +
|
|
357
|
+
'<tr><td>Alice</td><td align="center">98</td><td align="right">Pass</td></tr>' +
|
|
358
|
+
'</table>'
|
|
359
|
+
)
|
|
360
|
+
.build()
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
#### Details (Expandable)
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
new HTML()
|
|
367
|
+
// Collapsed by default
|
|
368
|
+
.details('Click to expand', '<p>Hidden content here.</p>')
|
|
369
|
+
|
|
370
|
+
// Open by default
|
|
371
|
+
.details(
|
|
372
|
+
HTML.bold('Changelog v4.16.5'),
|
|
373
|
+
'<ul><li>Fix InputRichMessage format</li><li>Add RichHTMLBuilder</li></ul>',
|
|
374
|
+
/* open */ true
|
|
375
|
+
)
|
|
376
|
+
.build()
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
#### Footnote References (HTML)
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
new HTML()
|
|
383
|
+
.paragraph(
|
|
384
|
+
'Telekaf ' + HTML.ref('note-1', '[1]') + ' is based on Telegraf ' + HTML.ref('note-2', '[2]') + '.'
|
|
385
|
+
)
|
|
386
|
+
.divider()
|
|
387
|
+
.referenceDefinition('note-1', HTML.url('https://npmjs.com/package/@sugansociety/gallagherrich', 'gallagherrich on npm'))
|
|
388
|
+
.referenceDefinition('note-2', HTML.url('https://github.com/telegraf/telegraf', 'telegraf on GitHub'))
|
|
389
|
+
.build()
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
#### Anchors & In-document Links
|
|
393
|
+
|
|
394
|
+
```ts
|
|
395
|
+
new HTML()
|
|
396
|
+
.raw(HTML.anchor('section-intro')) // invisible anchor target
|
|
397
|
+
.heading(2, 'Introduction')
|
|
398
|
+
.paragraph('Jump to: ' + HTML.anchorLink('section-api', 'API Reference'))
|
|
399
|
+
.raw(HTML.anchor('section-api'))
|
|
400
|
+
.heading(2, 'API Reference')
|
|
401
|
+
.build()
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
#### Special Elements
|
|
405
|
+
|
|
406
|
+
```ts
|
|
407
|
+
new HTML()
|
|
408
|
+
// AI thinking block (visible in sendRichMessageDraft)
|
|
409
|
+
.thinking(HTML.italic('Analyzing your request...'))
|
|
410
|
+
|
|
411
|
+
// Raw HTML for anything not covered by builder methods
|
|
412
|
+
.raw('<tg-map lat="41.9" long="12.5" zoom="14"/>')
|
|
413
|
+
.raw('<aside>Pull quote<cite>The Author</cite></aside>')
|
|
414
|
+
.build()
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
---
|
|
418
|
+
|
|
419
|
+
### RichMarkdownBuilder
|
|
420
|
+
|
|
421
|
+
Same API surface as `RichHTMLBuilder` but produces Markdown output:
|
|
422
|
+
|
|
423
|
+
```ts
|
|
424
|
+
import { RichMessage } from '@sugansociety/gallagherrich'
|
|
425
|
+
const { RichMarkdownBuilder: MD } = RichMessage
|
|
426
|
+
|
|
427
|
+
const msg = new MD()
|
|
428
|
+
.heading(1, 'Rich Markdown')
|
|
429
|
+
.paragraph(
|
|
430
|
+
MD.bold('bold') + ' ' +
|
|
431
|
+
MD.italic('italic') + ' ' +
|
|
432
|
+
MD.strikethrough('strike') + ' ' +
|
|
433
|
+
MD.marked('==highlighted==') + ' ' +
|
|
434
|
+
MD.spoiler('||spoiler||') + ' ' +
|
|
435
|
+
MD.code('`code`')
|
|
436
|
+
)
|
|
437
|
+
.divider()
|
|
438
|
+
.ul('Item 1', 'Item 2', MD.bold('Bold item'))
|
|
439
|
+
.ol('Step 1', 'Step 2', 'Step 3')
|
|
440
|
+
.taskList(
|
|
441
|
+
{ text: 'Done', checked: true },
|
|
442
|
+
{ text: 'Pending', checked: false },
|
|
443
|
+
)
|
|
444
|
+
.divider()
|
|
445
|
+
.pre('console.log("hello")', 'javascript')
|
|
446
|
+
.divider()
|
|
447
|
+
.table(
|
|
448
|
+
['Name', 'Score'],
|
|
449
|
+
[['Alice', '98'], ['Bob', '87']],
|
|
450
|
+
['left', 'center'],
|
|
451
|
+
)
|
|
452
|
+
.divider()
|
|
453
|
+
.mathBlock('E = mc^2')
|
|
454
|
+
.divider()
|
|
455
|
+
.blockQuote(MD.italic('"Quote text"'), '— Author')
|
|
456
|
+
.divider()
|
|
457
|
+
.photo('https://example.com/photo.jpg', 'Photo caption')
|
|
458
|
+
.collage('https://example.com/1.jpg', 'https://example.com/2.jpg')
|
|
459
|
+
.slideshow('https://example.com/1.jpg', 'https://example.com/2.jpg')
|
|
460
|
+
.divider()
|
|
461
|
+
.details('Expand me', '### Hidden heading\n\n- item 1\n- item 2')
|
|
462
|
+
.divider()
|
|
463
|
+
.paragraph('See footnote' + MD.sup('[1]'))
|
|
464
|
+
.footnote('1', MD.url('https://t.me/suganzi', '@suganzi'))
|
|
465
|
+
.build()
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
**All Markdown inline helpers:**
|
|
469
|
+
|
|
470
|
+
```ts
|
|
471
|
+
MD.bold('text') // **text**
|
|
472
|
+
MD.italic('text') // *text*
|
|
473
|
+
MD.underline('text') // <u>text</u>
|
|
474
|
+
MD.strikethrough('text') // ~~text~~
|
|
475
|
+
MD.spoiler('text') // ||text||
|
|
476
|
+
MD.code('text') // `text`
|
|
477
|
+
MD.marked('text') // ==text==
|
|
478
|
+
MD.sub('text') // <sub>text</sub>
|
|
479
|
+
MD.sup('text') // <sup>text</sup>
|
|
480
|
+
MD.url('https://...', 'label') // [label](url)
|
|
481
|
+
MD.email('a@b.com', 'label') // [label](mailto:a@b.com)
|
|
482
|
+
MD.phone('+123', 'label') // [label](tel:+123)
|
|
483
|
+
MD.mention(123456789, 'Alice') // [Alice](tg://user?id=123456789)
|
|
484
|
+
MD.customEmoji('id', '👍') // 
|
|
485
|
+
MD.time(1647531900, 'wDT') // 
|
|
486
|
+
MD.inlineMath('a^2') // $a^2$
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
---
|
|
490
|
+
|
|
491
|
+
### Streaming with sendRichMessageDraft
|
|
492
|
+
|
|
493
|
+
`sendRichMessageDraft` streams a partial rich message in private chats. The draft is ephemeral (30-second preview). You **must** finalize with `sendRichMessage` to persist it.
|
|
494
|
+
|
|
495
|
+
- `chat_id` — private chat only (integer)
|
|
496
|
+
- `draft_id` — non-zero integer; updates with the same `draft_id` are animated
|
|
497
|
+
|
|
498
|
+
```ts
|
|
499
|
+
bot.command('ai', async (ctx) => {
|
|
500
|
+
const DRAFT_ID = 1 // any non-zero integer
|
|
501
|
+
|
|
502
|
+
const steps = [
|
|
503
|
+
'Reading your request...',
|
|
504
|
+
'Searching knowledge base...',
|
|
505
|
+
'Composing answer...',
|
|
506
|
+
]
|
|
507
|
+
|
|
508
|
+
// Stream thinking blocks
|
|
509
|
+
for (const step of steps) {
|
|
510
|
+
await ctx.sendRichMessageDraft(
|
|
511
|
+
DRAFT_ID,
|
|
512
|
+
new HTML().thinking(HTML.italic(step)).build()
|
|
513
|
+
)
|
|
514
|
+
await new Promise((r) => setTimeout(r, 900))
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// Finalize — must call sendRichMessage after streaming
|
|
518
|
+
await ctx.sendRichMessage(
|
|
519
|
+
new HTML()
|
|
520
|
+
.heading(2, '🤖 AI Response')
|
|
521
|
+
.paragraph('Here is the final answer from the AI.')
|
|
522
|
+
.divider()
|
|
523
|
+
.footer(HTML.url('https://t.me/suganzi', '@suganzi'))
|
|
524
|
+
.build()
|
|
525
|
+
)
|
|
526
|
+
})
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
You can also call `sendRichMessageDraft` explicitly:
|
|
530
|
+
|
|
531
|
+
```ts
|
|
532
|
+
// Explicit
|
|
533
|
+
await ctx.telegram.sendRichMessageDraft(
|
|
534
|
+
ctx.chat.id, // private chat integer ID
|
|
535
|
+
42, // draft_id
|
|
536
|
+
new HTML().thinking('Processing...').build(),
|
|
537
|
+
{ message_thread_id: 123 }
|
|
538
|
+
)
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
---
|
|
542
|
+
|
|
543
|
+
### reply_markup with Rich Messages
|
|
544
|
+
|
|
545
|
+
All `sendRichMessage` calls accept a `reply_markup` option with inline keyboards:
|
|
546
|
+
|
|
547
|
+
```ts
|
|
548
|
+
import { Markup } from '@sugansociety/gallagherrich'
|
|
549
|
+
|
|
550
|
+
const msg = new HTML()
|
|
551
|
+
.heading(2, 'Choose an option')
|
|
552
|
+
.paragraph('Tap a button below:')
|
|
553
|
+
.build()
|
|
554
|
+
|
|
555
|
+
await ctx.sendRichMessage(msg, {
|
|
556
|
+
reply_markup: Markup.inlineKeyboard([
|
|
557
|
+
[
|
|
558
|
+
Markup.button.callback('✅ Yes', 'answer:yes'),
|
|
559
|
+
Markup.button.callback('❌ No', 'answer:no'),
|
|
560
|
+
],
|
|
561
|
+
[Markup.button.url('🌐 Visit', 'https://t.me/suganzi')],
|
|
562
|
+
]).reply_markup,
|
|
563
|
+
})
|
|
564
|
+
|
|
565
|
+
bot.action('answer:yes', async (ctx) => {
|
|
566
|
+
await ctx.answerCbQuery('You chose Yes!')
|
|
567
|
+
})
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
Or use `reply_markup` directly:
|
|
571
|
+
|
|
572
|
+
```ts
|
|
573
|
+
await ctx.sendRichMessage(msg, {
|
|
574
|
+
reply_markup: {
|
|
575
|
+
inline_keyboard: [
|
|
576
|
+
[{ text: 'Button 1', callback_data: 'btn1' }],
|
|
577
|
+
[{ text: 'Open URL', url: 'https://telegram.org' }],
|
|
578
|
+
],
|
|
579
|
+
},
|
|
580
|
+
})
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
---
|
|
584
|
+
|
|
585
|
+
### Inline / Web App Queries
|
|
586
|
+
|
|
587
|
+
Use `InputRichMessageContent` as `input_message_content` in inline query results:
|
|
588
|
+
|
|
589
|
+
```ts
|
|
590
|
+
import { RichMessage } from '@sugansociety/gallagherrich'
|
|
591
|
+
const { RichHTMLBuilder: HTML } = RichMessage
|
|
592
|
+
|
|
593
|
+
bot.on('inline_query', async (ctx) => {
|
|
594
|
+
const richContent: RichMessage.InputRichMessageContent = {
|
|
595
|
+
rich_message: new HTML()
|
|
596
|
+
.heading(1, 'Result from Inline Query')
|
|
597
|
+
.paragraph('Sent via ' + HTML.url('https://t.me/suganzi', '@suganzi') + '.')
|
|
598
|
+
.build(),
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
await ctx.answerInlineQuery([
|
|
602
|
+
{
|
|
603
|
+
type: 'article',
|
|
604
|
+
id: '1',
|
|
605
|
+
title: 'Rich Message Result',
|
|
606
|
+
input_message_content: richContent,
|
|
607
|
+
},
|
|
608
|
+
])
|
|
609
|
+
})
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
---
|
|
613
|
+
|
|
614
|
+
### TypeScript
|
|
615
|
+
|
|
616
|
+
All types are exported under the `RichMessage` namespace:
|
|
617
|
+
|
|
618
|
+
```ts
|
|
619
|
+
import { RichMessage } from '@sugansociety/gallagherrich'
|
|
620
|
+
import type { ExtraSendRichMessage, ExtraSendRichMessageDraft } from '@sugansociety/gallagherrich/types'
|
|
621
|
+
|
|
622
|
+
// Builder types
|
|
623
|
+
const builder: RichMessage.RichHTMLBuilder = new RichMessage.RichHTMLBuilder()
|
|
624
|
+
const mdBuilder: RichMessage.RichMarkdownBuilder = new RichMessage.RichMarkdownBuilder()
|
|
625
|
+
|
|
626
|
+
// Message types
|
|
627
|
+
const input: RichMessage.InputRichMessage = { html: '<p>hello</p>' }
|
|
628
|
+
const content: RichMessage.InputRichMessageContent = { rich_message: input }
|
|
629
|
+
const received: RichMessage.RichMessage = ctx.message.rich_message
|
|
630
|
+
|
|
631
|
+
// Extra types
|
|
632
|
+
const extra: ExtraSendRichMessage = {
|
|
633
|
+
disable_notification: true,
|
|
634
|
+
reply_markup: { inline_keyboard: [] },
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// Custom context with rich message
|
|
638
|
+
import { Context, Telegraf } from '@sugansociety/gallagherrich'
|
|
639
|
+
interface MyCtx extends Context {
|
|
640
|
+
session?: { lastDraftId: number }
|
|
641
|
+
}
|
|
642
|
+
const bot = new Telegraf<MyCtx>(process.env.BOT_TOKEN)
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
---
|
|
646
|
+
|
|
647
|
+
## For 3.x users
|
|
648
|
+
|
|
649
|
+
- [3.x docs](https://telegraf.js.org/v3)
|
|
650
|
+
- [4.0 release notes](https://github.com/telegraf/telegraf/releases/tag/v4.0.0)
|
|
651
|
+
|
|
652
|
+
## Introduction
|
|
653
|
+
|
|
654
|
+
Bots are special [Telegram](https://telegram.org) accounts designed to handle messages automatically.
|
|
655
|
+
Users can interact with bots by sending them command messages in private or group chats.
|
|
656
|
+
These accounts serve as an interface for code running somewhere on your server.
|
|
657
|
+
|
|
658
|
+
Telegraf is a library that makes it simple for you to develop your own Telegram bots using JavaScript or [TypeScript](https://www.typescriptlang.org/).
|
|
659
|
+
|
|
660
|
+
### Features
|
|
661
|
+
|
|
662
|
+
- Full [Telegram Bot API 10.1](https://core.telegram.org/bots/api) support with **Rich Messages**
|
|
663
|
+
- [Excellent TypeScript typings](https://github.com/telegraf/telegraf/releases/tag/v4.0.0)
|
|
664
|
+
- [Lightweight](https://packagephobia.com/result?p=telegraf,node-telegram-bot-api)
|
|
665
|
+
- [AWS **λ**](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html)
|
|
666
|
+
/ [Firebase](https://firebase.google.com/products/functions/)
|
|
667
|
+
/ [Glitch](https://glitch.com/edit/#!/dashing-light)
|
|
668
|
+
/ [Fly.io](https://fly.io/docs/languages-and-frameworks/node)
|
|
669
|
+
/ Whatever ready
|
|
670
|
+
- `http/https/fastify/Connect.js/express.js` compatible webhooks
|
|
671
|
+
- Extensible
|
|
672
|
+
|
|
673
|
+
### Example
|
|
674
|
+
|
|
675
|
+
```js
|
|
676
|
+
const { Telegraf } = require('@sugansociety/gallagherrich')
|
|
677
|
+
const { message } = require('@sugansociety/gallagherrich/filters')
|
|
678
|
+
|
|
679
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
680
|
+
bot.start((ctx) => ctx.reply('Welcome'))
|
|
681
|
+
bot.help((ctx) => ctx.reply('Send me a sticker'))
|
|
682
|
+
bot.on(message('sticker'), (ctx) => ctx.reply('👍'))
|
|
683
|
+
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
|
|
684
|
+
bot.launch()
|
|
685
|
+
|
|
686
|
+
// Enable graceful stop
|
|
687
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
688
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
```js
|
|
692
|
+
const { Telegraf } = require('@sugansociety/gallagherrich')
|
|
693
|
+
|
|
694
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
695
|
+
bot.command('oldschool', (ctx) => ctx.reply('Hello'))
|
|
696
|
+
bot.command('hipster', Telegraf.reply('λ'))
|
|
697
|
+
bot.launch()
|
|
698
|
+
|
|
699
|
+
// Enable graceful stop
|
|
700
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
701
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
For additional bot examples see the new [`docs repo`](https://github.com/feathers-studio/telegraf-docs/).
|
|
705
|
+
|
|
706
|
+
### Resources
|
|
707
|
+
|
|
708
|
+
- [Getting started](#getting-started)
|
|
709
|
+
- [API reference](https://telegraf.js.org/modules.html)
|
|
710
|
+
- Telegram groups (sorted by number of members):
|
|
711
|
+
- [English](https://t.me/TelegrafJSChat)
|
|
712
|
+
- [Russian](https://t.me/telegrafjs_ru)
|
|
713
|
+
- [Uzbek](https://t.me/botjs_uz)
|
|
714
|
+
- [Ethiopian](https://t.me/telegraf_et)
|
|
715
|
+
- [GitHub Discussions](https://github.com/telegraf/telegraf/discussions)
|
|
716
|
+
- [Dependent repositories](https://libraries.io/npm/telegraf/dependent_repositories)
|
|
717
|
+
|
|
718
|
+
## Getting started
|
|
719
|
+
|
|
720
|
+
### Telegram token
|
|
721
|
+
|
|
722
|
+
To use the [Telegram Bot API](https://core.telegram.org/bots/api),
|
|
723
|
+
you first have to [get a bot account](https://core.telegram.org/bots)
|
|
724
|
+
by [chatting with BotFather](https://core.telegram.org/bots#6-botfather).
|
|
725
|
+
|
|
726
|
+
BotFather will give you a _token_, something like `123456789:AbCdefGhIJKlmNoPQRsTUVwxyZ`.
|
|
727
|
+
|
|
728
|
+
### Installation
|
|
729
|
+
|
|
730
|
+
```shellscript
|
|
731
|
+
$ npm install @sugansociety/gallagherrich
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
or
|
|
735
|
+
|
|
736
|
+
```shellscript
|
|
737
|
+
$ yarn add @sugansociety/gallagherrich
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
or
|
|
741
|
+
|
|
742
|
+
```shellscript
|
|
743
|
+
$ pnpm add @sugansociety/gallagherrich
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
### `Telegraf` class
|
|
747
|
+
|
|
748
|
+
[`Telegraf`] instance represents your bot. It's responsible for obtaining updates and passing them to your handlers.
|
|
749
|
+
|
|
750
|
+
Start 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.
|
|
751
|
+
|
|
752
|
+
### `Context` class
|
|
753
|
+
|
|
754
|
+
`ctx` you can see in every example is a [`Context`] instance.
|
|
755
|
+
[`Telegraf`] creates one for each incoming update and passes it to your middleware.
|
|
756
|
+
It contains the `update`, `botInfo`, and `telegram` for making arbitrary Bot API requests,
|
|
757
|
+
as well as shorthand methods and getters.
|
|
758
|
+
|
|
759
|
+
This is probably the class you'll be using the most.
|
|
760
|
+
|
|
761
|
+
<!--
|
|
762
|
+
TODO: Verify and update list
|
|
763
|
+
Here is a list of
|
|
764
|
+
|
|
765
|
+
#### Known middleware
|
|
766
|
+
|
|
767
|
+
- [Internationalization](https://github.com/telegraf/telegraf-i18n)—simplifies selecting the right translation to use when responding to a user.
|
|
768
|
+
- [Redis powered session](https://github.com/telegraf/telegraf-session-redis)—store session data using Redis.
|
|
769
|
+
- [Local powered session (via lowdb)](https://github.com/RealSpeaker/telegraf-session-local)—store session data in a local file.
|
|
770
|
+
- [Rate-limiting](https://github.com/telegraf/telegraf-ratelimit)—apply rate limitting to chats or users.
|
|
771
|
+
- [Bottleneck powered throttling](https://github.com/KnightNiwrem/telegraf-throttler)—apply throttling to both incoming updates and outgoing API calls.
|
|
772
|
+
- [Menus via inline keyboards](https://github.com/EdJoPaTo/telegraf-inline-menu)—simplify creating interfaces based on menus.
|
|
773
|
+
- [Stateless Questions](https://github.com/EdJoPaTo/telegraf-stateless-question)—create stateless questions to Telegram users working in privacy mode.
|
|
774
|
+
- [Natural language processing via wit.ai](https://github.com/telegraf/telegraf-wit)
|
|
775
|
+
- [Natural language processing via recast.ai](https://github.com/telegraf/telegraf-recast)
|
|
776
|
+
- [Multivariate and A/B testing](https://github.com/telegraf/telegraf-experiments)—add experiments to see how different versions of a feature are used.
|
|
777
|
+
- [Powerfull bot stats via Mixpanel](https://github.com/telegraf/telegraf-mixpanel)
|
|
778
|
+
- [statsd integration](https://github.com/telegraf/telegraf-statsd)
|
|
779
|
+
- [and more...](https://www.npmjs.com/search?q=telegraf-)
|
|
780
|
+
-->
|
|
781
|
+
|
|
782
|
+
#### Shorthand methods
|
|
783
|
+
|
|
784
|
+
```js
|
|
785
|
+
import { Telegraf } from '@sugansociety/gallagherrich'
|
|
786
|
+
import { message } from '@sugansociety/gallagherrich/filters'
|
|
787
|
+
|
|
788
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
789
|
+
|
|
790
|
+
bot.command('quit', async (ctx) => {
|
|
791
|
+
// Explicit usage
|
|
792
|
+
await ctx.telegram.leaveChat(ctx.message.chat.id)
|
|
793
|
+
|
|
794
|
+
// Using context shortcut
|
|
795
|
+
await ctx.leaveChat()
|
|
796
|
+
})
|
|
797
|
+
|
|
798
|
+
bot.on(message('text'), async (ctx) => {
|
|
799
|
+
// Explicit usage
|
|
800
|
+
await ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`)
|
|
801
|
+
|
|
802
|
+
// Using context shortcut
|
|
803
|
+
await ctx.reply(`Hello ${ctx.state.role}`)
|
|
804
|
+
})
|
|
805
|
+
|
|
806
|
+
bot.on('callback_query', async (ctx) => {
|
|
807
|
+
// Explicit usage
|
|
808
|
+
await ctx.telegram.answerCbQuery(ctx.callbackQuery.id)
|
|
809
|
+
|
|
810
|
+
// Using context shortcut
|
|
811
|
+
await ctx.answerCbQuery()
|
|
812
|
+
})
|
|
813
|
+
|
|
814
|
+
bot.on('inline_query', async (ctx) => {
|
|
815
|
+
const result = []
|
|
816
|
+
// Explicit usage
|
|
817
|
+
await ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, result)
|
|
818
|
+
|
|
819
|
+
// Using context shortcut
|
|
820
|
+
await ctx.answerInlineQuery(result)
|
|
821
|
+
})
|
|
822
|
+
|
|
823
|
+
bot.launch()
|
|
824
|
+
|
|
825
|
+
// Enable graceful stop
|
|
826
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
827
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
828
|
+
```
|
|
829
|
+
|
|
830
|
+
## Production
|
|
831
|
+
|
|
832
|
+
### Webhooks
|
|
833
|
+
|
|
834
|
+
```TS
|
|
835
|
+
import { Telegraf } from "@sugansociety/gallagherrich";
|
|
836
|
+
import { message } from '@sugansociety/gallagherrich/filters';
|
|
837
|
+
|
|
838
|
+
const bot = new Telegraf(token);
|
|
839
|
+
|
|
840
|
+
bot.on(message("text"), ctx => ctx.reply("Hello"));
|
|
841
|
+
|
|
842
|
+
// Start webhook via launch method (preferred)
|
|
843
|
+
bot.launch({
|
|
844
|
+
webhook: {
|
|
845
|
+
// Public domain for webhook; e.g.: example.com
|
|
846
|
+
domain: webhookDomain,
|
|
847
|
+
|
|
848
|
+
// Port to listen on; e.g.: 8080
|
|
849
|
+
port: port,
|
|
850
|
+
|
|
851
|
+
// Optional path to listen for.
|
|
852
|
+
// `bot.secretPathComponent()` will be used by default
|
|
853
|
+
path: webhookPath,
|
|
854
|
+
|
|
855
|
+
// Optional secret to be sent back in a header for security.
|
|
856
|
+
// e.g.: `crypto.randomBytes(64).toString("hex")`
|
|
857
|
+
secretToken: randomAlphaNumericString,
|
|
858
|
+
},
|
|
859
|
+
});
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
Use `createWebhook()` if you want to attach Telegraf to an existing http server.
|
|
863
|
+
|
|
864
|
+
<!-- global bot, tlsOptions -->
|
|
865
|
+
|
|
866
|
+
```TS
|
|
867
|
+
import { createServer } from "http";
|
|
868
|
+
|
|
869
|
+
createServer(await bot.createWebhook({ domain: "example.com" })).listen(3000);
|
|
870
|
+
```
|
|
871
|
+
|
|
872
|
+
```TS
|
|
873
|
+
import { createServer } from "https";
|
|
874
|
+
|
|
875
|
+
createServer(tlsOptions, await bot.createWebhook({ domain: "example.com" })).listen(8443);
|
|
876
|
+
```
|
|
877
|
+
|
|
878
|
+
- [AWS Lambda example integration](https://github.com/feathers-studio/telegraf-docs/tree/master/examples/functions/aws-lambda)
|
|
879
|
+
- [Google Cloud Functions example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/functions/google-cloud-function.ts)
|
|
880
|
+
- [`express` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/express.ts)
|
|
881
|
+
- [`fastify` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/fastify.ts)
|
|
882
|
+
- [`koa` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/koa.ts)
|
|
883
|
+
- [NestJS framework integration module](https://github.com/bukhalo/nestjs-telegraf)
|
|
884
|
+
- [Cloudflare Workers integration module](https://github.com/Tsuk1ko/cfworker-middware-telegraf)
|
|
885
|
+
- Use [`bot.handleUpdate`](https://telegraf.js.org/classes/Telegraf-1.html#handleupdate) to write new integrations
|
|
886
|
+
|
|
887
|
+
### Error handling
|
|
888
|
+
|
|
889
|
+
If 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 terminates. If it does not rethrow, the error is swallowed.
|
|
890
|
+
|
|
891
|
+
Default `bot.handleError` always rethrows. You can overwrite it using `bot.catch` if you need to.
|
|
892
|
+
|
|
893
|
+
⚠️ Swallowing unknown errors might leave the process in invalid state!
|
|
894
|
+
|
|
895
|
+
ℹ️ In production, `systemd` or [`pm2`](https://www.npmjs.com/package/pm2) can restart your bot if it exits for any reason.
|
|
896
|
+
|
|
897
|
+
## Advanced topics
|
|
898
|
+
|
|
899
|
+
### Working with files
|
|
900
|
+
|
|
901
|
+
Supported file sources:
|
|
902
|
+
|
|
903
|
+
- `Existing file_id`
|
|
904
|
+
- `File path`
|
|
905
|
+
- `Url`
|
|
906
|
+
- `Buffer`
|
|
907
|
+
- `ReadStream`
|
|
908
|
+
|
|
909
|
+
Also, you can provide an optional name of a file as `filename` when you send the file.
|
|
910
|
+
|
|
911
|
+
<!-- global bot, fs -->
|
|
912
|
+
|
|
913
|
+
```js
|
|
914
|
+
bot.on('message', async (ctx) => {
|
|
915
|
+
// resend existing file by file_id
|
|
916
|
+
await ctx.replyWithSticker('123123jkbhj6b')
|
|
917
|
+
|
|
918
|
+
// send file
|
|
919
|
+
await ctx.replyWithVideo(Input.fromLocalFile('/path/to/video.mp4'))
|
|
920
|
+
|
|
921
|
+
// send stream
|
|
922
|
+
await ctx.replyWithVideo(
|
|
923
|
+
Input.fromReadableStream(fs.createReadStream('/path/to/video.mp4'))
|
|
924
|
+
)
|
|
925
|
+
|
|
926
|
+
// send buffer
|
|
927
|
+
await ctx.replyWithVoice(Input.fromBuffer(Buffer.alloc()))
|
|
928
|
+
|
|
929
|
+
// send url via Telegram server
|
|
930
|
+
await ctx.replyWithPhoto(Input.fromURL('https://picsum.photos/200/300/'))
|
|
931
|
+
|
|
932
|
+
// pipe url content
|
|
933
|
+
await ctx.replyWithPhoto(
|
|
934
|
+
Input.fromURLStream('https://picsum.photos/200/300/?random', 'kitten.jpg')
|
|
935
|
+
)
|
|
936
|
+
})
|
|
937
|
+
```
|
|
938
|
+
|
|
939
|
+
### Middleware
|
|
940
|
+
|
|
941
|
+
In addition to `ctx: Context`, each middleware receives `next: () => Promise<void>`.
|
|
942
|
+
|
|
943
|
+
As in Koa and some other middleware-based libraries,
|
|
944
|
+
`await next()` will call next middleware and wait for it to finish:
|
|
945
|
+
|
|
946
|
+
```TS
|
|
947
|
+
import { Telegraf } from '@sugansociety/gallagherrich';
|
|
948
|
+
import { message } from '@sugansociety/gallagherrich/filters';
|
|
949
|
+
|
|
950
|
+
const bot = new Telegraf(process.env.BOT_TOKEN);
|
|
951
|
+
|
|
952
|
+
bot.use(async (ctx, next) => {
|
|
953
|
+
console.time(`Processing update ${ctx.update.update_id}`);
|
|
954
|
+
await next() // runs next middleware
|
|
955
|
+
// runs after next middleware finishes
|
|
956
|
+
console.timeEnd(`Processing update ${ctx.update.update_id}`);
|
|
957
|
+
})
|
|
958
|
+
|
|
959
|
+
bot.on(message('text'), (ctx) => ctx.reply('Hello World'));
|
|
960
|
+
bot.launch();
|
|
961
|
+
|
|
962
|
+
// Enable graceful stop
|
|
963
|
+
process.once('SIGINT', () => bot.stop('SIGINT'));
|
|
964
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'));
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
With this simple ability, you can:
|
|
968
|
+
|
|
969
|
+
- extract information from updates and then `await next()` to avoid disrupting other middleware,
|
|
970
|
+
- like [`Composer`] and [`Router`], `await next()` for updates you don't wish to handle,
|
|
971
|
+
- like [`session`] and [`Scenes`], [extend the context](#extending-context) by mutating `ctx` before `await next()`,
|
|
972
|
+
- [intercept API calls](https://github.com/telegraf/telegraf/discussions/1267#discussioncomment-254525),
|
|
973
|
+
- reuse [other people's code](https://www.npmjs.com/search?q=telegraf-),
|
|
974
|
+
- do whatever **you** come up with!
|
|
975
|
+
|
|
976
|
+
[`Telegraf`]: https://telegraf.js.org/classes/Telegraf-1.html
|
|
977
|
+
[`Composer`]: https://telegraf.js.org/classes/Composer.html
|
|
978
|
+
[`Context`]: https://telegraf.js.org/classes/Context.html
|
|
979
|
+
[`Router`]: https://telegraf.js.org/classes/Router.html
|
|
980
|
+
[`session`]: https://telegraf.js.org/modules.html#session
|
|
981
|
+
[`Scenes`]: https://telegraf.js.org/modules/Scenes.html
|
|
982
|
+
|
|
983
|
+
### Usage with TypeScript
|
|
984
|
+
|
|
985
|
+
Telegraf is written in TypeScript and therefore ships with declaration files for the entire library.
|
|
986
|
+
Moreover, it includes types for the complete Telegram API via the [`typegram`](https://github.com/KnorpelSenf/typegram) package.
|
|
987
|
+
While most types of Telegraf's API surface are self-explanatory, there's some notable things to keep in mind.
|
|
988
|
+
|
|
989
|
+
#### Extending `Context`
|
|
990
|
+
|
|
991
|
+
The exact shape of `ctx` can vary based on the installed middleware.
|
|
992
|
+
Some custom middleware might register properties on the context object that Telegraf is not aware of.
|
|
993
|
+
Consequently, you can change the type of `ctx` to fit your needs in order for you to have proper TypeScript types for your data.
|
|
994
|
+
This is done through Generics:
|
|
995
|
+
|
|
996
|
+
```ts
|
|
997
|
+
import { Context, Telegraf } from '@sugansociety/gallagherrich'
|
|
998
|
+
|
|
999
|
+
// Define your own context type
|
|
1000
|
+
interface MyContext extends Context {
|
|
1001
|
+
myProp?: string
|
|
1002
|
+
myOtherProp?: number
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// Create your bot and tell it about your context type
|
|
1006
|
+
const bot = new Telegraf<MyContext>('SECRET TOKEN')
|
|
1007
|
+
|
|
1008
|
+
// Register middleware and launch your bot as usual
|
|
1009
|
+
bot.use((ctx, next) => {
|
|
1010
|
+
// Yay, `myProp` is now available here as `string | undefined`!
|
|
1011
|
+
ctx.myProp = ctx.chat?.first_name?.toUpperCase()
|
|
1012
|
+
return next()
|
|
1013
|
+
})
|
|
1014
|
+
// ...
|
|
1015
|
+
```
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
---
|
|
1019
|
+
|
|
1020
|
+
## Author & Maintainer
|
|
1021
|
+
|
|
1022
|
+
This fork is maintained by **@suganzi** — [t.me/suganzi](https://t.me/suganzi)
|
|
1023
|
+
|
|
1024
|
+
Upstream project: [telegraf/telegraf](https://github.com/telegraf/telegraf) by The Telegraf Contributors.
|