UNPKG

35.6 kBPlain TextView Raw
1import * as tg from './core/types/typegram'
2import * as tt from './telegram-types'
3import ApiClient from './core/network/client'
4import { isAbsolute } from 'path'
5import { URL } from 'url'
6
7export class Telegram extends ApiClient {
8 /**
9 * Get basic information about the bot
10 */
11 getMe() {
12 return this.callApi('getMe', {})
13 }
14
15 /**
16 * Get basic info about a file and prepare it for downloading
17 * @param fileId Id of file to get link to
18 */
19 getFile(fileId: string) {
20 return this.callApi('getFile', { file_id: fileId })
21 }
22
23 /**
24 * Get download link to a file
25 */
26 async getFileLink(fileId: string | tg.File) {
27 if (typeof fileId === 'string') {
28 fileId = await this.getFile(fileId)
29 } else if (fileId.file_path === undefined) {
30 fileId = await this.getFile(fileId.file_id)
31 }
32
33 // Local bot API instances return the absolute path to the file
34 if (fileId.file_path !== undefined && isAbsolute(fileId.file_path)) {
35 const url = new URL(this.options.apiRoot)
36 url.port = ''
37 url.pathname = fileId.file_path
38 url.protocol = 'file:'
39 return url
40 }
41
42 return new URL(
43 `./file/${this.options.apiMode}${this.token}/${fileId.file_path!}`,
44 this.options.apiRoot
45 )
46 }
47
48 /**
49 * Directly request incoming updates.
50 * You should probably use `Telegraf::launch` instead.
51 */
52 getUpdates(
53 timeout: number,
54 limit: number,
55 offset: number,
56 allowedUpdates: readonly tt.UpdateType[] | undefined
57 ) {
58 return this.callApi('getUpdates', {
59 allowed_updates: allowedUpdates,
60 limit,
61 offset,
62 timeout,
63 })
64 }
65
66 getWebhookInfo() {
67 return this.callApi('getWebhookInfo', {})
68 }
69
70 getGameHighScores(
71 userId: number,
72 inlineMessageId: string | undefined,
73 chatId: number | undefined,
74 messageId: number | undefined
75 ) {
76 return this.callApi('getGameHighScores', {
77 user_id: userId,
78 inline_message_id: inlineMessageId,
79 chat_id: chatId,
80 message_id: messageId,
81 })
82 }
83
84 setGameScore(
85 userId: number,
86 score: number,
87 inlineMessageId: string | undefined,
88 chatId: number | undefined,
89 messageId: number | undefined,
90 editMessage = true,
91 force = false
92 ) {
93 return this.callApi('setGameScore', {
94 force,
95 score,
96 user_id: userId,
97 inline_message_id: inlineMessageId,
98 chat_id: chatId,
99 message_id: messageId,
100 disable_edit_message: !editMessage,
101 })
102 }
103
104 /**
105 * Specify a url to receive incoming updates via an outgoing webhook
106 * @param url HTTPS url to send updates to. Use an empty string to remove webhook integration
107 */
108 setWebhook(url: string, extra?: tt.ExtraSetWebhook) {
109 return this.callApi('setWebhook', {
110 url,
111 ...extra,
112 })
113 }
114
115 /**
116 * Remove webhook integration
117 */
118 deleteWebhook(extra?: { drop_pending_updates?: boolean }) {
119 return this.callApi('deleteWebhook', {
120 ...extra,
121 })
122 }
123
124 /**
125 * Send a text message
126 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
127 * @param text Text of the message to be sent
128 */
129 sendMessage(
130 chatId: number | string,
131 text: string,
132 extra?: tt.ExtraReplyMessage
133 ) {
134 return this.callApi('sendMessage', { chat_id: chatId, text, ...extra })
135 }
136
137 /**
138 * Forward existing message.
139 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
140 * @param fromChatId Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
141 * @param messageId Message identifier in the chat specified in from_chat_id
142 */
143 forwardMessage(
144 chatId: number | string,
145 fromChatId: number | string,
146 messageId: number,
147 extra?: { disable_notification?: boolean }
148 ) {
149 return this.callApi('forwardMessage', {
150 chat_id: chatId,
151 from_chat_id: fromChatId,
152 message_id: messageId,
153 ...extra,
154 })
155 }
156
157 /**
158 * Use this method when you need to tell the user that something is happening on the bot's side.
159 * The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
160 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
161 */
162 sendChatAction(chatId: number | string, action: tt.ChatAction) {
163 return this.callApi('sendChatAction', { chat_id: chatId, action })
164 }
165
166 getUserProfilePhotos(userId: number, offset?: number, limit?: number) {
167 return this.callApi('getUserProfilePhotos', {
168 user_id: userId,
169 offset,
170 limit,
171 })
172 }
173
174 /**
175 * Send point on the map
176 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
177 */
178 sendLocation(
179 chatId: number | string,
180 latitude: number,
181 longitude: number,
182 extra?: tt.ExtraLocation
183 ) {
184 return this.callApi('sendLocation', {
185 chat_id: chatId,
186 latitude,
187 longitude,
188 ...extra,
189 })
190 }
191
192 sendVenue(
193 chatId: number | string,
194 latitude: number,
195 longitude: number,
196 title: string,
197 address: string,
198 extra?: tt.ExtraVenue
199 ) {
200 return this.callApi('sendVenue', {
201 latitude,
202 longitude,
203 title,
204 address,
205 chat_id: chatId,
206 ...extra,
207 })
208 }
209
210 /**
211 * @param chatId Unique identifier for the target private chat
212 */
213 sendInvoice(
214 chatId: number | string,
215 invoice: tt.NewInvoiceParameters,
216 extra?: tt.ExtraInvoice
217 ) {
218 return this.callApi('sendInvoice', {
219 chat_id: chatId,
220 ...invoice,
221 ...extra,
222 })
223 }
224
225 sendContact(
226 chatId: number | string,
227 phoneNumber: string,
228 firstName: string,
229 extra?: tt.ExtraContact
230 ) {
231 return this.callApi('sendContact', {
232 chat_id: chatId,
233 phone_number: phoneNumber,
234 first_name: firstName,
235 ...extra,
236 })
237 }
238
239 /**
240 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
241 */
242 sendPhoto(
243 chatId: number | string,
244 photo: tg.Opts<'sendPhoto'>['photo'],
245 extra?: tt.ExtraPhoto
246 ) {
247 return this.callApi('sendPhoto', { chat_id: chatId, photo, ...extra })
248 }
249
250 /**
251 * Send a dice, which will have a random value from 1 to 6.
252 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
253 */
254 sendDice(chatId: number | string, extra?: tt.ExtraDice) {
255 return this.callApi('sendDice', { chat_id: chatId, ...extra })
256 }
257
258 /**
259 * Send general files. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
260 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
261 */
262 sendDocument(
263 chatId: number | string,
264 document: tg.Opts<'sendDocument'>['document'],
265 extra?: tt.ExtraDocument
266 ) {
267 return this.callApi('sendDocument', { chat_id: chatId, document, ...extra })
268 }
269
270 /**
271 * Send audio files, if you want Telegram clients to display them in the music player.
272 * Your audio must be in the .mp3 format.
273 * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
274 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
275 */
276 sendAudio(
277 chatId: number | string,
278 audio: tg.Opts<'sendAudio'>['audio'],
279 extra?: tt.ExtraAudio
280 ) {
281 return this.callApi('sendAudio', { chat_id: chatId, audio, ...extra })
282 }
283
284 /**
285 * Send .webp stickers
286 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
287 */
288 sendSticker(
289 chatId: number | string,
290 sticker: tg.Opts<'sendSticker'>['sticker'],
291 extra?: tt.ExtraSticker
292 ) {
293 return this.callApi('sendSticker', { chat_id: chatId, sticker, ...extra })
294 }
295
296 /**
297 * Send video files, Telegram clients support mp4 videos (other formats may be sent as Document)
298 * Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
299 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
300 */
301 sendVideo(
302 chatId: number | string,
303 video: tg.Opts<'sendVideo'>['video'],
304 extra?: tt.ExtraVideo
305 ) {
306 return this.callApi('sendVideo', { chat_id: chatId, video, ...extra })
307 }
308
309 /**
310 * Send .gif animations
311 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
312 */
313 sendAnimation(
314 chatId: number | string,
315 animation: tg.Opts<'sendAnimation'>['animation'],
316 extra?: tt.ExtraAnimation
317 ) {
318 return this.callApi('sendAnimation', {
319 chat_id: chatId,
320 animation,
321 ...extra,
322 })
323 }
324
325 /**
326 * Send video messages
327 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
328 */
329 sendVideoNote(
330 chatId: number | string,
331 videoNote: string | tg.InputFileVideoNote,
332 extra?: tt.ExtraVideoNote
333 ) {
334 return this.callApi('sendVideoNote', {
335 chat_id: chatId,
336 video_note: videoNote,
337 ...extra,
338 })
339 }
340
341 /**
342 * Send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
343 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
344 */
345 sendVoice(
346 chatId: number | string,
347 voice: tg.Opts<'sendVoice'>['voice'],
348 extra?: tt.ExtraVoice
349 ) {
350 return this.callApi('sendVoice', { chat_id: chatId, voice, ...extra })
351 }
352
353 /**
354 * @param chatId Unique identifier for the target chat
355 * @param gameShortName Short name of the game, serves as the unique identifier for the game. Set up your games via Botfather.
356 */
357 sendGame(chatId: number, gameName: string, extra?: tt.ExtraGame) {
358 return this.callApi('sendGame', {
359 chat_id: chatId,
360 game_short_name: gameName,
361 ...extra,
362 })
363 }
364
365 /**
366 * Send a group of photos or videos as an album
367 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
368 * @param media A JSON-serialized array describing photos and videos to be sent, must include 2–10 items
369 */
370 sendMediaGroup(
371 chatId: number | string,
372 media:
373 | ReadonlyArray<tg.InputMediaPhoto | tg.InputMediaVideo>
374 | readonly tg.InputMediaAudio[]
375 | readonly tg.InputMediaDocument[],
376 extra?: tt.ExtraMediaGroup
377 ) {
378 return this.callApi('sendMediaGroup', { chat_id: chatId, media, ...extra })
379 }
380
381 /**
382 * Send a native poll.
383 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
384 * @param question Poll question, 1-255 characters
385 * @param options A JSON-serialized list of answer options, 2-10 strings 1-100 characters each
386 */
387 sendPoll(
388 chatId: number | string,
389 question: string,
390 options: readonly string[],
391 extra?: tt.ExtraPoll
392 ) {
393 return this.callApi('sendPoll', {
394 chat_id: chatId,
395 type: 'regular',
396 question,
397 options,
398 ...extra,
399 })
400 }
401
402 /**
403 * Send a native quiz.
404 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
405 * @param question Poll question, 1-255 characters
406 * @param options A JSON-serialized list of answer options, 2-10 strings 1-100 characters each
407 */
408 sendQuiz(
409 chatId: number | string,
410 question: string,
411 options: readonly string[],
412 extra: tt.ExtraPoll
413 ) {
414 return this.callApi('sendPoll', {
415 chat_id: chatId,
416 type: 'quiz',
417 question,
418 options,
419 ...extra,
420 })
421 }
422
423 /**
424 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
425 * @param messageId Identifier of the original message with the poll
426 */
427 stopPoll(
428 chatId: number | string,
429 messageId: number,
430 extra?: tt.ExtraStopPoll
431 ) {
432 return this.callApi('stopPoll', {
433 chat_id: chatId,
434 message_id: messageId,
435 ...extra,
436 })
437 }
438
439 /**
440 * Get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.)
441 * @param chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
442 */
443 getChat(chatId: number | string) {
444 return this.callApi('getChat', { chat_id: chatId })
445 }
446
447 /**
448 * @param chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
449 */
450 getChatAdministrators(chatId: number | string) {
451 return this.callApi('getChatAdministrators', { chat_id: chatId })
452 }
453
454 /**
455 * Get information about a member of a chat.
456 * @param chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
457 * @param userId Unique identifier of the target user
458 */
459 getChatMember(chatId: string | number, userId: number) {
460 return this.callApi('getChatMember', { chat_id: chatId, user_id: userId })
461 }
462
463 /**
464 * Get the number of members in a chat
465 * @param chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
466 */
467 getChatMembersCount(chatId: string | number) {
468 return this.callApi('getChatMembersCount', { chat_id: chatId })
469 }
470
471 /**
472 * Send answers to an inline query.
473 * No more than 50 results per query are allowed.
474 */
475 answerInlineQuery(
476 inlineQueryId: string,
477 results: readonly tg.InlineQueryResult[],
478 extra?: tt.ExtraAnswerInlineQuery
479 ) {
480 return this.callApi('answerInlineQuery', {
481 inline_query_id: inlineQueryId,
482 results,
483 ...extra,
484 })
485 }
486
487 setChatPermissions(chatId: number | string, permissions: tg.ChatPermissions) {
488 return this.callApi('setChatPermissions', { chat_id: chatId, permissions })
489 }
490
491 /**
492 * Kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights
493 * @param chatId Unique identifier for the target group or username of the target supergroup or channel (in the format `@channelusername`)
494 * @param untilDate Date when the user will be unbanned, unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever
495 */
496 kickChatMember(
497 chatId: number | string,
498 userId: number,
499 untilDate?: number,
500 extra?: tt.ExtraKickChatMember
501 ) {
502 return this.callApi('kickChatMember', {
503 chat_id: chatId,
504 user_id: userId,
505 until_date: untilDate,
506 ...extra,
507 })
508 }
509
510 /**
511 * Promote or demote a user in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Pass False for all boolean parameters to demote a user.
512 * @param chatId Unique identifier for the target chat or username of the target channel (in the format `@channelusername`)
513 */
514 promoteChatMember(
515 chatId: number | string,
516 userId: number,
517 extra: tt.ExtraPromoteChatMember
518 ) {
519 return this.callApi('promoteChatMember', {
520 chat_id: chatId,
521 user_id: userId,
522 ...extra,
523 })
524 }
525
526 /**
527 * Restrict a user in a supergroup. The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. Pass True for all boolean parameters to lift restrictions from a user.
528 * @param chatId Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
529 */
530 restrictChatMember(
531 chatId: string | number,
532 userId: number,
533 extra: tt.ExtraRestrictChatMember
534 ) {
535 return this.callApi('restrictChatMember', {
536 chat_id: chatId,
537 user_id: userId,
538 ...extra,
539 })
540 }
541
542 setChatAdministratorCustomTitle(
543 chatId: number | string,
544 userId: number,
545 title: string
546 ) {
547 return this.callApi('setChatAdministratorCustomTitle', {
548 chat_id: chatId,
549 user_id: userId,
550 custom_title: title,
551 })
552 }
553
554 /**
555 * Export an invite link to a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
556 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
557 */
558 exportChatInviteLink(chatId: number | string) {
559 return this.callApi('exportChatInviteLink', { chat_id: chatId })
560 }
561
562 createChatInviteLink(
563 chatId: number | string,
564 extra?: tt.ExtraCreateChatInviteLink
565 ) {
566 return this.callApi('createChatInviteLink', {
567 chat_id: chatId,
568 ...extra,
569 })
570 }
571
572 editChatInviteLink(
573 chatId: number | string,
574 inviteLink: string,
575 extra?: tt.ExtraEditChatInviteLink
576 ) {
577 return this.callApi('editChatInviteLink', {
578 chat_id: chatId,
579 invite_link: inviteLink,
580 ...extra,
581 })
582 }
583
584 revokeChatInviteLink(chatId: number | string, inviteLink: string) {
585 return this.callApi('revokeChatInviteLink', {
586 chat_id: chatId,
587 invite_link: inviteLink,
588 })
589 }
590
591 setChatPhoto(
592 chatId: number | string,
593 photo: tg.Opts<'setChatPhoto'>['photo']
594 ) {
595 return this.callApi('setChatPhoto', { chat_id: chatId, photo })
596 }
597
598 deleteChatPhoto(chatId: number | string) {
599 return this.callApi('deleteChatPhoto', { chat_id: chatId })
600 }
601
602 /**
603 * Change the title of a chat. Titles can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights
604 * @param chatId Unique identifier for the target group or username of the target supergroup or channel (in the format `@channelusername`)
605 * @param title New chat title, 1-255 characters
606 */
607 setChatTitle(chatId: number | string, title: string) {
608 return this.callApi('setChatTitle', { chat_id: chatId, title })
609 }
610
611 setChatDescription(chatId: number | string, description?: string) {
612 return this.callApi('setChatDescription', { chat_id: chatId, description })
613 }
614
615 /**
616 * Pin a message in a group, a supergroup, or a channel. The bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' admin right in the supergroup or 'can_edit_messages' admin right in the channel.
617 * @param chatId Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
618 */
619 pinChatMessage(
620 chatId: number | string,
621 messageId: number,
622 extra?: { disable_notification?: boolean }
623 ) {
624 return this.callApi('pinChatMessage', {
625 chat_id: chatId,
626 message_id: messageId,
627 ...extra,
628 })
629 }
630
631 /**
632 * Unpin a message in a group, a supergroup, or a channel. The bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' admin right in the supergroup or 'can_edit_messages' admin right in the channel.
633 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
634 */
635 unpinChatMessage(chatId: number | string, messageId?: number) {
636 return this.callApi('unpinChatMessage', {
637 chat_id: chatId,
638 message_id: messageId,
639 })
640 }
641
642 /**
643 * Clear the list of pinned messages in a chat
644 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
645 */
646 unpinAllChatMessages(chatId: number | string) {
647 return this.callApi('unpinAllChatMessages', { chat_id: chatId })
648 }
649
650 /**
651 * Use this method for your bot to leave a group, supergroup or channel
652 * @param chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
653 */
654 leaveChat(chatId: number | string) {
655 return this.callApi('leaveChat', { chat_id: chatId })
656 }
657
658 /**
659 * Unban a user from a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights
660 * @param chatId Unique identifier for the target group or username of the target supergroup or channel (in the format @username)
661 * @param userId Unique identifier of the target user
662 */
663 unbanChatMember(
664 chatId: number | string,
665 userId: number,
666 extra?: { only_if_banned?: boolean }
667 ) {
668 return this.callApi('unbanChatMember', {
669 chat_id: chatId,
670 user_id: userId,
671 ...extra,
672 })
673 }
674
675 answerCbQuery(
676 callbackQueryId: string,
677 text?: string,
678 extra?: tt.ExtraAnswerCbQuery
679 ) {
680 return this.callApi('answerCallbackQuery', {
681 text,
682 callback_query_id: callbackQueryId,
683 ...extra,
684 })
685 }
686
687 answerGameQuery(callbackQueryId: string, url: string) {
688 return this.callApi('answerCallbackQuery', {
689 url,
690 callback_query_id: callbackQueryId,
691 })
692 }
693
694 /**
695 * If you sent an invoice requesting a shipping address and the parameter is_flexible was specified,
696 * the Bot API will send an Update with a shipping_query field to the bot.
697 * Reply to shipping queries.
698 * @param ok Specify True if delivery to the specified address is possible and False if there are any problems (for example, if delivery to the specified address is not possible)
699 * @param shippingOptions Required if ok is True. A JSON-serialized array of available shipping options.
700 * @param errorMessage Required if ok is False. Error message in human readable form that explains why it is impossible to complete the order (e.g. "Sorry, delivery to your desired address is unavailable'). Telegram will display this message to the user.
701 */
702 answerShippingQuery(
703 shippingQueryId: string,
704 ok: boolean,
705 shippingOptions: readonly tg.ShippingOption[] | undefined,
706 errorMessage: string | undefined
707 ) {
708 return this.callApi('answerShippingQuery', {
709 ok,
710 shipping_query_id: shippingQueryId,
711 shipping_options: shippingOptions,
712 error_message: errorMessage,
713 })
714 }
715
716 /**
717 * Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query.
718 * Respond to such pre-checkout queries. On success, True is returned.
719 * Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
720 * @param ok Specify True if everything is alright (goods are available, etc.) and the bot is ready to proceed with the order. Use False if there are any problems.
721 * @param errorMessage Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!"). Telegram will display this message to the user.
722 */
723 answerPreCheckoutQuery(
724 preCheckoutQueryId: string,
725 ok: boolean,
726 errorMessage?: string
727 ) {
728 return this.callApi('answerPreCheckoutQuery', {
729 ok,
730 pre_checkout_query_id: preCheckoutQueryId,
731 error_message: errorMessage,
732 })
733 }
734
735 /**
736 * Edit text and game messages sent by the bot or via the bot (for inline bots).
737 * On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
738 * @param chatId Required if inlineMessageId is not specified. Unique identifier for the target chat or username of the target channel (in the format @channelusername)
739 * @param messageId Required if inlineMessageId is not specified. Identifier of the sent message
740 * @param inlineMessageId Required if chatId and messageId are not specified. Identifier of the inline message
741 * @param text New text of the message
742 */
743 editMessageText(
744 chatId: number | string | undefined,
745 messageId: number | undefined,
746 inlineMessageId: string | undefined,
747 text: string,
748 extra?: tt.ExtraEditMessageText
749 ) {
750 return this.callApi('editMessageText', {
751 text,
752 chat_id: chatId,
753 message_id: messageId,
754 inline_message_id: inlineMessageId,
755 ...extra,
756 })
757 }
758
759 /**
760 * Edit captions of messages sent by the bot or via the bot (for inline bots).
761 * On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
762 * @param chatId Required if inlineMessageId is not specified. Unique identifier for the target chat or username of the target channel (in the format @channelusername)
763 * @param messageId Required if inlineMessageId is not specified. Identifier of the sent message
764 * @param inlineMessageId Required if chatId and messageId are not specified. Identifier of the inline message
765 * @param caption New caption of the message
766 * @param markup A JSON-serialized object for an inline keyboard.
767 */
768 editMessageCaption(
769 chatId: number | string | undefined,
770 messageId: number | undefined,
771 inlineMessageId: string | undefined,
772 caption: string | undefined,
773 extra?: tt.ExtraEditMessageCaption
774 ) {
775 return this.callApi('editMessageCaption', {
776 caption,
777 chat_id: chatId,
778 message_id: messageId,
779 inline_message_id: inlineMessageId,
780 ...extra,
781 })
782 }
783
784 /**
785 * Edit animation, audio, document, photo, or video messages.
786 * If a message is a part of a message album, then it can be edited only to a photo or a video.
787 * Otherwise, message type can be changed arbitrarily.
788 * When inline message is edited, new file can't be uploaded.
789 * Use previously uploaded file via its file_id or specify a URL.
790 * @param chatId Required if inlineMessageId is not specified. Unique identifier for the target chat or username of the target channel (in the format @channelusername)
791 * @param messageId Required if inlineMessageId is not specified. Identifier of the sent message
792 * @param inlineMessageId Required if chatId and messageId are not specified. Identifier of the inline message
793 * @param media New media of message
794 * @param markup Markup of inline keyboard
795 */
796 editMessageMedia(
797 chatId: number | string | undefined,
798 messageId: number | undefined,
799 inlineMessageId: string | undefined,
800 media: tg.InputMedia,
801 extra?: tt.ExtraEditMessageMedia
802 ) {
803 return this.callApi('editMessageMedia', {
804 chat_id: chatId,
805 message_id: messageId,
806 inline_message_id: inlineMessageId,
807 media,
808 ...extra,
809 })
810 }
811
812 /**
813 * Edit only the reply markup of messages sent by the bot or via the bot (for inline bots).
814 * @param chatId Required if inlineMessageId is not specified. Unique identifier for the target chat or username of the target channel (in the format @channelusername)
815 * @param messageId Required if inlineMessageId is not specified. Identifier of the sent message
816 * @param inlineMessageId Required if chatId and messageId are not specified. Identifier of the inline message
817 * @param markup A JSON-serialized object for an inline keyboard.
818 * @returns If edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
819 */
820 editMessageReplyMarkup(
821 chatId: number | string | undefined,
822 messageId: number | undefined,
823 inlineMessageId: string | undefined,
824 markup: tg.InlineKeyboardMarkup | undefined
825 ) {
826 return this.callApi('editMessageReplyMarkup', {
827 chat_id: chatId,
828 message_id: messageId,
829 inline_message_id: inlineMessageId,
830 reply_markup: markup,
831 })
832 }
833
834 editMessageLiveLocation(
835 chatId: number | string | undefined,
836 messageId: number | undefined,
837 inlineMessageId: string | undefined,
838 latitude: number,
839 longitude: number,
840 extra?: tt.ExtraEditMessageLiveLocation
841 ) {
842 return this.callApi('editMessageLiveLocation', {
843 latitude,
844 longitude,
845 chat_id: chatId,
846 message_id: messageId,
847 inline_message_id: inlineMessageId,
848 ...extra,
849 })
850 }
851
852 stopMessageLiveLocation(
853 chatId: number | string | undefined,
854 messageId: number | undefined,
855 inlineMessageId: string | undefined,
856 markup?: tg.InlineKeyboardMarkup
857 ) {
858 return this.callApi('stopMessageLiveLocation', {
859 chat_id: chatId,
860 message_id: messageId,
861 inline_message_id: inlineMessageId,
862 reply_markup: markup,
863 })
864 }
865
866 /**
867 * Delete a message, including service messages, with the following limitations:
868 * - A message can only be deleted if it was sent less than 48 hours ago.
869 * - Bots can delete outgoing messages in groups and supergroups.
870 * - Bots granted can_post_messages permissions can delete outgoing messages in channels.
871 * - If the bot is an administrator of a group, it can delete any message there.
872 * - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
873 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
874 */
875 deleteMessage(chatId: number | string, messageId: number) {
876 return this.callApi('deleteMessage', {
877 chat_id: chatId,
878 message_id: messageId,
879 })
880 }
881
882 setChatStickerSet(chatId: number | string, setName: string) {
883 return this.callApi('setChatStickerSet', {
884 chat_id: chatId,
885 sticker_set_name: setName,
886 })
887 }
888
889 deleteChatStickerSet(chatId: number | string) {
890 return this.callApi('deleteChatStickerSet', { chat_id: chatId })
891 }
892
893 getStickerSet(name: string) {
894 return this.callApi('getStickerSet', { name })
895 }
896
897 /**
898 * Upload a .png file with a sticker for later use in createNewStickerSet and addStickerToSet methods (can be used multiple times)
899 * https://core.telegram.org/bots/api#sending-files
900 * @param ownerId User identifier of sticker file owner
901 * @param stickerFile Png image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px.
902 */
903 uploadStickerFile(
904 ownerId: number,
905 stickerFile: tg.Opts<'uploadStickerFile'>['png_sticker']
906 ) {
907 return this.callApi('uploadStickerFile', {
908 user_id: ownerId,
909 png_sticker: stickerFile,
910 })
911 }
912
913 /**
914 * Create new sticker set owned by a user. The bot will be able to edit the created sticker set
915 * @param ownerId User identifier of created sticker set owner
916 * @param name Short name of sticker set, to be used in t.me/addstickers/ URLs (e.g., animals). Can contain only english letters, digits and underscores. Must begin with a letter, can't contain consecutive underscores and must end in “_by_<bot username>”. <bot_username> is case insensitive. 1-64 characters.
917 * @param title Sticker set title, 1-64 characters
918 */
919 createNewStickerSet(
920 ownerId: number,
921 name: string,
922 title: string,
923 stickerData: tt.ExtraCreateNewStickerSet
924 ) {
925 return this.callApi('createNewStickerSet', {
926 name,
927 title,
928 user_id: ownerId,
929 ...stickerData,
930 })
931 }
932
933 /**
934 * Add a new sticker to a set created by the bot
935 * @param ownerId User identifier of sticker set owner
936 * @param name Sticker set name
937 */
938 addStickerToSet(
939 ownerId: number,
940 name: string,
941 stickerData: tt.ExtraAddStickerToSet
942 ) {
943 return this.callApi('addStickerToSet', {
944 name,
945 user_id: ownerId,
946 ...stickerData,
947 })
948 }
949
950 /**
951 * Move a sticker in a set created by the bot to a specific position
952 * @param sticker File identifier of the sticker
953 * @param position New sticker position in the set, zero-based
954 */
955 setStickerPositionInSet(sticker: string, position: number) {
956 return this.callApi('setStickerPositionInSet', {
957 sticker,
958 position,
959 })
960 }
961
962 setStickerSetThumb(
963 name: string,
964 userId: number,
965 thumb: tg.Opts<'setStickerSetThumb'>['thumb']
966 ) {
967 return this.callApi('setStickerSetThumb', { name, user_id: userId, thumb })
968 }
969
970 /**
971 * Delete a sticker from a set created by the bot.
972 * @param sticker File identifier of the sticker
973 */
974 deleteStickerFromSet(sticker: string) {
975 return this.callApi('deleteStickerFromSet', { sticker })
976 }
977
978 /**
979 * Get the current list of the bot's commands.
980 */
981 getMyCommands(extra: tg.Opts<'getMyCommands'> = {}) {
982 return this.callApi('getMyCommands', extra)
983 }
984
985 /**
986 * Change the list of the bot's commands.
987 * @param commands A list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified.
988 */
989 setMyCommands(
990 commands: readonly tg.BotCommand[],
991 extra?: tt.ExtraSetMyCommands
992 ) {
993 return this.callApi('setMyCommands', { commands, ...extra })
994 }
995
996 deleteMyCommands(extra: tg.Opts<'deleteMyCommands'> = {}) {
997 return this.callApi('deleteMyCommands', extra)
998 }
999
1000 setPassportDataErrors(
1001 userId: number,
1002 errors: readonly tg.PassportElementError[]
1003 ) {
1004 return this.callApi('setPassportDataErrors', {
1005 user_id: userId,
1006 errors: errors,
1007 })
1008 }
1009
1010 /**
1011 * Send copy of existing message.
1012 * @deprecated use `copyMessage` instead
1013 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1014 * @param message Received message object
1015 */
1016 sendCopy(
1017 chatId: number | string,
1018 message: tg.Message,
1019 extra?: tt.ExtraCopyMessage
1020 ): Promise<tg.MessageId> {
1021 return this.copyMessage(chatId, message.chat.id, message.message_id, extra)
1022 }
1023
1024 /**
1025 * Send copy of existing message
1026 * @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
1027 * @param fromChatId Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
1028 * @param messageId Message identifier in the chat specified in from_chat_id
1029 */
1030 copyMessage(
1031 chatId: number | string,
1032 fromChatId: number | string,
1033 messageId: number,
1034 extra?: tt.ExtraCopyMessage
1035 ) {
1036 return this.callApi('copyMessage', {
1037 chat_id: chatId,
1038 from_chat_id: fromChatId,
1039 message_id: messageId,
1040 ...extra,
1041 })
1042 }
1043
1044 /**
1045 * Log out from the cloud Bot API server before launching the bot locally
1046 */
1047 logOut() {
1048 return this.callApi('logOut', {})
1049 }
1050
1051 /**
1052 * Close the bot instance before moving it from one local server to another
1053 */
1054 close() {
1055 return this.callApi('close', {})
1056 }
1057}
1058
1059export default Telegram