From 9df884e66782ac813f611d8fa8a90c8eaa504d35 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 8 May 2021 12:36:15 +0300 Subject: [PATCH] feat(client): sendVote method --- packages/client/src/client.ts | 19 +++++ packages/client/src/methods/_imports.ts | 3 +- .../client/src/methods/messages/send-vote.ts | 70 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 packages/client/src/methods/messages/send-vote.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 7b9bf3bc..fae976c5 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -75,6 +75,7 @@ import { searchMessages } from './methods/messages/search-messages' import { sendMediaGroup } from './methods/messages/send-media-group' import { sendMedia } from './methods/messages/send-media' import { sendText } from './methods/messages/send-text' +import { sendVote } from './methods/messages/send-vote' import { unpinMessage } from './methods/messages/unpin-message' import { initTakeoutSession } from './methods/misc/init-takeout-session' import { @@ -121,6 +122,7 @@ import { Message, PartialExcept, PartialOnly, + Poll, ReplyMarkup, SentCode, StickerSet, @@ -1850,6 +1852,22 @@ export interface TelegramClient extends BaseTelegramClient { clearDraft?: boolean } ): Promise + /** + * Send or retract a vote in a poll. + * + * @param chatId Chat ID where this poll was found + * @param message Message ID where this poll was found + * @param options + * Selected options, or `null` to retract. + * You can pass indexes of the answers or the `Buffer`s + * representing them. In case of indexes, the poll will first + * be requested from the server. + */ + sendVote( + chatId: InputPeerLike, + message: number, + options: null | MaybeArray + ): Promise /** * Unpin a message in a group, supergroup, channel or PM. * @@ -2246,6 +2264,7 @@ export class TelegramClient extends BaseTelegramClient { sendMediaGroup = sendMediaGroup sendMedia = sendMedia sendText = sendText + sendVote = sendVote unpinMessage = unpinMessage initTakeoutSession = initTakeoutSession registerParseMode = registerParseMode diff --git a/packages/client/src/methods/_imports.ts b/packages/client/src/methods/_imports.ts index eb215449..a191911d 100644 --- a/packages/client/src/methods/_imports.ts +++ b/packages/client/src/methods/_imports.ts @@ -30,7 +30,8 @@ import { InputInlineResult, InputStickerSetItem, TakeoutSession, - StickerSet + StickerSet, + Poll } from '../types' // @copy diff --git a/packages/client/src/methods/messages/send-vote.ts b/packages/client/src/methods/messages/send-vote.ts new file mode 100644 index 00000000..3331812e --- /dev/null +++ b/packages/client/src/methods/messages/send-vote.ts @@ -0,0 +1,70 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike, MtCuteArgumentError, MtCuteTypeAssertionError, Poll } from '../../types' +import { MaybeArray } from '@mtcute/core' +import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils' +import { assertTypeIs } from '../../utils/type-assertion' + +/** + * Send or retract a vote in a poll. + * + * @param chatId Chat ID where this poll was found + * @param message Message ID where this poll was found + * @param options + * Selected options, or `null` to retract. + * You can pass indexes of the answers or the `Buffer`s + * representing them. In case of indexes, the poll will first + * be requested from the server. + * @internal + */ +export async function sendVote( + this: TelegramClient, + chatId: InputPeerLike, + message: number, + options: null | MaybeArray +): Promise { + if (options === null) options = [] + if (!Array.isArray(options)) options = [options] + + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) + + let poll: Poll | undefined = undefined + if (options.some((it) => typeof it === 'number')) { + const msg = await this.getMessages(peer, message) + if (!(msg.media instanceof Poll)) + throw new MtCuteArgumentError('This message does not contain a poll') + + poll = msg.media + options = options.map((opt) => { + if (typeof opt === 'number') { + return poll!.raw.answers[opt].option + } + return opt + }) + } + + const res = await this.call({ + _: 'messages.sendVote', + peer, + msgId: message, + options: options as Buffer[] + }) + + if (!(res._ === 'updates' || res._ === 'updatesCombined')) + throw new MtCuteTypeAssertionError( + '_findMessageInUpdate', + 'updates | updatesCombined', + res._ + ) + + this._handleUpdate(res, true) + + const upd = res.updates[0] + assertTypeIs('sendVote (@ messages.sendVote)', upd, 'updateMessagePoll') + if (!upd.poll) { + throw new MtCuteTypeAssertionError('sendVote (@ messages.sendVote)', 'poll', 'undefined') + } + + const { users } = createUsersChatsIndex(res) + + return new Poll(this, upd.poll, users, upd.results) +}