feat(client): sendVote method

This commit is contained in:
teidesu 2021-05-08 12:36:15 +03:00
parent d36c1781bd
commit 9df884e667
3 changed files with 91 additions and 1 deletions

View file

@ -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<Message>
/**
* 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<number | Buffer>
): Promise<Poll>
/**
* 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

View file

@ -30,7 +30,8 @@ import {
InputInlineResult,
InputStickerSetItem,
TakeoutSession,
StickerSet
StickerSet,
Poll
} from '../types'
// @copy

View file

@ -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<number | Buffer>
): Promise<Poll> {
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)
}