1 | import Context from './context'
|
2 | import { Middleware } from './middleware'
|
3 |
|
4 | type ReplyContext = { [key in keyof Context & `reply${string}`]: Context[key] }
|
5 |
|
6 | function makeReply<
|
7 | C extends Context,
|
8 | E extends { reply_to_message_id?: number },
|
9 | >(ctx: C, extra?: E) {
|
10 | const reply_to_message_id = ctx.message?.message_id
|
11 | return { reply_to_message_id, ...extra }
|
12 | }
|
13 |
|
14 | const replyContext: ReplyContext = {
|
15 | replyWithChatAction: function () {
|
16 | throw new TypeError(
|
17 | 'ctx.replyWithChatAction has been removed, use ctx.sendChatAction instead'
|
18 | )
|
19 | },
|
20 | reply(this: Context, text, extra) {
|
21 | this.assert(this.chat, 'reply')
|
22 | return this.telegram.sendMessage(this.chat.id, text, makeReply(this, extra))
|
23 | },
|
24 | replyWithAnimation(this: Context, animation, extra) {
|
25 | this.assert(this.chat, 'replyWithAnimation')
|
26 | return this.telegram.sendAnimation(
|
27 | this.chat.id,
|
28 | animation,
|
29 | makeReply(this, extra)
|
30 | )
|
31 | },
|
32 | replyWithAudio(this: Context, audio, extra) {
|
33 | this.assert(this.chat, 'replyWithAudio')
|
34 | return this.telegram.sendAudio(this.chat.id, audio, makeReply(this, extra))
|
35 | },
|
36 | replyWithContact(this: Context, phoneNumber, firstName, extra) {
|
37 | this.assert(this.chat, 'replyWithContact')
|
38 | return this.telegram.sendContact(
|
39 | this.chat.id,
|
40 | phoneNumber,
|
41 | firstName,
|
42 | makeReply(this, extra)
|
43 | )
|
44 | },
|
45 | replyWithDice(this: Context, extra) {
|
46 | this.assert(this.chat, 'replyWithDice')
|
47 | return this.telegram.sendDice(this.chat.id, makeReply(this, extra))
|
48 | },
|
49 | replyWithDocument(this: Context, document, extra) {
|
50 | this.assert(this.chat, 'replyWithDocument')
|
51 | return this.telegram.sendDocument(
|
52 | this.chat.id,
|
53 | document,
|
54 | makeReply(this, extra)
|
55 | )
|
56 | },
|
57 | replyWithGame(this: Context, gameName, extra) {
|
58 | this.assert(this.chat, 'replyWithGame')
|
59 | return this.telegram.sendGame(
|
60 | this.chat.id,
|
61 | gameName,
|
62 | makeReply(this, extra)
|
63 | )
|
64 | },
|
65 | replyWithHTML(this: Context, html, extra) {
|
66 | this.assert(this.chat, 'replyWithHTML')
|
67 | return this.telegram.sendMessage(this.chat.id, html, {
|
68 | parse_mode: 'HTML',
|
69 | reply_to_message_id: this.message?.message_id,
|
70 | ...extra,
|
71 | })
|
72 | },
|
73 | replyWithInvoice(this: Context, invoice, extra) {
|
74 | this.assert(this.chat, 'replyWithInvoice')
|
75 | return this.telegram.sendInvoice(
|
76 | this.chat.id,
|
77 | invoice,
|
78 | makeReply(this, extra)
|
79 | )
|
80 | },
|
81 | replyWithLocation(this: Context, latitude, longitude, extra) {
|
82 | this.assert(this.chat, 'replyWithLocation')
|
83 | return this.telegram.sendLocation(
|
84 | this.chat.id,
|
85 | latitude,
|
86 | longitude,
|
87 | makeReply(this, extra)
|
88 | )
|
89 | },
|
90 | replyWithMarkdown(this: Context, markdown, extra) {
|
91 | this.assert(this.chat, 'replyWithMarkdown')
|
92 | return this.telegram.sendMessage(this.chat.id, markdown, {
|
93 | parse_mode: 'Markdown',
|
94 | reply_to_message_id: this.message?.message_id,
|
95 | ...extra,
|
96 | })
|
97 | },
|
98 | replyWithMarkdownV2(this: Context, markdown, extra) {
|
99 | this.assert(this.chat, 'replyWithMarkdownV2')
|
100 | return this.telegram.sendMessage(this.chat.id, markdown, {
|
101 | parse_mode: 'MarkdownV2',
|
102 | reply_to_message_id: this.message?.message_id,
|
103 | ...extra,
|
104 | })
|
105 | },
|
106 | replyWithMediaGroup(this: Context, media, extra) {
|
107 | this.assert(this.chat, 'replyWithMediaGroup')
|
108 | return this.telegram.sendMediaGroup(
|
109 | this.chat.id,
|
110 | media,
|
111 | makeReply(this, extra)
|
112 | )
|
113 | },
|
114 | replyWithPhoto(this: Context, photo, extra) {
|
115 | this.assert(this.chat, 'replyWithPhoto')
|
116 | return this.telegram.sendPhoto(this.chat.id, photo, makeReply(this, extra))
|
117 | },
|
118 | replyWithPoll(this: Context, question, options, extra) {
|
119 | this.assert(this.chat, 'replyWithPoll')
|
120 | return this.telegram.sendPoll(
|
121 | this.chat.id,
|
122 | question,
|
123 | options,
|
124 | makeReply(this, extra)
|
125 | )
|
126 | },
|
127 | replyWithQuiz(this: Context, question, options, extra) {
|
128 | this.assert(this.chat, 'replyWithQuiz')
|
129 | return this.telegram.sendQuiz(
|
130 | this.chat.id,
|
131 | question,
|
132 | options,
|
133 | makeReply(this, extra)
|
134 | )
|
135 | },
|
136 | replyWithSticker(this: Context, sticker, extra) {
|
137 | this.assert(this.chat, 'replyWithSticker')
|
138 | return this.telegram.sendSticker(
|
139 | this.chat.id,
|
140 | sticker,
|
141 | makeReply(this, extra)
|
142 | )
|
143 | },
|
144 | replyWithVenue(this: Context, latitude, longitude, title, address, extra) {
|
145 | this.assert(this.chat, 'replyWithVenue')
|
146 | return this.telegram.sendVenue(
|
147 | this.chat.id,
|
148 | latitude,
|
149 | longitude,
|
150 | title,
|
151 | address,
|
152 | makeReply(this, extra)
|
153 | )
|
154 | },
|
155 | replyWithVideo(this: Context, video, extra) {
|
156 | this.assert(this.chat, 'replyWithVideo')
|
157 | return this.telegram.sendVideo(this.chat.id, video, makeReply(this, extra))
|
158 | },
|
159 | replyWithVideoNote(this: Context, videoNote, extra) {
|
160 | this.assert(this.chat, 'replyWithVideoNote')
|
161 | return this.telegram.sendVideoNote(
|
162 | this.chat.id,
|
163 | videoNote,
|
164 | makeReply(this, extra)
|
165 | )
|
166 | },
|
167 | replyWithVoice(this: Context, voice, extra) {
|
168 | this.assert(this.chat, 'replyWithVoice')
|
169 | return this.telegram.sendVoice(this.chat.id, voice, makeReply(this, extra))
|
170 | },
|
171 | }
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export function useNewReplies<C extends Context>(): Middleware<C> {
|
182 | return (ctx, next) => {
|
183 | ctx.reply = replyContext.reply
|
184 | ctx.replyWithPhoto = replyContext.replyWithPhoto
|
185 | ctx.replyWithMediaGroup = replyContext.replyWithMediaGroup
|
186 | ctx.replyWithAudio = replyContext.replyWithAudio
|
187 | ctx.replyWithDice = replyContext.replyWithDice
|
188 | ctx.replyWithDocument = replyContext.replyWithDocument
|
189 | ctx.replyWithSticker = replyContext.replyWithSticker
|
190 | ctx.replyWithVideo = replyContext.replyWithVideo
|
191 | ctx.replyWithAnimation = replyContext.replyWithAnimation
|
192 | ctx.replyWithVideoNote = replyContext.replyWithVideoNote
|
193 | ctx.replyWithInvoice = replyContext.replyWithInvoice
|
194 | ctx.replyWithGame = replyContext.replyWithGame
|
195 | ctx.replyWithVoice = replyContext.replyWithVoice
|
196 | ctx.replyWithPoll = replyContext.replyWithPoll
|
197 | ctx.replyWithQuiz = replyContext.replyWithQuiz
|
198 | ctx.replyWithChatAction = replyContext.replyWithChatAction
|
199 | ctx.replyWithLocation = replyContext.replyWithLocation
|
200 | ctx.replyWithVenue = replyContext.replyWithVenue
|
201 | ctx.replyWithContact = replyContext.replyWithContact
|
202 | ctx.replyWithMarkdown = replyContext.replyWithMarkdown
|
203 | ctx.replyWithMarkdownV2 = replyContext.replyWithMarkdownV2
|
204 | ctx.replyWithHTML = replyContext.replyWithHTML
|
205 | return next()
|
206 | }
|
207 | }
|