feat(client): sendVote method
This commit is contained in:
parent
d36c1781bd
commit
9df884e667
3 changed files with 91 additions and 1 deletions
|
@ -75,6 +75,7 @@ import { searchMessages } from './methods/messages/search-messages'
|
||||||
import { sendMediaGroup } from './methods/messages/send-media-group'
|
import { sendMediaGroup } from './methods/messages/send-media-group'
|
||||||
import { sendMedia } from './methods/messages/send-media'
|
import { sendMedia } from './methods/messages/send-media'
|
||||||
import { sendText } from './methods/messages/send-text'
|
import { sendText } from './methods/messages/send-text'
|
||||||
|
import { sendVote } from './methods/messages/send-vote'
|
||||||
import { unpinMessage } from './methods/messages/unpin-message'
|
import { unpinMessage } from './methods/messages/unpin-message'
|
||||||
import { initTakeoutSession } from './methods/misc/init-takeout-session'
|
import { initTakeoutSession } from './methods/misc/init-takeout-session'
|
||||||
import {
|
import {
|
||||||
|
@ -121,6 +122,7 @@ import {
|
||||||
Message,
|
Message,
|
||||||
PartialExcept,
|
PartialExcept,
|
||||||
PartialOnly,
|
PartialOnly,
|
||||||
|
Poll,
|
||||||
ReplyMarkup,
|
ReplyMarkup,
|
||||||
SentCode,
|
SentCode,
|
||||||
StickerSet,
|
StickerSet,
|
||||||
|
@ -1850,6 +1852,22 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
clearDraft?: boolean
|
clearDraft?: boolean
|
||||||
}
|
}
|
||||||
): Promise<Message>
|
): 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.
|
* Unpin a message in a group, supergroup, channel or PM.
|
||||||
*
|
*
|
||||||
|
@ -2246,6 +2264,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
sendMediaGroup = sendMediaGroup
|
sendMediaGroup = sendMediaGroup
|
||||||
sendMedia = sendMedia
|
sendMedia = sendMedia
|
||||||
sendText = sendText
|
sendText = sendText
|
||||||
|
sendVote = sendVote
|
||||||
unpinMessage = unpinMessage
|
unpinMessage = unpinMessage
|
||||||
initTakeoutSession = initTakeoutSession
|
initTakeoutSession = initTakeoutSession
|
||||||
registerParseMode = registerParseMode
|
registerParseMode = registerParseMode
|
||||||
|
|
|
@ -30,7 +30,8 @@ import {
|
||||||
InputInlineResult,
|
InputInlineResult,
|
||||||
InputStickerSetItem,
|
InputStickerSetItem,
|
||||||
TakeoutSession,
|
TakeoutSession,
|
||||||
StickerSet
|
StickerSet,
|
||||||
|
Poll
|
||||||
} from '../types'
|
} from '../types'
|
||||||
|
|
||||||
// @copy
|
// @copy
|
||||||
|
|
70
packages/client/src/methods/messages/send-vote.ts
Normal file
70
packages/client/src/methods/messages/send-vote.ts
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue