import constants = require ( '../.constants' ) ; import http = require ( 'http' ) ; import ariaTools = require ( '../download_tools/aria-tools' ) ; import TelegramBot = require ( 'node-telegram-bot-api' ) ; import details = require ( '../dl_model/detail' ) ; import dlm = require ( '../dl_model/dl-manager' ) ; var dlManager = dlm . DlManager . getInstance ( ) ; export async function deleteMsg ( bot , msg , delay ? ) { if ( delay ) await sleep ( delay ) ; bot . deleteMessage ( msg . chat . id , msg . message_id . toString ( ) ) . catch ( err => { console . log ( `Failed to delete message. Does the bot have message delete permissions for this chat? ${ err . message }` ) ; } ) ; } export function editMessage ( bot , msg , text ) { if ( msg && msg . chat && msg . chat . id && msg . message_id ) { return bot . editMessageText ( text , { chat_id : msg . chat . id , message_id : msg . message_id , parse_mode : 'HTML' } ) ; } else { return Promise . resolve ( ) ; } } export function sendMessage ( bot , msg , text , delay ? , callback ? , quickDeleteOriginal ? ) { if ( ! delay ) delay = 10000 ; bot . sendMessage ( msg . chat . id , text , { reply_to_message_id : msg . message_id , parse_mode : 'HTML' } ) . then ( ( res ) => { if ( callback ) callback ( res ) ; if ( delay > - 1 ) { deleteMsg ( bot , res , delay ) ; if ( quickDeleteOriginal ) { deleteMsg ( bot , msg ) ; } else { deleteMsg ( bot , msg , delay ) ; } } } ) . catch ( ( err ) => { console . error ( `sendMessage error: ${ err . message }` ) ; } ) ; } export function sendUnauthorizedMessage ( bot , msg ) { sendMessage ( bot , msg , `You aren't authorized to use this bot here.` ) ; } export function sendMessageReplyOriginal ( bot , dlDetails , message ) { return bot . sendMessage ( dlDetails . tgChatId , message , { reply_to_message_id : dlDetails . tgMessageId , parse_mode : 'HTML' } ) ; } export function sleep ( ms ) { return new Promise ( resolve => setTimeout ( resolve , ms ) ) ; } export function isAuthorized ( msg , skipDlOwner ? ) { for ( var i = 0 ; i < constants . SUDO_USERS . length ; i ++ ) { if ( constants . SUDO_USERS [ i ] === msg . from . id ) return 0 ; } if ( ! skipDlOwner && msg . reply_to_message ) { var dlDetails = dlManager . getDownloadByMsgId ( msg . reply_to_message ) ; if ( dlDetails && msg . from . id === dlDetails . tgFromId ) return 1 ; } if ( constants . AUTHORIZED_CHATS . indexOf ( msg . chat . id ) > - 1 && msg . chat . all_members_are_administrators ) return 2 ; if ( constants . AUTHORIZED_CHATS . indexOf ( msg . chat . id ) > - 1 ) return 3 ; return - 1 ; } export function isAdmin ( bot , msg , callback ) { bot . getChatAdministrators ( msg . chat . id ) . then ( members => { for ( var i = 0 ; i < members . length ; i ++ ) { if ( members [ i ] . user . id === msg . from . id ) { callback ( null , true ) ; return ; } } callback ( null , false ) ; } ) . catch ( ( ) => { callback ( null , false ) ; } ) ; } param { boolean } successful param { string } gid param { number } originGroup param { string } driveURL export function notifyExternal ( dlDetails , successful , gid , originGroup , driveURL ? ) { if ( ! constants . DOWNLOAD_NOTIFY_TARGET || ! constants . DOWNLOAD_NOTIFY_TARGET . enabled ) return ; ariaTools . getStatus ( dlDetails , ( err , message , filename , filesize ) => { var name ; var size ; if ( ! err ) { if ( filename !== 'Metadata' ) name = filename ; if ( filesize !== '0B' ) size = filesize ; } const data = JSON . stringify ( { successful : successful , file : { name : name , driveURL : driveURL , size : size } , originGroup : originGroup } ) ; const options = { host : constants . DOWNLOAD_NOTIFY_TARGET . host , port : constants . DOWNLOAD_NOTIFY_TARGET . port , path : constants . DOWNLOAD_NOTIFY_TARGET . path , method : 'POST' , headers : { 'Content-Type' : 'application/json' , 'Content-Length' : Buffer . byteLength ( data ) } } ; var req = http . request ( options ) ; req . on ( 'error' , ( e ) => { console . error ( `notifyExternal failed: ${ e . message }` ) ; } ) ; req . write ( data ) ; req . end ( ) ; } ) ; }