Skip to content

Commit 80c8ffe

Browse files
authored
fix: download url (#169)
1 parent bdfc372 commit 80c8ffe

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

apps/core-telegram/server/services/queue/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function handleTicketMessageCreated(msg: EventMessage['TicketMessageCreate
3535
// Send Telegram message to Owner user via Wasabi Bot
3636
const wasabiUser = await repository.telegram.findUserByIdAndBotId(msg.data.ticketOwnerId, telegram.wasabiBotId)
3737
if (wasabiUser) {
38-
const text = `${wasabiUser.user?.name} ${wasabiUser.user?.surname}: ${msg.data.userText}`
38+
const text = `${msg.data.userName} ${msg.data.userSurname}: ${msg.data.userText}`
3939
await useWasabiBot().api.sendMessage(wasabiUser.telegramId, text)
4040
}
4141

apps/core-telegram/server/services/telegram/wasabi-bot.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Ticket, User } from '@roll-stack/database'
22
import type { Context } from 'grammy'
3+
import fs from 'node:fs/promises'
34
import { repository } from '@roll-stack/database'
45
import { Bot } from 'grammy'
56
import { generateAccessCode } from './common'
@@ -147,7 +148,9 @@ async function handlePhoto(ctx: Context) {
147148
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
148149
if (downloadUrl) {
149150
const uploaded = await uploadToStorage(downloadUrl, fileId)
150-
fileUrl = uploaded.fileUrl
151+
if (uploaded?.fileUrl) {
152+
fileUrl = uploaded.fileUrl
153+
}
151154
}
152155

153156
// Forward messages with file to group
@@ -188,7 +191,9 @@ async function handleVideo(ctx: Context) {
188191
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
189192
if (downloadUrl) {
190193
const uploaded = await uploadToStorage(downloadUrl, fileId)
191-
fileUrl = uploaded.fileUrl
194+
if (uploaded?.fileUrl) {
195+
fileUrl = uploaded.fileUrl
196+
}
192197
}
193198

194199
await ctx.api.forwardMessage(telegram.filesGroupId, ctx.message.chat.id, ctx.message.message_id)
@@ -228,7 +233,9 @@ async function handleFile(ctx: Context) {
228233
const downloadUrl = await getFileDownloadUrl({ ctx, fileId, botToken })
229234
if (downloadUrl) {
230235
const uploaded = await uploadToStorage(downloadUrl, fileId)
231-
fileUrl = uploaded.fileUrl
236+
if (uploaded?.fileUrl) {
237+
fileUrl = uploaded.fileUrl
238+
}
232239
}
233240

234241
await ctx.api.forwardMessage(telegram.filesGroupId, ctx.message.chat.id, ctx.message.message_id)
@@ -278,7 +285,8 @@ async function getFileDownloadUrl(data: { ctx: Context, fileId: string, botToken
278285
return null
279286
}
280287

281-
return `${data.botToken}/${file.file_path}`
288+
// /var/lib/bot/token/documents/file_id.ext
289+
return file.file_path ?? null
282290
} catch (e) {
283291
logger.error('getFileDownloadUrl', e)
284292
return null
@@ -295,16 +303,25 @@ async function getBotToken(): Promise<string | null> {
295303
}
296304

297305
async function uploadToStorage(downloadUrl: string, fileId: string) {
298-
const extension = downloadUrl.split('.').pop()
299-
const buffer = await fetch(downloadUrl).then((res) => res.arrayBuffer())
306+
try {
307+
const extension = downloadUrl.split('.').pop()
308+
309+
const buffer = await fs.readFile(downloadUrl)
310+
if (!buffer) {
311+
return null
312+
}
300313

301-
const fileInnerUri = `/${S3_TELEGRAM_DIRECTORY}/${fileId}.${extension}`
302-
const fileUrl = `${mediaUrl}${fileInnerUri}`
314+
const fileInnerUri = `/${S3_TELEGRAM_DIRECTORY}/${fileId}.${extension}`
315+
const fileUrl = `${mediaUrl}${fileInnerUri}`
303316

304-
const storage = useStorage('s3')
305-
await storage.setItemRaw(fileInnerUri, buffer)
317+
const storage = useStorage('s3')
318+
await storage.setItemRaw(fileInnerUri, buffer)
306319

307-
return { fileUrl }
320+
return { fileUrl }
321+
} catch (e) {
322+
logger.error('uploadToStorage', e)
323+
return null
324+
}
308325
}
309326

310327
export function useWasabiBot(): Bot {

0 commit comments

Comments
 (0)