我正在尝试从Telegram API (而不是bot API)获得最新消息。我目前正在使用messages.getHistory,但从一开始就返回所有消息。如果我收到了新消息(因为我登录了),那也没问题。
到目前为止,我最好的选择是读取所有的消息,然后跟踪偏移量,这样我就不会再读相同的消息了,但是这太慢了,而且资源也很昂贵。
发布于 2017-04-04 06:06:03
有一种从Telegram API获得实时更新的更简单的方法.
如果您将您的TCP连接设置为非轮询,那么只要您的电报帐户有更新,这些消息就会被简单地推送给您。
这消除了您提到的成本,您根本没有任何副本。
对于我的电报客户端,我成功地做到了这一点,只需在启动时运行它:
TL.invokewithlayer(layer, TL.initconnection(app_id, device_model, system_version, app_version, lang_code, TL.help_getconfig))然后,当连接的TCP套接字到达时,我只处理来自它的传入数据。
发布于 2017-04-05 07:03:20
查尔斯的回答为我指明了正确的方向。对于那些对node.js版本感兴趣的人,我设法通过使用电报链接模块并将connectionType设置为TCP使其正常工作:
var telegramLink = require('telegram.link')();
// set the environment
var app = {
// NOTE: if you FORK the project you MUST use your APP ID.
// Otherwise YOUR APPLICATION WILL BE BLOCKED BY TELEGRAM
// You can obtain your own APP ID for your application here: https://my.telegram.org
id: 12345,
hash: 'somehashcode',
version: require('../package.json').version,
lang: 'en',
deviceModel: os.type().replace('Darwin', 'OS_X'),
systemVersion: os.platform() + '/' + os.release(),
connectionType: 'TCP'
};
//var primaryDC = telegramLink.TEST_PRIMARY_DC;
var primaryDC = telegramLink.PROD_PRIMARY_DC;
...
telegramLink.createClient(app, dataCenter, function() {
...简单的一点是,将其更改为TCP将给您提供所需的效果,并且您将在registerOnUpdates中将消息推送给您:
clientProxy.getClient().account.updateStatus(false).then(function() {
clientProxy.getClient().registerOnUpdates(function(update) {
console.log('update', update.toPrintable());
clientProxy.getClient().messages.receivedMessages(update.id, function(err) { console.log(err); });
});
...注意receivedMessages --如果你不打这个电话,电报就不会给你任何新的更新。如果电报链接中没有定义receivedMessages,请将以下代码添加到lib/api/messages.js中:
// ***
// messages.**receivedMessages(max_id, [callback])**
// Return a Promise to Confirms receipt of messages by a client, cancels PUSH-notification sending.
// [Click here for more details](https://core.telegram.org/method/messages.receivedMessages)
Messages.prototype.receivedMessages = function(max_id, callback) {
return utility.callService(api.service.messages.receivedMessages, this.client, this.client._channel, callback, arguments);
};https://stackoverflow.com/questions/43178748
复制相似问题