I am using node.js with gram-js library to handle TDlib. In my project I gather statistics from multiple groups using GetBroadcastStats call. It is a prety basic call just like in documentation.
const stats = await client.invoke(
new Api.stats.GetBroadcastStats({
channel: channel,
dark: true,
})
);
It works just fine for most cases except for few specific channels. I get following error
RPCError: 303: STATS_MIGRATE_4 (caused by stats.GetBroadcastStats)
at RPCMessageToError (C:\Users\Igor\Desktop\work\telegram\tgStatistics\node_modules\telegram\errors\index.js:28:12)
at MTProtoSender._handleRPCResult (C:\Users\Igor\Desktop\work\telegram\tgStatistics\node_modules\telegram\network\MTProtoSender.js:517:58)
at MTProtoSender._processMessage (C:\Users\Igor\Desktop\work\telegram\tgStatistics\node_modules\telegram\network\MTProtoSender.js:442:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async MTProtoSender._recvLoop (C:\Users\Igor\Desktop\work\telegram\tgStatistics\node_modules\telegram\network\MTProtoSender.js:418:17) {
code: 303,
errorMessage: 'STATS_MIGRATE_4'
After some research in documentation I've figured out the cause of this problem. Documentation states: The query must be sent to the datacenter with ID channelFull.stats_dc, obtainable using channels.getFullChannel. In my case I have client with datacenter 2 but channel with datacenter 4. Apperantly it is problem related to TDlib itselft. I can't figure out how to change datacenter of my client or is it even possible. It seems kind of odd that this specific action needs so much additional actions when every telegram client I tested preforms statistics gathering seamlessly. I will appreciate any help
After one more days of search I finally figured out the solution. So telegram documentation contains also information about switching servers. So similar error may occur while downloading files from telegram. And file downloading feature is already added to Gram-js library, so I can view source code on how to do it. Using it I created my own implementation of switching datacenters.
It works for me in any cases. But it is recomended to implement server swithcing only when your client dcId is not the same as fullChannel.fullChat.statsDc