import { Api } from 'telegram' ; import { Snake } from '../../client' ; import { SendMedia , sendMediaMoreParams } from './SendMedia' ; import { UploadFile } from './UploadFile' ; import { decodeFileId } from 'tg-file-id' ; import { Media } from '../../Utils/Media' ; import BigInt from 'big-integer' ; export async function SendPhoto ( snakeClient , chatId , fileId , more ? ) { try { if ( Buffer . isBuffer ( fileId ) ) { fileId as Buffer ; let file = await UploadFile ( snakeClient , fileId , { workers : more ?. workers || 1 , } ) ; let media = new Api . InputMediaUploadedPhoto ( { file : new Api . InputFile ( { ... file ! } ) , } ) ; return SendMedia ( snakeClient , chatId , media , more ) ; } if ( typeof fileId == 'string' && ( /^http/i . exec ( String ( fileId ) ) || /^(\/|\.\.?\/|~\/)/i . exec ( String ( fileId ) ) ) ) { fileId as string ; let file = await UploadFile ( snakeClient , fileId , { workers : more ?. workers || 1 , } ) ; let media = new Api . InputMediaUploadedPhoto ( { file : new Api . InputFile ( { ... file ! } ) , } ) ; return SendMedia ( snakeClient , chatId , media , more ) ; } if ( fileId instanceof Media ) { let file ; try { fileId as Media ; file = fileId . decode ( ) ; } catch ( e ) { throw new Error ( 'Invalid fileId!' ) ; } if ( file ) { if ( file . typeId !== 2 ) { throw new Error ( `typeId invalid. typeId must be 2 but got ${ file . typeId }` ) ; } let accessHash = String ( file . access_hash ) ; let id = String ( file . id ) ; let results ; while ( true ) { try { let media = new Api . InputMediaPhoto ( { id : new Api . InputPhoto ( { id : BigInt ( id ) , accessHash : BigInt ( accessHash ) , fileReference : Buffer . from ( file . fileReference , 'hex' ) , } ) , } ) ; results = SendMedia ( snakeClient , chatId , media , more ) ; break ; } catch ( e ) { if ( Number ( accessHash ) > 0 && Number ( id ) > 0 ) { accessHash = `-${ accessHash }` ; } else if ( Number ( accessHash ) < 0 && Number ( id ) > 0 ) { id = `-${ id }` ; } else if ( Number ( accessHash ) < 0 && Number ( id ) < 0 ) { accessHash = accessHash . replace ( /^\-/ , '' ) ; } else { throw new Error ( e . message ) ; break ; } } } return results ; } } if ( typeof fileId == 'string' ) { let file ; try { file = decodeFileId ( String ( fileId ) ) ; } catch ( e ) { throw new Error ( 'Invalid fileId!' ) ; } if ( file ) { if ( file . typeId !== 2 ) { throw new Error ( `typeId invalid. typeId must be 2 but got ${ file . typeId }` ) ; } let accessHash = String ( file . accessHash ) ; let id = String ( file . id ) ; let results ; while ( true ) { try { let media = new Api . InputMediaPhoto ( { id : new Api . InputPhoto ( { id : BigInt ( id ) , accessHash : BigInt ( accessHash ) , fileReference : Buffer . from ( file . fileReference , 'hex' ) , } ) , } ) ; results = SendMedia ( snakeClient , chatId , media , more ) ; break ; } catch ( e ) { if ( Number ( accessHash ) > 0 && Number ( id ) > 0 ) { accessHash = `-${ accessHash }` ; } else if ( Number ( accessHash ) < 0 && Number ( id ) > 0 ) { id = `-${ id }` ; } else if ( Number ( accessHash ) < 0 && Number ( id ) < 0 ) { accessHash = accessHash . replace ( /^\-/ , '' ) ; } else { throw new Error ( e . message ) ; break ; } } } return results ; } } } catch ( error ) { return snakeClient . _handleError ( error , `telegram.sendPhoto(${ chatId },${ Buffer . isBuffer ( fileId ) ? `<Buffer ${ fileId . toString ( 'hex' ) }>` : JSON . stringify ( fileId ) }${ more ? ',' + JSON . stringify ( more ) : '' })` ) ; } }