MadelineProto icon indicating copy to clipboard operation
MadelineProto copied to clipboard

Receive 2 to 4 equal updates from bots

Open StrangePers opened this issue 9 years ago • 5 comments

When any bot in private messaging with me send a message - there are 2 to 4 updates with UpdateNewMessage that are absolutly the same. (Only update_id is encrimenting). Why is it happening and how to handle it? With users, channels, groups, supergroups there's no such problem. Only messages that bots send appears to be duplicated.

StrangePers avatar Apr 12 '17 15:04 StrangePers

How are you getting updates? Are you using the latest version? Is the pts of the messages different?

danog avatar Apr 13 '17 00:04 danog

How are you getting updates?

 $offset  = 0;
while(1){
    $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0.5]);
        foreach ($updates as $update) {
            $offset = $update['update_id'] + 1;
            switch ($update['update']['_']) {
                case 'updateNewMessage':
                           //do stuff
                case 'updateNewChannelMessage':
                          //do stuff
                  ...
             }
        }
  echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL;
}

Are you using the latest version?

Yes

Is the pts of the messages different?

pts is the same. Only update_id differes

P.S. And all this works perfectly for all messages in chats, supergroups, pm. Duplicate pts is just received from bots, when writing them directly. P.P.S. Today received duplicate updates for even privite message from user(updateNewMessage). It also had only different update_ids.

StrangePers avatar Apr 13 '17 18:04 StrangePers

I've seen the same strange behavior. As a temporary workaround I'm filtering out duplicates (by message ID) with something like this:

while(1) {
    $seen_ids = [];  // array containing seen message IDs
    $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0.5]);
    foreach ($updates as $update) {
        $offset = $update['update_id'] + 1;
        // -----------------------------------------
        // skip already seen message IDs
        if (!empty($update['update']['message']['id']) {
            if (in_array($update['update']['message']['id'], $seen_ids)) {
                continue;
            }
            // mark this message ID as seen
            $seen_ids[] = $update['update']['message']['id'];
        }
        // -----------------------------------------
        switch ($update['update']['_']) {
            case 'updateNewMessage':
                       //do stuff
            case 'updateNewChannelMessage':
                      //do stuff
              ...
         }
    }
}

nicorac avatar May 12 '17 08:05 nicorac

Same problem for me. For now I use workaround proposed above. Actually not only messages from bots are duplicated, but sometimes messages in private chats are too.

stmotw avatar May 19 '17 16:05 stmotw

Fixed

danog avatar Jun 15 '17 13:06 danog