feat(client): sendDice method

This commit is contained in:
teidesu 2021-04-10 15:10:34 +03:00
parent 7e4142a572
commit 0f2744b997
2 changed files with 116 additions and 0 deletions

View file

@ -27,6 +27,7 @@ import { iterHistory } from './methods/messages/iter-history'
import { _parseEntities } from './methods/messages/parse-entities'
import { searchGlobal } from './methods/messages/search-global'
import { searchMessages } from './methods/messages/search-messages'
import { sendDice } from './methods/messages/send-dice'
import { sendMedia } from './methods/messages/send-media'
import { sendPhoto } from './methods/messages/send-photo'
import { sendText } from './methods/messages/send-text'
@ -740,6 +741,46 @@ export class TelegramClient extends BaseTelegramClient {
): AsyncIterableIterator<Message> {
return searchMessages.apply(this, arguments)
}
/**
* Send an animated dice with a random value.
*
* For convenience, supported dice emojis are available
* as static members of {@link Dice}.
*
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
* @param emoji Emoji representing a dice
* @param params Additional sending parameters
* @link Dice
*/
sendDice(
chatId: InputPeerLike,
emoji: string,
params?: {
/**
* Message to reply to. Either a message object or message ID.
*/
replyTo?: number | Message
/**
* Whether to send this message silently.
*/
silent?: boolean
/**
* If set, the message will be scheduled to this date.
* When passing a number, a UNIX time in ms is expected.
*/
schedule?: Date | number
/**
* For bots: inline or reply markup or an instruction
* to hide a reply keyboard or to force a reply.
*/
replyMarkup?: ReplyMarkup
}
): Promise<Message> {
return sendDice.apply(this, arguments)
}
/**
* Send a single media.
*

View file

@ -0,0 +1,75 @@
import { TelegramClient } from '../../client'
import { BotKeyboard, InputPeerLike, Message, ReplyMarkup } from '../../types'
import { normalizeDate, randomUlong } from '../../utils/misc-utils'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Send an animated dice with a random value.
*
* For convenience, known dice emojis are available
* as static members of {@link Dice}.
*
* Note that dice result value is generated randomly on the server,
* you can't influence it in any way!
*
* @param chatId ID of the chat, its username, phone or `"me"` or `"self"`
* @param emoji Emoji representing a dice
* @param params Additional sending parameters
* @link Dice
* @internal
*/
export async function sendDice(
this: TelegramClient,
chatId: InputPeerLike,
emoji: string,
params?: {
/**
* Message to reply to. Either a message object or message ID.
*/
replyTo?: number | Message
/**
* Whether to send this message silently.
*/
silent?: boolean
/**
* If set, the message will be scheduled to this date.
* When passing a number, a UNIX time in ms is expected.
*/
schedule?: Date | number
/**
* For bots: inline or reply markup or an instruction
* to hide a reply keyboard or to force a reply.
*/
replyMarkup?: ReplyMarkup
}
): Promise<Message> {
if (!params) params = {}
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
const res = await this.call({
_: 'messages.sendMedia',
peer,
media: {
_: 'inputMediaDice',
emoticon: emoji
},
silent: params.silent,
replyToMsgId: params.replyTo
? typeof params.replyTo === 'number'
? params.replyTo
: params.replyTo.id
: undefined,
randomId: randomUlong(),
scheduleDate: normalizeDate(params.schedule),
replyMarkup,
message: ''
})
return this._findMessageInUpdate(res)
}