@tgify/tgify 0.1.0
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 +23 -0
- package/README.md +356 -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 +100 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +582 -0
- package/lib/context.js +1219 -0
- package/lib/core/helpers/args.js +57 -0
- package/lib/core/helpers/check.js +55 -0
- package/lib/core/helpers/compact.js +16 -0
- package/lib/core/helpers/deunionize.js +12 -0
- package/lib/core/helpers/formatting.js +91 -0
- package/lib/core/helpers/util.js +50 -0
- package/lib/core/network/client.js +330 -0
- package/lib/core/network/error.js +21 -0
- package/lib/core/network/multipart-stream.js +71 -0
- package/lib/core/network/polling.js +87 -0
- package/lib/core/network/webhook.js +54 -0
- package/lib/core/types/typegram.js +27 -0
- package/lib/filters.js +69 -0
- package/lib/format.js +38 -0
- package/lib/future.js +149 -0
- package/lib/index.js +58 -0
- package/lib/input.js +61 -0
- package/lib/markup.js +121 -0
- package/lib/middleware.js +2 -0
- package/lib/reactions.js +84 -0
- package/lib/router.js +46 -0
- package/lib/scenes/base.js +39 -0
- package/lib/scenes/context.js +104 -0
- package/lib/scenes/index.js +21 -0
- package/lib/scenes/stage.js +49 -0
- package/lib/scenes/wizard/context.js +31 -0
- package/lib/scenes/wizard/index.js +45 -0
- package/lib/scenes.js +17 -0
- package/lib/session.js +166 -0
- package/lib/telegraf.js +256 -0
- package/lib/telegram-types.js +6 -0
- package/lib/telegram.js +1240 -0
- package/lib/types.js +2 -0
- package/lib/utils.js +5 -0
- package/markup.d.ts +1 -0
- package/markup.js +1 -0
- package/package.json +140 -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 +1661 -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/typegram.ts +54 -0
- package/src/filters.ts +109 -0
- package/src/format.ts +110 -0
- package/src/future.ts +213 -0
- package/src/index.ts +17 -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 +354 -0
- package/src/telegram-types.ts +219 -0
- package/src/telegram.ts +1635 -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 +655 -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 +26 -0
- package/typings/core/helpers/util.d.ts.map +1 -0
- package/typings/core/network/client.d.ts +53 -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 +16 -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 +6 -0
- package/typings/core/network/webhook.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +42 -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 +15 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +50 -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 +115 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +117 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +675 -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/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-2019 Vitaly Domnikov
|
|
4
|
+
Copyright (c) 2020-2023 The Telegraf Contributors
|
|
5
|
+
Copyright (c) 2026 IATNAOD
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
<header>
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<h1 align="center">tgify</h1>
|
|
5
|
+
|
|
6
|
+
<p>Modern Telegram Bot API framework for Node.js</p>
|
|
7
|
+
|
|
8
|
+
<a href="https://core.telegram.org/bots/api">
|
|
9
|
+
<img src="https://img.shields.io/badge/Bot%20API-v7.1-f36caf.svg?style=flat-square" alt="Bot API Version" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://packagephobia.com/result?p=tgify,node-telegram-bot-api">
|
|
12
|
+
<img src="https://flat.badgen.net/packagephobia/install/tgify" alt="install size" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/IATNAOD/tgify">
|
|
15
|
+
<img src="https://img.shields.io/github/languages/top/IATNAOD/tgify?style=flat-square&logo=github" alt="GitHub top language" />
|
|
16
|
+
</a>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
</header>
|
|
20
|
+
|
|
21
|
+
## Introduction
|
|
22
|
+
|
|
23
|
+
Bots are special [Telegram](https://telegram.org) accounts designed to handle messages automatically.
|
|
24
|
+
Users can interact with bots by sending them command messages in private or group chats.
|
|
25
|
+
These accounts serve as an interface for code running somewhere on your server.
|
|
26
|
+
|
|
27
|
+
Tgify is a library that makes it simple for you to develop your own Telegram bots using JavaScript or [TypeScript](https://www.typescriptlang.org/).
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
- Full [Telegram Bot API 7.1](https://core.telegram.org/bots/api) support
|
|
32
|
+
- [Excellent TypeScript typings](https://github.com/IATNAOD/tgify/releases/tag/v4.0.0)
|
|
33
|
+
- [Lightweight](https://packagephobia.com/result?p=tgify,node-telegram-bot-api)
|
|
34
|
+
- [AWS **λ**](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html)
|
|
35
|
+
/ [Firebase](https://firebase.google.com/products/functions/)
|
|
36
|
+
/ [Glitch](https://glitch.com/edit/#!/dashing-light)
|
|
37
|
+
/ [Fly.io](https://fly.io/docs/languages-and-frameworks/node)
|
|
38
|
+
/ Whatever ready
|
|
39
|
+
- `http/https/fastify/Connect.js/express.js` compatible webhooks
|
|
40
|
+
- Extensible
|
|
41
|
+
|
|
42
|
+
### Example
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
const { Telegraf } = require('telegraf')
|
|
46
|
+
const { message } = require('telegraf/filters')
|
|
47
|
+
|
|
48
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
49
|
+
bot.start((ctx) => ctx.reply('Welcome'))
|
|
50
|
+
bot.help((ctx) => ctx.reply('Send me a sticker'))
|
|
51
|
+
bot.on(message('sticker'), (ctx) => ctx.reply('👍'))
|
|
52
|
+
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
|
|
53
|
+
bot.launch()
|
|
54
|
+
|
|
55
|
+
// Enable graceful stop
|
|
56
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
57
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
const { Telegraf } = require('telegraf')
|
|
62
|
+
|
|
63
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
64
|
+
bot.command('oldschool', (ctx) => ctx.reply('Hello'))
|
|
65
|
+
bot.command('hipster', Telegraf.reply('λ'))
|
|
66
|
+
bot.launch()
|
|
67
|
+
|
|
68
|
+
// Enable graceful stop
|
|
69
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
70
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
For additional bot examples see the new [`docs repo`](https://github.com/feathers-studio/telegraf-docs/).
|
|
74
|
+
|
|
75
|
+
### Resources
|
|
76
|
+
|
|
77
|
+
- [Getting started](#getting-started)
|
|
78
|
+
- [GitHub Discussions](https://github.com/IATNAOD/tgify/discussions)
|
|
79
|
+
- [Dependent repositories](https://libraries.io/npm/tgify/dependent_repositories)
|
|
80
|
+
|
|
81
|
+
## Getting started
|
|
82
|
+
|
|
83
|
+
### Telegram token
|
|
84
|
+
|
|
85
|
+
To use the [Telegram Bot API](https://core.telegram.org/bots/api),
|
|
86
|
+
you first have to [get a bot account](https://core.telegram.org/bots)
|
|
87
|
+
by [chatting with BotFather](https://core.telegram.org/bots#6-botfather).
|
|
88
|
+
|
|
89
|
+
BotFather will give you a _token_, something like `123456789:AbCdefGhIJKlmNoPQRsTUVwxyZ`.
|
|
90
|
+
|
|
91
|
+
### Installation
|
|
92
|
+
|
|
93
|
+
```shellscript
|
|
94
|
+
$ npm install tgify
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
or
|
|
98
|
+
|
|
99
|
+
```shellscript
|
|
100
|
+
$ yarn add tgify
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
or
|
|
104
|
+
|
|
105
|
+
```shellscript
|
|
106
|
+
$ pnpm add tgify
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### `Telegraf` class
|
|
110
|
+
|
|
111
|
+
[`Telegraf`] instance represents your bot. It's responsible for obtaining updates and passing them to your handlers.
|
|
112
|
+
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
### `Context` class
|
|
116
|
+
|
|
117
|
+
`ctx` you can see in every example is a [`Context`] instance.
|
|
118
|
+
[`Telegraf`] creates one for each incoming update and passes it to your middleware.
|
|
119
|
+
It contains the `update`, `botInfo`, and `telegram` for making arbitrary Bot API requests,
|
|
120
|
+
as well as shorthand methods and getters.
|
|
121
|
+
|
|
122
|
+
This is probably the class you'll be using the most.
|
|
123
|
+
|
|
124
|
+
#### Shorthand methods
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
import { Telegraf } from 'telegraf'
|
|
128
|
+
import { message } from 'telegraf/filters'
|
|
129
|
+
|
|
130
|
+
const bot = new Telegraf(process.env.BOT_TOKEN)
|
|
131
|
+
|
|
132
|
+
bot.command('quit', async (ctx) => {
|
|
133
|
+
// Explicit usage
|
|
134
|
+
await ctx.telegram.leaveChat(ctx.message.chat.id)
|
|
135
|
+
|
|
136
|
+
// Using context shortcut
|
|
137
|
+
await ctx.leaveChat()
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
bot.on(message('text'), async (ctx) => {
|
|
141
|
+
// Explicit usage
|
|
142
|
+
await ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`)
|
|
143
|
+
|
|
144
|
+
// Using context shortcut
|
|
145
|
+
await ctx.reply(`Hello ${ctx.state.role}`)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
bot.on('callback_query', async (ctx) => {
|
|
149
|
+
// Explicit usage
|
|
150
|
+
await ctx.telegram.answerCbQuery(ctx.callbackQuery.id)
|
|
151
|
+
|
|
152
|
+
// Using context shortcut
|
|
153
|
+
await ctx.answerCbQuery()
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
bot.on('inline_query', async (ctx) => {
|
|
157
|
+
const result = []
|
|
158
|
+
// Explicit usage
|
|
159
|
+
await ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, result)
|
|
160
|
+
|
|
161
|
+
// Using context shortcut
|
|
162
|
+
await ctx.answerInlineQuery(result)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
bot.launch()
|
|
166
|
+
|
|
167
|
+
// Enable graceful stop
|
|
168
|
+
process.once('SIGINT', () => bot.stop('SIGINT'))
|
|
169
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'))
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Production
|
|
173
|
+
|
|
174
|
+
### Webhooks
|
|
175
|
+
|
|
176
|
+
```TS
|
|
177
|
+
import { Telegraf } from "telegraf";
|
|
178
|
+
import { message } from 'telegraf/filters';
|
|
179
|
+
|
|
180
|
+
const bot = new Telegraf(token);
|
|
181
|
+
|
|
182
|
+
bot.on(message("text"), ctx => ctx.reply("Hello"));
|
|
183
|
+
|
|
184
|
+
// Start webhook via launch method (preferred)
|
|
185
|
+
bot.launch({
|
|
186
|
+
webhook: {
|
|
187
|
+
// Public domain for webhook; e.g.: example.com
|
|
188
|
+
domain: webhookDomain,
|
|
189
|
+
|
|
190
|
+
// Port to listen on; e.g.: 8080
|
|
191
|
+
port: port,
|
|
192
|
+
|
|
193
|
+
// Optional path to listen for.
|
|
194
|
+
// `bot.secretPathComponent()` will be used by default
|
|
195
|
+
path: webhookPath,
|
|
196
|
+
|
|
197
|
+
// Optional secret to be sent back in a header for security.
|
|
198
|
+
// e.g.: `crypto.randomBytes(64).toString("hex")`
|
|
199
|
+
secretToken: randomAlphaNumericString,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Use `createWebhook()` if you want to attach Telegraf to an existing http server.
|
|
205
|
+
|
|
206
|
+
<!-- global bot, tlsOptions -->
|
|
207
|
+
|
|
208
|
+
```TS
|
|
209
|
+
import { createServer } from "http";
|
|
210
|
+
|
|
211
|
+
createServer(await bot.createWebhook({ domain: "example.com" })).listen(3000);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```TS
|
|
215
|
+
import { createServer } from "https";
|
|
216
|
+
|
|
217
|
+
createServer(tlsOptions, await bot.createWebhook({ domain: "example.com" })).listen(8443);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
- [AWS Lambda example integration](https://github.com/feathers-studio/telegraf-docs/tree/master/examples/functions/aws-lambda)
|
|
221
|
+
- [Google Cloud Functions example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/functions/google-cloud-function.ts)
|
|
222
|
+
- [`express` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/express.ts)
|
|
223
|
+
- [`fastify` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/fastify.ts)
|
|
224
|
+
- [`koa` example integration](https://github.com/feathers-studio/telegraf-docs/blob/master/examples/webhook/koa.ts)
|
|
225
|
+
- [Cloudflare Workers integration module](https://github.com/Tsuk1ko/cfworker-middware-telegraf)
|
|
226
|
+
- Use [`bot.handleUpdate`](https://telegraf.js.org/classes/Telegraf-1.html#handleupdate) to write new integrations
|
|
227
|
+
|
|
228
|
+
### Error handling
|
|
229
|
+
|
|
230
|
+
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.
|
|
231
|
+
|
|
232
|
+
Default `bot.handleError` always rethrows. You can overwrite it using `bot.catch` if you need to.
|
|
233
|
+
|
|
234
|
+
⚠️ Swallowing unknown errors might leave the process in invalid state!
|
|
235
|
+
|
|
236
|
+
ℹ️ In production, `systemd` or [`pm2`](https://www.npmjs.com/package/pm2) can restart your bot if it exits for any reason.
|
|
237
|
+
|
|
238
|
+
## Advanced topics
|
|
239
|
+
|
|
240
|
+
### Working with files
|
|
241
|
+
|
|
242
|
+
Supported file sources:
|
|
243
|
+
|
|
244
|
+
- `Existing file_id`
|
|
245
|
+
- `File path`
|
|
246
|
+
- `Url`
|
|
247
|
+
- `Buffer`
|
|
248
|
+
- `ReadStream`
|
|
249
|
+
|
|
250
|
+
Also, you can provide an optional name of a file as `filename` when you send the file.
|
|
251
|
+
|
|
252
|
+
<!-- global bot, fs -->
|
|
253
|
+
|
|
254
|
+
```js
|
|
255
|
+
bot.on('message', async (ctx) => {
|
|
256
|
+
// resend existing file by file_id
|
|
257
|
+
await ctx.replyWithSticker('123123jkbhj6b')
|
|
258
|
+
|
|
259
|
+
// send file
|
|
260
|
+
await ctx.replyWithVideo(Input.fromLocalFile('/path/to/video.mp4'))
|
|
261
|
+
|
|
262
|
+
// send stream
|
|
263
|
+
await ctx.replyWithVideo(
|
|
264
|
+
Input.fromReadableStream(fs.createReadStream('/path/to/video.mp4'))
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
// send buffer
|
|
268
|
+
await ctx.replyWithVoice(Input.fromBuffer(Buffer.alloc()))
|
|
269
|
+
|
|
270
|
+
// send url via Telegram server
|
|
271
|
+
await ctx.replyWithPhoto(Input.fromURL('https://picsum.photos/200/300/'))
|
|
272
|
+
|
|
273
|
+
// pipe url content
|
|
274
|
+
await ctx.replyWithPhoto(
|
|
275
|
+
Input.fromURLStream('https://picsum.photos/200/300/?random', 'kitten.jpg')
|
|
276
|
+
)
|
|
277
|
+
})
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Middleware
|
|
281
|
+
|
|
282
|
+
In addition to `ctx: Context`, each middleware receives `next: () => Promise<void>`.
|
|
283
|
+
|
|
284
|
+
As in Koa and some other middleware-based libraries,
|
|
285
|
+
`await next()` will call next middleware and wait for it to finish:
|
|
286
|
+
|
|
287
|
+
```TS
|
|
288
|
+
import { Telegraf } from 'telegraf';
|
|
289
|
+
import { message } from 'telegraf/filters';
|
|
290
|
+
|
|
291
|
+
const bot = new Telegraf(process.env.BOT_TOKEN);
|
|
292
|
+
|
|
293
|
+
bot.use(async (ctx, next) => {
|
|
294
|
+
console.time(`Processing update ${ctx.update.update_id}`);
|
|
295
|
+
await next() // runs next middleware
|
|
296
|
+
// runs after next middleware finishes
|
|
297
|
+
console.timeEnd(`Processing update ${ctx.update.update_id}`);
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
bot.on(message('text'), (ctx) => ctx.reply('Hello World'));
|
|
301
|
+
bot.launch();
|
|
302
|
+
|
|
303
|
+
// Enable graceful stop
|
|
304
|
+
process.once('SIGINT', () => bot.stop('SIGINT'));
|
|
305
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'));
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
With this simple ability, you can:
|
|
309
|
+
|
|
310
|
+
- extract information from updates and then `await next()` to avoid disrupting other middleware,
|
|
311
|
+
- like [`Composer`] and [`Router`], `await next()` for updates you don't wish to handle,
|
|
312
|
+
- like [`session`] and [`Scenes`], [extend the context](#extending-context) by mutating `ctx` before `await next()`,
|
|
313
|
+
- [intercept API calls](https://github.com/telegraf/telegraf/discussions/1267#discussioncomment-254525),
|
|
314
|
+
- reuse [other people's code](https://www.npmjs.com/search?q=telegraf-),
|
|
315
|
+
- do whatever **you** come up with!
|
|
316
|
+
|
|
317
|
+
[`Telegraf`]: https://telegraf.js.org/classes/Telegraf-1.html
|
|
318
|
+
[`Composer`]: https://telegraf.js.org/classes/Composer.html
|
|
319
|
+
[`Context`]: https://telegraf.js.org/classes/Context.html
|
|
320
|
+
[`Router`]: https://telegraf.js.org/classes/Router.html
|
|
321
|
+
[`session`]: https://telegraf.js.org/modules.html#session
|
|
322
|
+
[`Scenes`]: https://telegraf.js.org/modules/Scenes.html
|
|
323
|
+
|
|
324
|
+
### Usage with TypeScript
|
|
325
|
+
|
|
326
|
+
Tgify is written in TypeScript and therefore ships with declaration files for the entire library.
|
|
327
|
+
Moreover, it includes types for the complete Telegram API via the [`typegram`](https://github.com/KnorpelSenf/typegram) package.
|
|
328
|
+
While most types of Tgify's API surface are self-explanatory, there's some notable things to keep in mind.
|
|
329
|
+
|
|
330
|
+
#### Extending `Context`
|
|
331
|
+
|
|
332
|
+
The exact shape of `ctx` can vary based on the installed middleware.
|
|
333
|
+
Some custom middleware might register properties on the context object that Telegraf is not aware of.
|
|
334
|
+
Consequently, you can change the type of `ctx` to fit your needs in order for you to have proper TypeScript types for your data.
|
|
335
|
+
This is done through Generics:
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
import { Context, Telegraf } from 'telegraf'
|
|
339
|
+
|
|
340
|
+
// Define your own context type
|
|
341
|
+
interface MyContext extends Context {
|
|
342
|
+
myProp?: string
|
|
343
|
+
myOtherProp?: number
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Create your bot and tell it about your context type
|
|
347
|
+
const bot = new Telegraf<MyContext>('SECRET TOKEN')
|
|
348
|
+
|
|
349
|
+
// Register middleware and launch your bot as usual
|
|
350
|
+
bot.use((ctx, next) => {
|
|
351
|
+
// Yay, `myProp` is now available here as `string | undefined`!
|
|
352
|
+
ctx.myProp = ctx.chat?.first_name?.toUpperCase()
|
|
353
|
+
return next()
|
|
354
|
+
})
|
|
355
|
+
// ...
|
|
356
|
+
```
|
package/filters.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typings/filters'
|
package/filters.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/filters')
|
package/format.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typings/format'
|
package/format.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/format')
|
package/future.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typings/future'
|
package/future.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/future')
|
package/lib/button.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.text = text;
|
|
4
|
+
exports.contactRequest = contactRequest;
|
|
5
|
+
exports.locationRequest = locationRequest;
|
|
6
|
+
exports.pollRequest = pollRequest;
|
|
7
|
+
exports.userRequest = userRequest;
|
|
8
|
+
exports.botRequest = botRequest;
|
|
9
|
+
exports.groupRequest = groupRequest;
|
|
10
|
+
exports.channelRequest = channelRequest;
|
|
11
|
+
exports.url = url;
|
|
12
|
+
exports.callback = callback;
|
|
13
|
+
exports.switchToChat = switchToChat;
|
|
14
|
+
exports.switchToCurrentChat = switchToCurrentChat;
|
|
15
|
+
exports.game = game;
|
|
16
|
+
exports.pay = pay;
|
|
17
|
+
exports.login = login;
|
|
18
|
+
exports.webApp = webApp;
|
|
19
|
+
function text(text, hide = false) {
|
|
20
|
+
return { text, hide };
|
|
21
|
+
}
|
|
22
|
+
function contactRequest(text, hide = false) {
|
|
23
|
+
return { text, request_contact: true, hide };
|
|
24
|
+
}
|
|
25
|
+
function locationRequest(text, hide = false) {
|
|
26
|
+
return { text, request_location: true, hide };
|
|
27
|
+
}
|
|
28
|
+
function pollRequest(text, type, hide = false) {
|
|
29
|
+
return { text, request_poll: { type }, hide };
|
|
30
|
+
}
|
|
31
|
+
function userRequest(text,
|
|
32
|
+
/** Must fit in a signed 32 bit int */
|
|
33
|
+
request_id, extra, hide = false) {
|
|
34
|
+
return {
|
|
35
|
+
text,
|
|
36
|
+
request_users: { request_id, ...extra },
|
|
37
|
+
hide,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function botRequest(text,
|
|
41
|
+
/** Must fit in a signed 32 bit int */
|
|
42
|
+
request_id, extra, hide = false) {
|
|
43
|
+
return {
|
|
44
|
+
text,
|
|
45
|
+
request_users: { request_id, user_is_bot: true, ...extra },
|
|
46
|
+
hide,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function groupRequest(text,
|
|
50
|
+
/** Must fit in a signed 32 bit int */
|
|
51
|
+
request_id, extra, hide = false) {
|
|
52
|
+
return {
|
|
53
|
+
text,
|
|
54
|
+
request_chat: { request_id, chat_is_channel: false, ...extra },
|
|
55
|
+
hide,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function channelRequest(text,
|
|
59
|
+
/** Must fit in a signed 32 bit int */
|
|
60
|
+
request_id, extra, hide = false) {
|
|
61
|
+
return {
|
|
62
|
+
text,
|
|
63
|
+
request_chat: { request_id, chat_is_channel: true, ...extra },
|
|
64
|
+
hide,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function url(text, url, hide = false) {
|
|
68
|
+
return { text, url, hide };
|
|
69
|
+
}
|
|
70
|
+
function callback(text, data, hide = false) {
|
|
71
|
+
return { text, callback_data: data, hide };
|
|
72
|
+
}
|
|
73
|
+
function switchToChat(text, value, hide = false) {
|
|
74
|
+
return { text, switch_inline_query: value, hide };
|
|
75
|
+
}
|
|
76
|
+
function switchToCurrentChat(text, value, hide = false) {
|
|
77
|
+
return { text, switch_inline_query_current_chat: value, hide };
|
|
78
|
+
}
|
|
79
|
+
function game(text, hide = false) {
|
|
80
|
+
return { text, callback_game: {}, hide };
|
|
81
|
+
}
|
|
82
|
+
function pay(text, hide = false) {
|
|
83
|
+
return { text, pay: true, hide };
|
|
84
|
+
}
|
|
85
|
+
function login(text, url, opts = {}, hide = false) {
|
|
86
|
+
return {
|
|
87
|
+
text,
|
|
88
|
+
login_url: { ...opts, url },
|
|
89
|
+
hide,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function webApp(text, url, hide = false
|
|
93
|
+
// works as both InlineKeyboardButton and KeyboardButton
|
|
94
|
+
) {
|
|
95
|
+
return {
|
|
96
|
+
text,
|
|
97
|
+
web_app: { url },
|
|
98
|
+
hide,
|
|
99
|
+
};
|
|
100
|
+
}
|
package/lib/cli.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import d from 'debug';
|
|
3
|
+
import parse from 'mri';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { Telegraf } from './index.js';
|
|
6
|
+
const debug = d('telegraf:cli');
|
|
7
|
+
const helpMsg = `Usage: telegraf [opts] <bot-file>
|
|
8
|
+
|
|
9
|
+
-t Bot token [$BOT_TOKEN]
|
|
10
|
+
-d Webhook domain [$BOT_DOMAIN]
|
|
11
|
+
-H Webhook host [0.0.0.0]
|
|
12
|
+
-p Webhook port [$PORT or 3000]
|
|
13
|
+
-l Enable logs
|
|
14
|
+
-h Show this help message
|
|
15
|
+
-m Bot API method to run directly
|
|
16
|
+
-D Data to pass to the Bot API method`;
|
|
17
|
+
const help = () => console.log(helpMsg);
|
|
18
|
+
/**
|
|
19
|
+
* Runs the cli program and returns exit code
|
|
20
|
+
*/
|
|
21
|
+
export async function main(argv, env = {}) {
|
|
22
|
+
const args = parse(argv, {
|
|
23
|
+
alias: {
|
|
24
|
+
// string params, all optional
|
|
25
|
+
t: 'token',
|
|
26
|
+
d: 'domain',
|
|
27
|
+
m: 'method',
|
|
28
|
+
D: 'data',
|
|
29
|
+
// defaults exist
|
|
30
|
+
H: 'host',
|
|
31
|
+
p: 'port',
|
|
32
|
+
// boolean params
|
|
33
|
+
l: 'logs',
|
|
34
|
+
h: 'help',
|
|
35
|
+
},
|
|
36
|
+
boolean: ['h', 'l'],
|
|
37
|
+
default: {
|
|
38
|
+
H: '0.0.0.0',
|
|
39
|
+
p: env.PORT || '3000',
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
if (args.help) {
|
|
43
|
+
help();
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
const token = args.token || env.BOT_TOKEN;
|
|
47
|
+
const domain = args.domain || env.BOT_DOMAIN;
|
|
48
|
+
if (!token) {
|
|
49
|
+
console.error('Please supply Bot Token');
|
|
50
|
+
help();
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
const bot = new Telegraf(token);
|
|
54
|
+
if (args.method) {
|
|
55
|
+
const method = args.method;
|
|
56
|
+
console.log(await bot.telegram.callApi(method, JSON.parse(args.data || '{}')));
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
let [, , file] = args._;
|
|
60
|
+
if (!file) {
|
|
61
|
+
try {
|
|
62
|
+
const packageJson = (await import(path.resolve(process.cwd(), 'package.json')));
|
|
63
|
+
file = packageJson.main || 'index.js';
|
|
64
|
+
// eslint-disable-next-line no-empty
|
|
65
|
+
}
|
|
66
|
+
catch (err) { }
|
|
67
|
+
}
|
|
68
|
+
if (!file) {
|
|
69
|
+
console.error('Please supply a bot handler file.\n');
|
|
70
|
+
help();
|
|
71
|
+
return 2;
|
|
72
|
+
}
|
|
73
|
+
if (file[0] !== '/')
|
|
74
|
+
file = path.resolve(process.cwd(), file);
|
|
75
|
+
try {
|
|
76
|
+
if (args.logs)
|
|
77
|
+
d.enable('telegraf:*');
|
|
78
|
+
const mod = await import(file);
|
|
79
|
+
const botHandler = mod.botHandler || mod.default;
|
|
80
|
+
const httpHandler = mod.httpHandler;
|
|
81
|
+
const tlsOptions = mod.tlsOptions;
|
|
82
|
+
const config = {};
|
|
83
|
+
if (domain) {
|
|
84
|
+
config.webhook = {
|
|
85
|
+
domain,
|
|
86
|
+
host: args.host,
|
|
87
|
+
port: Number(args.port),
|
|
88
|
+
tlsOptions,
|
|
89
|
+
cb: httpHandler,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
bot.use(botHandler);
|
|
93
|
+
debug(`Starting module ${file}`);
|
|
94
|
+
await bot.launch(config);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
console.error(`Error launching bot from ${file}`, err === null || err === void 0 ? void 0 : err.stack);
|
|
98
|
+
return 3;
|
|
99
|
+
}
|
|
100
|
+
// Enable graceful stop
|
|
101
|
+
process.once('SIGINT', () => bot.stop('SIGINT'));
|
|
102
|
+
process.once('SIGTERM', () => bot.stop('SIGTERM'));
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
process.exitCode = await main(process.argv, process.env);
|