feat(client): closePoll method

This commit is contained in:
teidesu 2021-05-08 13:19:37 +03:00
parent 9df884e667
commit 8a0c9984b5
4 changed files with 83 additions and 0 deletions

View file

@ -60,6 +60,7 @@ import { _normalizeFileToDocument } from './methods/files/normalize-file-to-docu
import { _normalizeInputFile } from './methods/files/normalize-input-file' import { _normalizeInputFile } from './methods/files/normalize-input-file'
import { _normalizeInputMedia } from './methods/files/normalize-input-media' import { _normalizeInputMedia } from './methods/files/normalize-input-media'
import { uploadFile } from './methods/files/upload-file' import { uploadFile } from './methods/files/upload-file'
import { closePoll } from './methods/messages/close-poll'
import { deleteMessages } from './methods/messages/delete-messages' import { deleteMessages } from './methods/messages/delete-messages'
import { editInlineMessage } from './methods/messages/edit-inline-message' import { editInlineMessage } from './methods/messages/edit-inline-message'
import { editMessage } from './methods/messages/edit-message' import { editMessage } from './methods/messages/edit-message'
@ -1178,6 +1179,16 @@ export interface TelegramClient extends BaseTelegramClient {
*/ */
progressCallback?: (uploaded: number, total: number) => void progressCallback?: (uploaded: number, total: number) => void
}): Promise<UploadedFile> }): Promise<UploadedFile>
/**
* Close a poll sent by you.
*
* Once closed, poll can't be re-opened, and nobody
* will be able to vote in it
*
* @param chatId Chat ID where this poll was found
* @param message Message ID where this poll was found
*/
closePoll(chatId: InputPeerLike, message: number): Promise<Poll>
/** /**
* Delete messages, including service messages. * Delete messages, including service messages.
* *
@ -2249,6 +2260,7 @@ export class TelegramClient extends BaseTelegramClient {
protected _normalizeInputFile = _normalizeInputFile protected _normalizeInputFile = _normalizeInputFile
protected _normalizeInputMedia = _normalizeInputMedia protected _normalizeInputMedia = _normalizeInputMedia
uploadFile = uploadFile uploadFile = uploadFile
closePoll = closePoll
deleteMessages = deleteMessages deleteMessages = deleteMessages
editInlineMessage = editInlineMessage editInlineMessage = editInlineMessage
editMessage = editMessage editMessage = editMessage

View file

@ -178,6 +178,7 @@ export async function _normalizeInputMedia(
_: 'inputMediaPoll', _: 'inputMediaPoll',
poll: { poll: {
_: 'poll', _: 'poll',
closed: media.closed,
id: bigInt.zero, id: bigInt.zero,
publicVoters: media.public, publicVoters: media.public,
multipleChoice: media.multiple, multipleChoice: media.multiple,

View file

@ -0,0 +1,64 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteTypeAssertionError, Poll } from '../../types'
import { tl } from '@mtcute/tl'
import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import bigInt from 'big-integer'
import { assertTypeIs } from '../../utils/type-assertion'
/**
* Close a poll sent by you.
*
* Once closed, poll can't be re-opened, and nobody
* will be able to vote in it
*
* @param chatId Chat ID where this poll was found
* @param message Message ID where this poll was found
* @internal
*/
export async function closePoll(
this: TelegramClient,
chatId: InputPeerLike,
message: number
): Promise<Poll> {
const res = await this.call({
_: 'messages.editMessage',
peer: normalizeToInputPeer(await this.resolvePeer(chatId)),
id: message,
media: {
_: 'inputMediaPoll',
poll: {
_: 'poll',
id: bigInt.zero,
closed: true,
question: '',
answers: [],
},
},
})
if (!(res._ === 'updates' || res._ === 'updatesCombined'))
throw new MtCuteTypeAssertionError(
'_findMessageInUpdate',
'updates | updatesCombined',
res._
)
this._handleUpdate(res, true)
const upd = res.updates[0]
assertTypeIs('closePoll (@ messages.editMessage)', upd, 'updateMessagePoll')
if (!upd.poll) {
throw new MtCuteTypeAssertionError(
'closePoll (@ messages.editMessage)',
'poll',
'undefined'
)
}
const { users } = createUsersChatsIndex(res)
return new Poll(this, upd.poll, users, upd.results)
}

View file

@ -449,6 +449,12 @@ export interface InputMediaPoll {
*/ */
answers: (string | tl.TypePollAnswer)[] answers: (string | tl.TypePollAnswer)[]
/**
* Whether this is poll is closed
* (i.e. nobody can vote anymore)
*/
closed?: boolean
/** /**
* Whether this is a public poll * Whether this is a public poll
* (i.e. users who have voted are visible to everyone) * (i.e. users who have voted are visible to everyone)