|
@@ -3,7 +3,7 @@ const { sleep } = require('../Helpers')
|
|
|
const errors = require('../errors')
|
|
|
const MemorySession = require('../sessions/Memory')
|
|
|
const { addKey } = require('../crypto/RSA')
|
|
|
-const { TLRequest } = require('../tl/tlobject')
|
|
|
+const { TLObject, TLRequest } = require('../tl/tlobject')
|
|
|
const utils = require('../Utils')
|
|
|
const Session = require('../sessions/Abstract')
|
|
|
const SQLiteSession = require('../sessions/SQLiteSession')
|
|
@@ -16,6 +16,7 @@ const MTProtoSender = require('../network/MTProtoSender')
|
|
|
const Helpers = require('../Helpers')
|
|
|
const { ConnectionTCPObfuscated } = require('../network/connection/TCPObfuscated')
|
|
|
const { BinaryWriter } = require('../extensions')
|
|
|
+const events = require('../events')
|
|
|
const DEFAULT_DC_ID = 4
|
|
|
const DEFAULT_IPV4_IP = '149.154.167.51'
|
|
|
const DEFAULT_IPV6_IP = '[2001:67c:4e8:f002::a]'
|
|
@@ -158,7 +159,6 @@ class TelegramClient {
|
|
|
async _updateLoop() {
|
|
|
while (this.isConnected()) {
|
|
|
const rnd = Helpers.getRandomInt(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
|
|
|
- console.log('rnd is ', rnd)
|
|
|
await Helpers.sleep(1000 * 60)
|
|
|
// We don't care about the result we just want to send it every
|
|
|
// 60 seconds so telegram doesn't stop the connection
|
|
@@ -167,7 +167,7 @@ class TelegramClient {
|
|
|
pingId: rnd,
|
|
|
}))
|
|
|
} catch (e) {
|
|
|
- console.log('err is ', e)
|
|
|
+ this._log.error('err is', e)
|
|
|
}
|
|
|
|
|
|
// this.session.save()
|
|
@@ -181,7 +181,7 @@ class TelegramClient {
|
|
|
try {
|
|
|
await this.invoke(new functions.updates.GetStateRequest())
|
|
|
} catch (e) {
|
|
|
- console.log('err is ', e)
|
|
|
+ this._log.error('err is', e)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -395,7 +395,7 @@ class TelegramClient {
|
|
|
e instanceof errors.PhoneCodeExpiredError ||
|
|
|
e instanceof errors.PhoneCodeHashEmptyError ||
|
|
|
e instanceof errors.PhoneCodeInvalidError) {
|
|
|
- console.log('Invalid code. Please try again.')
|
|
|
+ this._log.error('Invalid code. Please try again.')
|
|
|
} else {
|
|
|
throw e
|
|
|
}
|
|
@@ -420,8 +420,8 @@ class TelegramClient {
|
|
|
})
|
|
|
break
|
|
|
} catch (e) {
|
|
|
- console.log(e)
|
|
|
- console.log('Invalid password. Please try again')
|
|
|
+ this._log.error(e)
|
|
|
+ this._log.error('Invalid password. Please try again')
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
@@ -433,7 +433,7 @@ class TelegramClient {
|
|
|
|
|
|
}
|
|
|
const name = utils.getDisplayName(me)
|
|
|
- console.log('Signed in successfully as' + name)
|
|
|
+ this._log.error('Signed in successfully as', name)
|
|
|
return this
|
|
|
}
|
|
|
|
|
@@ -561,8 +561,22 @@ class TelegramClient {
|
|
|
}
|
|
|
|
|
|
|
|
|
- // event region
|
|
|
- addEventHandler(callback, event) {
|
|
|
+ /**
|
|
|
+ * Adds an event handler, allowing the specified callback to be
|
|
|
+ * called when a matching event (or events) is received.
|
|
|
+ */
|
|
|
+ addEventHandler(callback, event = null) {
|
|
|
+ if (Array.isArray(event)) {
|
|
|
+ event.forEach((e) => this.addEventHandler(callback, e))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!event) {
|
|
|
+ event = new events.Raw()
|
|
|
+ } else if (event.prototype instanceof TLObject) {
|
|
|
+ event = new events.Raw(event)
|
|
|
+ }
|
|
|
+
|
|
|
this._eventBuilders.push([event, callback])
|
|
|
}
|
|
|
|
|
@@ -830,7 +844,7 @@ class TelegramClient {
|
|
|
return utils.getInputPeer(channels.chats[0])
|
|
|
// eslint-disable-next-line no-empty
|
|
|
} catch (e) {
|
|
|
- console.log(e)
|
|
|
+ this._log.error(e)
|
|
|
}
|
|
|
}
|
|
|
throw new Error(`Could not find the input entity for ${peer.id || peer.channelId || peer.chatId || peer.userId}.
|
|
@@ -1057,7 +1071,6 @@ class TelegramClient {
|
|
|
let which
|
|
|
let loc
|
|
|
if (photo instanceof types.UserProfilePhoto || photo instanceof types.ChatPhoto) {
|
|
|
- console.log('i am ere')
|
|
|
dcId = photo.dcId
|
|
|
which = downloadBig ? photo.photoBig : photo.photoSmall
|
|
|
loc = new types.InputPeerPhotoFileLocation({
|
|
@@ -1066,7 +1079,7 @@ class TelegramClient {
|
|
|
volumeId: which.volumeId,
|
|
|
big: downloadBig,
|
|
|
})
|
|
|
- console.log(loc)
|
|
|
+ this._log.debug(loc)
|
|
|
} else {
|
|
|
// It doesn't make any sense to check if `photo` can be used
|
|
|
// as input location, because then this method would be able
|