From 0f2744b997dc6778efff6f13ccaeadfcf6ee0109 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 10 Apr 2021 15:10:34 +0300 Subject: [PATCH] feat(client): sendDice method --- packages/client/src/client.ts | 41 ++++++++++ .../client/src/methods/messages/send-dice.ts | 75 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 packages/client/src/methods/messages/send-dice.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 989adf1b..9427bcfb 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -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 { 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 { + return sendDice.apply(this, arguments) + } /** * Send a single media. * diff --git a/packages/client/src/methods/messages/send-dice.ts b/packages/client/src/methods/messages/send-dice.ts new file mode 100644 index 00000000..189c9f23 --- /dev/null +++ b/packages/client/src/methods/messages/send-dice.ts @@ -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 { + 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) +}