refactor(client): made builder methods modify existing object instead of spreading, and also improved docs

This commit is contained in:
teidesu 2021-05-16 13:44:59 +03:00
parent 803daecdf3
commit d75071c284
4 changed files with 324 additions and 195 deletions

View file

@ -5,7 +5,6 @@ import {
InputMediaGeo, InputMediaGeo,
InputMediaGeoLive, InputMediaGeoLive,
InputMediaVenue, InputMediaVenue,
Venue,
} from '../../media' } from '../../media'
/** /**
@ -111,73 +110,99 @@ export type InputInlineMessage =
| InputInlineMessageGame | InputInlineMessageGame
export namespace BotInlineMessage { export namespace BotInlineMessage {
/**
* Create a text inline message
*
* @param text Message text
* @param params
*/
export function text( export function text(
text: string, text: string,
params?: Omit<InputInlineMessageText, 'type' | 'text'> params: Omit<InputInlineMessageText, 'type' | 'text'> = {}
): InputInlineMessageText { ): InputInlineMessageText {
return { const ret = params as tl.Mutable<InputInlineMessageText>
type: 'text', ret.type = 'text'
text, ret.text = text
...(params || {}), return ret
}
} }
/**
* Create an inline message containing
* media from the result
*/
export function media( export function media(
params?: Omit<InputInlineMessageMedia, 'type'> params: Omit<InputInlineMessageMedia, 'type'> = {}
): InputInlineMessageMedia { ): InputInlineMessageMedia {
return { const ret = params as tl.Mutable<InputInlineMessageMedia>
type: 'media', ret.type = 'media'
...(params || {}), return ret
}
} }
/**
* Create an inline message containing a geolocation
*
* @param latitude Latitude of the location
* @param longitude Longitude of the location
* @param params Additional parameters
*/
export function geo( export function geo(
latitude: number, latitude: number,
longitude: number, longitude: number,
params?: Omit<InputInlineMessageGeo, 'type' | 'latitude' | 'longitude'> params: Omit<InputInlineMessageGeo, 'type' | 'latitude' | 'longitude'> = {}
): InputInlineMessageGeo { ): InputInlineMessageGeo {
return { const ret = params as tl.Mutable<InputInlineMessageGeo>
type: 'geo', ret.type = 'geo'
latitude, ret.latitude = latitude
longitude, ret.longitude = longitude
...(params || {}), return ret
}
} }
/**
* Create an inline message containing a live geolocation
*
* @param latitude Latitude of the current location
* @param longitude Longitude of the current location
* @param params Additional parameters
*/
export function geoLive( export function geoLive(
latitude: number, latitude: number,
longitude: number, longitude: number,
params?: Omit< params: Omit<
InputInlineMessageGeoLive, InputInlineMessageGeoLive,
'type' | 'latitude' | 'longitude' 'type' | 'latitude' | 'longitude'
> > = {}
): InputInlineMessageGeoLive { ): InputInlineMessageGeoLive {
return { const ret = params as tl.Mutable<InputInlineMessageGeoLive>
type: 'geo_live', ret.type = 'geo_live'
latitude, ret.latitude = latitude
longitude, ret.longitude = longitude
...(params || {}), return ret
}
} }
/**
* Create an inline message containing a venue
*/
export function venue( export function venue(
params: Omit<InputInlineMessageVenue, 'type'> params: Omit<InputInlineMessageVenue, 'type'>
): InputInlineMessageVenue { ): InputInlineMessageVenue {
return { const ret = params as tl.Mutable<InputInlineMessageVenue>
type: 'venue', ret.type = 'venue'
...params, return ret
}
} }
/**
* Create an inline message containing a game
* from the inline result
*/
export function game( export function game(
params: Omit<InputInlineMessageGame, 'type'> params: Omit<InputInlineMessageGame, 'type'>
): InputInlineMessageGame { ): InputInlineMessageGame {
return { const ret = params as tl.Mutable<InputInlineMessageGame>
type: 'game', ret.type = 'game'
...params, return ret
}
} }
/** @internal */
export async function _convertToTl( export async function _convertToTl(
client: TelegramClient, client: TelegramClient,
obj: InputInlineMessage, obj: InputInlineMessage,

View file

@ -1,5 +1,5 @@
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { BotInlineMessage, InputInlineMessage } from './input-inline-message' import { BotInlineMessage, InputInlineMessage, InputInlineMessageGame } from './input-inline-message'
import { TelegramClient } from '../../../client' import { TelegramClient } from '../../../client'
import { fileIdToInputDocument, fileIdToInputPhoto } from '@mtcute/file-id' import { fileIdToInputDocument, fileIdToInputPhoto } from '@mtcute/file-id'
import { extractFileName } from '../../../utils/file-utils' import { extractFileName } from '../../../utils/file-utils'
@ -480,82 +480,123 @@ export type InputInlineResult =
| InputInlineResultContact | InputInlineResultContact
export namespace BotInline { export namespace BotInline {
/**
* Create an inline result containing an article
*
* @param id Inline result ID
* @param params Article
*/
export function article( export function article(
id: string, id: string,
params: Omit<InputInlineResultArticle, 'type' | 'id'> params: Omit<InputInlineResultArticle, 'type' | 'id'>
): InputInlineResultArticle { ): InputInlineResultArticle {
return { const ret = params as tl.Mutable<InputInlineResultArticle>
id, ret.id = id
type: 'article', ret.type = 'article'
...params, return ret
}
} }
/**
* Create an inline result containing a GIF
*
* @param id Inline result ID
* @param media GIF animation
* @param params Additional parameters
*/
export function gif( export function gif(
id: string, id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument, media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultGif, 'type' | 'id' | 'media'> params: Omit<InputInlineResultGif, 'type' | 'id' | 'media'> = {}
): InputInlineResultGif { ): InputInlineResultGif {
return { const ret = params as tl.Mutable<InputInlineResultGif>
id, ret.id = id
media, ret.type = 'gif'
type: 'gif', ret.media = media
...params, return ret
}
} }
/**
* Create an inline result containing a video
*
* @param id Inline result ID
* @param media Video
* @param params Additional parameters
*/
export function video( export function video(
id: string, id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument, media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultVideo, 'type' | 'id' | 'media'> params: Omit<InputInlineResultVideo, 'type' | 'id' | 'media'>
): InputInlineResultVideo { ): InputInlineResultVideo {
return { const ret = params as tl.Mutable<InputInlineResultVideo>
id, ret.id = id
type: 'video', ret.type = 'video'
media, ret.media = media
...params, return ret
}
} }
/**
* Create an inline result containing an audio file
*
* @param id Inline result ID
* @param media Audio file
* @param params Additional parameters
*/
export function audio( export function audio(
id: string, id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument, media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultAudio, 'type' | 'id' | 'media'> params: Omit<InputInlineResultAudio, 'type' | 'id' | 'media'>
): InputInlineResultAudio { ): InputInlineResultAudio {
return { const ret = params as tl.Mutable<InputInlineResultAudio>
id, ret.id = id
type: 'audio', ret.type = 'audio'
media, ret.media = media
...params, return ret
}
} }
/**
* Create an inline result containing a voice note
*
* @param id Inline result ID
* @param media Voice note
* @param params Additional parameters
*/
export function voice( export function voice(
id: string, id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument, media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultVoice, 'type' | 'id' | 'media'> params: Omit<InputInlineResultVoice, 'type' | 'id' | 'media'>
): InputInlineResultVoice { ): InputInlineResultVoice {
return { const ret = params as tl.Mutable<InputInlineResultVoice>
id, ret.id = id
type: 'voice', ret.type = 'voice'
media, ret.media = media
...params, return ret
}
} }
/**
* Create an inline result containing a photo
*
* @param id Inline result ID
* @param media Photo
* @param params Additional parameters
*/
export function photo( export function photo(
id: string, id: string,
media: string | tl.RawInputWebDocument | tl.RawInputPhoto, media: string | tl.RawInputWebDocument | tl.RawInputPhoto,
params?: Omit<InputInlineResultPhoto, 'type' | 'id' | 'media'> params: Omit<InputInlineResultPhoto, 'type' | 'id' | 'media'> = {}
): InputInlineResultPhoto { ): InputInlineResultPhoto {
return { const ret = params as tl.Mutable<InputInlineResultPhoto>
id, ret.id = id
type: 'photo', ret.type = 'photo'
media, ret.media = media
...(params || {}), return ret
}
} }
/**
* Create an inline result containing a sticker
*
* @param id Inline result ID
* @param media Sticker
*/
export function sticker( export function sticker(
id: string, id: string,
media: string | tl.RawInputDocument media: string | tl.RawInputDocument
@ -567,67 +608,100 @@ export namespace BotInline {
} }
} }
/**
* Create an inline result containing a document
* (only PDF and ZIP are supported)
*
* @param id Inline result ID
* @param media Document
* @param params Additional parameters
*/
export function file( export function file(
id: string, id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument, media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultFile, 'type' | 'id' | 'media'> params: Omit<InputInlineResultFile, 'type' | 'id' | 'media'>
): InputInlineResultFile { ): InputInlineResultFile {
return { const ret = params as tl.Mutable<InputInlineResultFile>
id, ret.id = id
type: 'file', ret.type = 'file'
media, ret.media = media
...params, return ret
}
} }
/**
* Create an inline result containing a geolocation
*
* @param id Inline result ID
* @param latitude Latitude of the location
* @param longitude Longitude of the location
* @param params Additional parameters
*/
export function geo( export function geo(
id: string,
latitude: number, latitude: number,
longitude: number, longitude: number,
params: Omit<InputInlineResultGeo, 'type' | 'latitude' | 'longitude'> params: Omit<InputInlineResultGeo, 'type' | 'latitude' | 'longitude'>
): InputInlineResultGeo { ): InputInlineResultGeo {
return { const ret = params as tl.Mutable<InputInlineResultGeo>
type: 'geo', ret.id = id
latitude, ret.type = 'geo'
longitude, ret.latitude = latitude
...params, ret.longitude = longitude
} return ret
} }
/**
* Create an inline result containing a venue
*
* @param id Inline result ID
* @param params Venue parameters
*/
export function venue( export function venue(
id: string, id: string,
params: Omit<InputInlineResultVenue, 'type' | 'id'> params: Omit<InputInlineResultVenue, 'type' | 'id'>
): InputInlineResultVenue { ): InputInlineResultVenue {
return { const ret = params as tl.Mutable<InputInlineResultVenue>
id, ret.id = id
type: 'venue', ret.type = 'venue'
...params, return ret
}
} }
/**
* Create an inline result containing a contact
*
* @param id Inline result ID
* @param params Contact parameters
*/
export function contact( export function contact(
id: string, id: string,
params: Omit<InputInlineResultContact, 'type' | 'id'> params: Omit<InputInlineResultContact, 'type' | 'id'>
): InputInlineResultContact { ): InputInlineResultContact {
return { const ret = params as tl.Mutable<InputInlineResultContact>
id, ret.id = id
type: 'contact', ret.type = 'contact'
...params, return ret
}
} }
/**
* Create an inline result containing a game
*
* @param id Inline result ID
* @param shortName Short name of the game
* @param params Additional parameters
*/
export function game( export function game(
id: string, id: string,
shortName: string, shortName: string,
params?: Omit<InputInlineResultGame, 'type' | 'id' | 'shortName'> params: Omit<InputInlineResultGame, 'type' | 'id' | 'shortName'> = {}
): InputInlineResultGame { ): InputInlineResultGame {
return { const ret = params as tl.Mutable<InputInlineResultGame>
id, ret.id = id
type: 'game', ret.type = 'game'
shortName, ret.shortName = shortName
...(params || {}) return ret
}
} }
/** @internal */
export async function _convertToTl( export async function _convertToTl(
client: TelegramClient, client: TelegramClient,
obj: InputInlineResult, obj: InputInlineResult,

View file

@ -81,11 +81,10 @@ export namespace BotKeyboard {
buttons: tl.TypeKeyboardButton[][], buttons: tl.TypeKeyboardButton[][],
params: Omit<ReplyKeyboardMarkup, 'type' | 'buttons'> = {} params: Omit<ReplyKeyboardMarkup, 'type' | 'buttons'> = {}
): ReplyKeyboardMarkup { ): ReplyKeyboardMarkup {
return { const ret = params as tl.Mutable<ReplyKeyboardMarkup>
type: 'reply', ret.type = 'reply'
buttons, ret.buttons = buttons
...params, return ret
}
} }
/** /**
@ -109,10 +108,9 @@ export namespace BotKeyboard {
export function forceReply( export function forceReply(
params: Omit<ReplyKeyboardForceReply, 'type'> = {} params: Omit<ReplyKeyboardForceReply, 'type'> = {}
): ReplyKeyboardForceReply { ): ReplyKeyboardForceReply {
return { const ret = params as tl.Mutable<ReplyKeyboardForceReply>
type: 'force_reply', ret.type = 'force_reply'
...params, return ret
}
} }
/** /**

View file

@ -2,6 +2,7 @@ import { InputFileLike } from '../files'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { Venue } from './venue' import { Venue } from './venue'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { InputInlineResultGame } from '../bots'
interface BaseInputMedia { interface BaseInputMedia {
/** /**
@ -574,145 +575,166 @@ export namespace InputMedia {
/** /**
* Create an animation to be sent * Create an animation to be sent
*
* @param file Animation
* @param params Additional parameters
*/ */
export function animation( export function animation(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaVideo> params: OmitTypeAndFile<InputMediaVideo> = {}
): InputMediaVideo { ): InputMediaVideo {
return { const ret = params as tl.Mutable<InputMediaVideo>
type: 'video', ret.type = 'video'
file, ret.file = file
isAnimated: true, ret.isAnimated = true
...(params || {}), return ret
}
} }
/** /**
* Create an audio to be sent * Create an audio to be sent
*
* @param file Audio file
* @param params Additional parameters
*/ */
export function audio( export function audio(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaAudio> params: OmitTypeAndFile<InputMediaAudio> = {}
): InputMediaAudio { ): InputMediaAudio {
return { const ret = params as tl.Mutable<InputMediaAudio>
type: 'audio', ret.type = 'audio'
file, ret.file = file
...(params || {}), return ret
}
} }
/** /**
* Create an document to be sent * Create an document to be sent
*
* @param file Document
* @param params Additional parameters
*/ */
export function document( export function document(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaDocument> params: OmitTypeAndFile<InputMediaDocument> = {}
): InputMediaDocument { ): InputMediaDocument {
return { const ret = params as tl.Mutable<InputMediaDocument>
type: 'document', ret.type = 'document'
file, ret.file = file
...(params || {}), return ret
}
} }
/** /**
* Create an photo to be sent * Create an photo to be sent
*
* @param file Photo
* @param params Additional parameters
*/ */
export function photo( export function photo(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaPhoto> params: OmitTypeAndFile<InputMediaPhoto> = {}
): InputMediaPhoto { ): InputMediaPhoto {
return { const ret = params as tl.Mutable<InputMediaPhoto>
type: 'photo', ret.type = 'photo'
file, ret.file = file
...(params || {}), return ret
}
} }
/** /**
* Create an video to be sent * Create an video to be sent
*
* @param file Video
* @param params Additional parameters
*/ */
export function video( export function video(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaVideo> params: OmitTypeAndFile<InputMediaVideo> = {}
): InputMediaVideo { ): InputMediaVideo {
return { const ret = params as tl.Mutable<InputMediaVideo>
type: 'video', ret.type = 'video'
file, ret.file = file
...(params || {}), return ret
}
} }
/** /**
* Create a voice message to be sent * Create a voice note to be sent
*
* @param file Voice note
* @param params Additional parameters
*/ */
export function voice( export function voice(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaVoice> params: OmitTypeAndFile<InputMediaVoice> = {}
): InputMediaVoice { ): InputMediaVoice {
return { const ret = params as tl.Mutable<InputMediaVoice>
type: 'voice', ret.type = 'voice'
file, ret.file = file
...(params || {}), return ret
}
} }
/** /**
* Create a sticker to be sent * Create a sticker to be sent
*
* @param file Sticker
* @param params Additional parameters
*/ */
export function sticker( export function sticker(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaSticker> params: OmitTypeAndFile<InputMediaSticker> = {}
): InputMediaSticker { ): InputMediaSticker {
return { const ret = params as tl.Mutable<InputMediaSticker>
type: 'sticker', ret.type = 'sticker'
file, ret.file = file
...(params || {}), return ret
}
} }
/** /**
* Create a venue to be sent * Create a venue to be sent
*
* @param params Venue parameters
*/ */
export function venue( export function venue(
params: OmitTypeAndFile<InputMediaVenue> params: OmitTypeAndFile<InputMediaVenue>
): InputMediaVenue { ): InputMediaVenue {
return { const ret = params as tl.Mutable<InputMediaVenue>
type: 'venue', ret.type = 'venue'
...params, return ret
}
} }
/** /**
* Create a geolocation to be sent * Create a geolocation to be sent
*
* @param latitude Latitude of the location
* @param longitude Longitude of the location
* @param params Additional parameters
*/ */
export function geo( export function geo(
latitude: number, latitude: number,
longitude: number, longitude: number,
params?: OmitTypeAndFile<InputMediaGeo, 'latitude' | 'longitude'> params: OmitTypeAndFile<InputMediaGeo, 'latitude' | 'longitude'> = {}
): InputMediaGeo { ): InputMediaGeo {
return { const ret = params as tl.Mutable<InputMediaGeo>
type: 'geo', ret.type = 'geo'
latitude, ret.latitude = latitude
longitude, ret.longitude = longitude
...(params || {}), return ret
}
} }
/** /**
* Create a live geolocation to be sent * Create a live geolocation to be sent
*
* @param latitude Latitude of the current location
* @param longitude Longitude of the current location
* @param params Additional parameters
*/ */
export function geoLive( export function geoLive(
latitude: number, latitude: number,
longitude: number, longitude: number,
params?: OmitTypeAndFile<InputMediaGeoLive, 'latitude' | 'longitude'> params: OmitTypeAndFile<InputMediaGeoLive, 'latitude' | 'longitude'> = {}
): InputMediaGeoLive { ): InputMediaGeoLive {
return { const ret = params as tl.Mutable<InputMediaGeoLive>
type: 'geo_live', ret.type = 'geo_live'
latitude, ret.latitude = latitude
longitude, ret.longitude = longitude
...(params || {}), return ret
}
} }
/** /**
@ -720,6 +742,8 @@ export namespace InputMedia {
* *
* For convenience, known dice emojis are available * For convenience, known dice emojis are available
* as static members of {@link Dice}. * as static members of {@link Dice}.
*
* @param emoji Emoji representing the dice
*/ */
export function dice(emoji: string): InputMediaDice { export function dice(emoji: string): InputMediaDice {
return { return {
@ -730,18 +754,21 @@ export namespace InputMedia {
/** /**
* Create a contact to be sent * Create a contact to be sent
*
* @param params Contact parameters
*/ */
export function contact( export function contact(
params: OmitTypeAndFile<InputMediaContact> params: OmitTypeAndFile<InputMediaContact>
): InputMediaContact { ): InputMediaContact {
return { const ret = params as tl.Mutable<InputMediaContact>
type: 'contact', ret.type = 'contact'
...params, return ret
}
} }
/** /**
* Create a game to be sent * Create a game to be sent
*
* @param game Game short name or TL object representing one
*/ */
export function game(game: string | tl.TypeInputGame): InputMediaGame { export function game(game: string | tl.TypeInputGame): InputMediaGame {
return { return {
@ -752,38 +779,41 @@ export namespace InputMedia {
/** /**
* Create an invoice to be sent * Create an invoice to be sent
*
* @param params Invoice parameters
*/ */
export function invoice( export function invoice(
params: OmitTypeAndFile<InputMediaInvoice> params: OmitTypeAndFile<InputMediaInvoice>
): InputMediaInvoice { ): InputMediaInvoice {
return { const ret = params as tl.Mutable<InputMediaInvoice>
type: 'invoice', ret.type = 'invoice'
...params, return ret
}
} }
/** /**
* Create a poll to be sent * Create a poll to be sent
*
* @param params Poll parameters
*/ */
export function poll( export function poll(
params: OmitTypeAndFile<InputMediaPoll> params: OmitTypeAndFile<InputMediaPoll>
): InputMediaPoll { ): InputMediaPoll {
return { const ret = params as tl.Mutable<InputMediaPoll>
type: 'poll', ret.type = 'poll'
...params, return ret
}
} }
/** /**
* Create a quiz to be sent * Create a quiz to be sent
*
* @param params Quiz parameters
*/ */
export function quiz( export function quiz(
params: OmitTypeAndFile<InputMediaQuiz> params: OmitTypeAndFile<InputMediaQuiz>
): InputMediaQuiz { ): InputMediaQuiz {
return { const ret = params as tl.Mutable<InputMediaQuiz>
type: 'quiz', ret.type = 'quiz'
...params, return ret
}
} }
/** /**
@ -793,15 +823,17 @@ export namespace InputMedia {
* Photo type is only inferred for reused files, * Photo type is only inferred for reused files,
* newly uploaded photos with `auto` will be * newly uploaded photos with `auto` will be
* uploaded as a document * uploaded as a document
*
* @param file The media file
* @param params Additional parameters
*/ */
export function auto( export function auto(
file: InputFileLike, file: InputFileLike,
params?: OmitTypeAndFile<InputMediaAuto> params: OmitTypeAndFile<InputMediaAuto> = {}
): InputMediaAuto { ): InputMediaAuto {
return { const ret = params as tl.Mutable<InputMediaAuto>
type: 'auto', ret.type = 'auto'
file, ret.file = file
...(params || {}), return ret
}
} }
} }