@darkwolf/telegram-bot.lazy.cjs
Version:
Telegram Bot API
92 lines (83 loc) • 2.68 kB
JavaScript
const types = require('../types')
const errors = require('../errors')
const constants = require('../constants')
class StopPollRequest {
constructor(parameters = {}, context) {
this.queryMethod = StopPollRequest.queryMethod
this
.setContext(context)
.setChatId(parameters.chatId)
.setMessageId(parameters.messageId)
.setReplyMarkup(parameters.replyMarkup)
}
setContext(context = {}) {
this.context = context
return this
}
setChatId(id) {
this.chatId = id
return this
}
setMessageId(id) {
this.messageId = id
return this
}
setReplyMarkup(markup) {
this.replyMarkup = markup ? (
markup instanceof types.InlineKeyboardMarkup ? markup : new types.InlineKeyboardMarkup(markup.inlineKeyboard)
) : undefined
return this
}
toParams() {
const params = {}
if (this.chatId) {
params.chat_id = this.chatId
}
if (this.messageId) {
params.message_id = this.messageId
}
if (this.replyMarkup) {
params.reply_markup = this.replyMarkup.toParams()
}
return params
}
async send() {
const response = await this.context.telegramBot.request(this)
if (response) {
if (response.ok) {
response.setResult(types.Poll.fromParams(response.result, this.context))
this.context.telegramBot.emit(constants.EventType.RESPONSE, response)
return response.result
} else {
let error
switch (response.errorCode) {
case 400: {
switch (response.description) {
case 'Bad Request: chat not found': {
error = new errors.ChatNotFoundError(this.id).setResponse(response)
break
}
case 'Bad Request: message with poll to stop not found': {
error = new errors.MessageNotFoundError(this.messageId).setResponse(response)
break
}
default: {
error = new errors.BadRequestError(response.description).setResponse(response)
}
}
break
}
}
if (!error) {
error = new errors.UnknownError(response.description).setResponse(response)
}
this.context.telegramBot.emit(constants.EventType.ERROR, error)
if (!this.context.telegramBot.settings.ignoreErrors) throw error
}
}
return null
}
}
StopPollRequest.queryMethod = 'stopPoll'
StopPollRequest.from = (parameters, context) => new StopPollRequest(parameters, context)
module.exports = StopPollRequest