[FEATURE] Add AI-based /ask command (complementing #54) #56

Merged
lucmsilva651 merged 14 commits from ai-features into main 2025-06-28 19:22:15 +00:00
Showing only changes of commit 276d053ed5 - Show all commits

View file

@ -45,15 +45,85 @@ class RateLimiter {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return `${chatId}:${messageId}`
}
private async waitForRateLimit(): Promise<void> {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (this.isRateLimited) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const now = Date.now()
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (now < this.rateLimitEndTime) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const waitTime = this.rateLimitEndTime - now
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await new Promise(resolve => setTimeout(resolve, waitTime))
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.isRateLimited = false
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
private async waitForRateLimit(chatId: number, messageId: number): Promise<void> {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (!this.isRateLimited) return
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
console.log(`[✨ AI | RATELIMIT] [${chatId}:${messageId}] Ratelimited, waiting for end of ${this.rateLimitEndTime - Date.now()}ms`)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const now = Date.now()
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (now < this.rateLimitEndTime) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await new Promise(resolve => setTimeout(resolve, this.rateLimitEndTime - now))
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
this.isRateLimited = false
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
private chunkText(text: string): string[] {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const chunks: string[] = []
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
let currentChunk = ''
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
let currentLength = 0
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const lines = text.split('\n')
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
for (const line of lines) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentLength + line.length + 1 > this.max_msg_length) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentChunk) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
chunks.push(currentChunk)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk = ''
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength = 0
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (line.length > this.max_msg_length) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
for (let i = 0; i < line.length; i += this.max_msg_length) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
chunks.push(line.substring(i, i + this.max_msg_length))
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk = line
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength = line.length
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentChunk) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk += '\n'
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength++
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk += line
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength += line.length
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentChunk) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
chunks.push(currentChunk)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return chunks
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
private handleTelegramError(error: unknown, messageKey: string, options: any, ctx: Context, chatId: number, messageId: number): boolean {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (!isTelegramError(error)) return false
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (error.response.error_code === 429) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const retryAfter = error.response.parameters?.retry_after || 1
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.isRateLimited = true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.rateLimitEndTime = Date.now() + (retryAfter * 1000)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const existingTimeout = this.updateQueue.get(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (existingTimeout) clearTimeout(existingTimeout)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const timeout = setTimeout(() => {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.processUpdate(ctx, chatId, messageId, options)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}, retryAfter * 1000)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.set(messageKey, timeout)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (error.response.error_code === 400) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (error.response.description?.includes("can't parse entities") || error.response.description?.includes("MESSAGE_TOO_LONG")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const plainOptions = { ...options, parse_mode: undefined }
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.processUpdate(ctx, chatId, messageId, plainOptions)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (error.response.description?.includes("message is not modified")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.pendingUpdates.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
logger.logError(error)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.pendingUpdates.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
logger.logError(error)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.pendingUpdates.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
private async processUpdate(
@ -68,81 +138,45 @@ class RateLimiter {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const now = Date.now()
const timeSinceLastEdit = now - this.lastEditTime
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await this.waitForRateLimit()
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await this.waitForRateLimit(chatId, messageId)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (timeSinceLastEdit < this.minInterval) {
const existingTimeout = this.updateQueue.get(messageKey)
if (existingTimeout) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
clearTimeout(existingTimeout)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (existingTimeout) clearTimeout(existingTimeout)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const timeout = setTimeout(() => {
this.processUpdate(ctx, chatId, messageId, options)
}, this.minInterval - timeSinceLastEdit)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.set(messageKey, timeout)
return
}
try {
if (latestText.length > this.max_msg_length) {
const chunks: string[] = []
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
let currentChunk = ''
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
let currentLength = 0
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
// Split text into chunks while preserving markdown formatting
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const lines = latestText.split('\n')
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
for (const line of lines) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentLength + line.length + 1 > this.max_msg_length) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentChunk) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
chunks.push(currentChunk)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk = ''
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength = 0
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
// if a single line is too long, split
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (line.length > this.max_msg_length) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
for (let i = 0; i < line.length; i += this.max_msg_length) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
chunks.push(line.substring(i, i + this.max_msg_length))
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk = line
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength = line.length
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentChunk) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk += '\n'
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength++
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentChunk += line
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
currentLength += line.length
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (currentChunk) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
chunks.push(currentChunk)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const chunks = this.chunkText(latestText)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const firstChunk = chunks[0]
logger.logChunk(chatId, messageId, firstChunk)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
try {
await ctx.telegram.editMessageText(chatId, messageId, undefined, firstChunk, options)
} catch (error: any) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (!error.response?.description?.includes("message is not modified")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} catch (error: unknown) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
isTelegramError(error) &&
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
!error.response.description?.includes("message is not modified")
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
throw error
}
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
for (let i = 1; i < chunks.length; i++) {
const chunk = chunks[i]
const overflowMessageId = this.overflowMessages.get(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (overflowMessageId) {
try {
await ctx.telegram.editMessageText(chatId, overflowMessageId, undefined, chunk, options)
logger.logChunk(chatId, overflowMessageId, chunk, true)
} catch (error: any) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (!error.response?.description?.includes("message is not modified")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} catch (error: unknown) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
isTelegramError(error) &&
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
!error.response.description?.includes("message is not modified")
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
throw error
}
}
@ -155,7 +189,6 @@ class RateLimiter {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.overflowMessages.set(messageKey, newMessage.message_id)
}
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.pendingUpdates.set(messageKey, firstChunk)
if (chunks.length > 1) {
this.pendingUpdates.set(
@ -164,54 +197,23 @@ class RateLimiter {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
)
}
} else {
const sanitizedText = latestText
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
logger.logChunk(chatId, messageId, sanitizedText)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
logger.logChunk(chatId, messageId, latestText)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
try {
await ctx.telegram.editMessageText(chatId, messageId, undefined, sanitizedText, options)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} catch (error: any) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (!error.response?.description?.includes("message is not modified")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await ctx.telegram.editMessageText(chatId, messageId, undefined, latestText, options)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} catch (error: unknown) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
isTelegramError(error) &&
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
!error.response.description?.includes("message is not modified")
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
throw error
}
}
this.pendingUpdates.delete(messageKey)
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.lastEditTime = Date.now()
this.updateQueue.delete(messageKey)
} catch (error: any) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (error.response?.error_code === 429) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const retryAfter = error.response.parameters?.retry_after || 1
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.isRateLimited = true
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.rateLimitEndTime = Date.now() + (retryAfter * 1000)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const existingTimeout = this.updateQueue.get(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (existingTimeout) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
clearTimeout(existingTimeout)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const timeout = setTimeout(() => {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.processUpdate(ctx, chatId, messageId, options)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}, retryAfter * 1000)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.set(messageKey, timeout)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else if (error.response?.error_code === 400) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (error.response?.description?.includes("can't parse entities")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
// try again with plain text
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const plainOptions = { ...options, parse_mode: undefined }
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await this.processUpdate(ctx, chatId, messageId, plainOptions)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else if (error.response?.description?.includes("MESSAGE_TOO_LONG")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
const plainOptions = { ...options, parse_mode: undefined }
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
await this.processUpdate(ctx, chatId, messageId, plainOptions)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else if (error.response?.description?.includes("message is not modified")) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.pendingUpdates.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
logger.logError(error)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.pendingUpdates.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
this.updateQueue.delete(messageKey)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} else {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
} catch (error: unknown) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
if (!this.handleTelegramError(error, messageKey, options, ctx, chatId, messageId)) {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
logger.logError(error)
this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey)
@ -232,4 +234,13 @@ class RateLimiter {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
}
export const rateLimiter = new RateLimiter()
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
export const rateLimiter = new RateLimiter()
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
function isTelegramError(error: unknown): error is { response: { description?: string, error_code?: number, parameters?: { retry_after?: number } } } {
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
return (
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
typeof error === "object" &&
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
error !== null &&
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
"response" in error &&
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
typeof (error as any).response === "object"
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
)
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type
}
GiovaniFZ commented 2025-06-28 13:58:13 +00:00 (Migrated from github.com)

pls, avoid to set any as a type

pls, avoid to set any as a type