import * as download from 'download' ; import { NextFunction } from 'grammy' ; import Context from '@/models/Context' ; import bot from '@/helpers/bot' ; import fileUrl from '@/helpers/fileUrl' ; import logAnswerTime from '@/helpers/logAnswerTime' ; import report from '@/helpers/report' ; export default async function checkGoogleCredentials ( ctx , next ) { const replyToMessage = ctx . message ?. reply_to_message ; if ( ! replyToMessage ) { return next ( ) ; } if ( replyToMessage . from ?. id !== bot . botInfo . id ) { return next ( ) ; } if ( replyToMessage . message_id !== ctx . dbchat . googleSetupMessageId ) { return next ( ) ; } const document = ctx . message ?. document ; if ( ! document ) { return next ( ) ; } if ( ! document . file_name || ( ! document . file_name . includes ( 'json' ) && ! document . file_name . includes ( 'txt' ) ) ) { return ctx . reply ( ctx . i18n . t ( 'google_error_mime' ) , { parse_mode : 'Markdown' , } ) ; } try { const fileData = await ctx . api . getFile ( document . file_id ) ; const url = await fileUrl ( fileData . file_path ) ; const data = await download ( url ) ; ctx . dbchat . googleKey = data . toString ( 'utf8' ) ; await ctx . dbchat . save ( ) ; await ctx . reply ( ctx . i18n . t ( 'google_success' , { projectId : JSON . parse ( ctx . dbchat . googleKey ) . project_id , } ) , { parse_mode : 'Markdown' , } ) ; logAnswerTime ( ctx , 'credentials check' ) ; } catch ( error ) { report ( error , { ctx , location : 'setupCheckingCredentials' } ) ; } }