Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get updates #67

Closed
abbas980301 opened this issue Mar 14, 2017 · 21 comments
Closed

get updates #67

abbas980301 opened this issue Mar 14, 2017 · 21 comments

Comments

@abbas980301
Copy link

First, thank you for this awesome project !
how can i Handling updates?
I tried this link
https://github.com/danog/MadelineProto#handling-updates
but i can't understand it! that return me duplicate updates in loop.
How can i remove updates from get_updates after process updates?

@danog
Copy link
Owner

danog commented Mar 14, 2017

You must increment the offset like with the bot API.

@danog danog closed this as completed Mar 14, 2017
@abbas980301
Copy link
Author

thanks.
after that ( increment the offset ) old updates will remove?

@danog
Copy link
Owner

danog commented Mar 14, 2017

@shakibonline Yep, exactly like in the bot API

@abbas980301
Copy link
Author

abbas980301 commented Mar 14, 2017

I use this code

$offset = 0;
while (1) {

    $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 10, 'timeout' => 1]); // Just like in the bot API, you can specify an offset, a limit and a timeout
    echo "\n\n";
    echo "===============[update]===========\n";
    echo json_encode($updates);
    echo "\n\n";

    foreach ($updates as $update) {
        $offset = $update['update_id']; // Just like in the bot API, the offset must be set to the last update_id
        // Parse $update['update'], that is an object of type Update
        if ($update['update']['_'] == 'updateNewMessage') {
            $messages_AffectedMessages = $MadelineProto->messages->readMessageContents(['id' => [$update['update']['message']['id']],]);
        }
    }
}

but it it didn't mark as read messages! what is wrong??

@danog
Copy link
Owner

danog commented Mar 14, 2017

@shakibonline Of course not, you aren't using the offset properly. Read the bot API documentation or take a look at the examples in the repo.

@abbas980301
Copy link
Author

abbas980301 commented Mar 14, 2017

what about this one?

        $offset = 0;
        while (true) {
        $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 1]); // Just like in the bot API, you can specify an offset, a limit and a timeout
        foreach ($updates as $update) {
            $offset = $update['update_id']; // Just like in the bot API, the offset must be set to the last update_id
            // Parse $update['update'], that is an object of type Update
        }
        var_dump($updates);
        }

is it true?

ps : i should store last offset for next run time?

@abbas980301
Copy link
Author

abbas980301 commented Mar 14, 2017

what is this error on running ( before load env & setting )

    An error getting response of method updates.getChannelDifference: WARNING: Wrong length was read (should've read 4, read 0)! in Connection on line 174. Retrying...

@danog
Copy link
Owner

danog commented Mar 14, 2017

is it true?

Nope, please check the official bot API documentation.

ps : i should store last offset for next run time?

Nope, please check the official bot API documentation.

what is this error on running

It sometimes happens, ignore it.

@abbas980301
Copy link
Author

abbas980301 commented Mar 15, 2017

thanks for answers.

Nope, please check the official bot API documentation.

did you mean this page?
http://daniil.it/MadelineProto/

@danog
Copy link
Owner

danog commented Mar 15, 2017

Nope, Google "telegram bot API documentation"

@Amirjan
Copy link

Amirjan commented Apr 10, 2017

Hi @danog
Is it possible get updates near real time?

@danog
Copy link
Owner

danog commented Apr 10, 2017

Yes, use callbacks.
You must run $MadelineProto->get_updates_difference() in a loop to get updates with the callback.

@Amirjan
Copy link

Amirjan commented Apr 10, 2017

I implement it:

function onNewChannelMessage($update){
file_put_contents('ChannelMessage.txt',print_r($update,true) , FILE_APPEND);
}

function onNewMessage($update){
file_put_contents('NewMessage.txt',print_r($update,true) , FILE_APPEND);
}

$MadelineProto->eventManager()->bind('NewChannelMessage', 'onNewChannelMessage');
$MadelineProto->eventManager()->bind('NewMessage', 'onNewMessage');

while(true){
$MadelineProto->get_updates_difference();
}

@vahidvdn
Copy link

You must run $MadelineProto->get_updates_difference() in a loop to get updates with the callback

@danog Hi. How did you implement non-blocking in php?

@danog
Copy link
Owner

danog commented Apr 11, 2017

I use pthreads for multithreading, but to enable it a ZTS version of PHP must be installed, along with the pthreads extension.

@nicorac
Copy link

nicorac commented Apr 28, 2017

@Amirjan: I'm trying to use your sample code:
#67 (comment)

but the line
$MadelineProto->eventManager()->bind('NewChannelMessage', 'onNewChannelMessage');

throws an exception:
PHP Fatal error: Uncaught danog\MadelineProto\TL\Exception: Could not find method: eventManager in madelineProto/src/danog/MadelineProto/TL/TL.php:357

I've also searched for "eventManager" in the whole source tree unsuccessfully.
Where is it supposed to be defined?

@danog
Copy link
Owner

danog commented Apr 28, 2017

@Amirjan is clearly using a custom event manager, a pull request would be nice :3

@Amirjan
Copy link

Amirjan commented Apr 29, 2017

@nicorac I implement it with my custom event manager
If dear @danog allows it, I will publish it here

@danog
Copy link
Owner

danog commented Apr 29, 2017

@Amirjan Sure, send a pr!

@Cojad
Copy link

Cojad commented May 9, 2017

@Amirjan, your event manager looks so awesome!

@Amirjan
Copy link

Amirjan commented May 10, 2017

ApiEventsManager.php:

listeners[$event][] = $callback; } public function fire($event, array $parameters) { if (!empty($this->listeners[$event])) { foreach ($this->listeners[$event] as $listener) { call_user_func_array($listener, $parameters); } } } } ?>

in MTProto.php:

protected $eventManager;

public function __construct($settings = []){

......
$this->should_serialize = true;
$this->eventManager = new ApiEventsManager();
}

public function eventManager()
{
    return $this->eventManager;
}

/MTProtoTools/UpdateHandler.php
public function get_updates_update_handler($update)
{
if (!$this->settings['updates']['handle_updates']) {
return;
}
switch ($update['_']) {
case 'updateNewChannelMessage':
$this->eventManager()->fire('NewChannelMessage',[$update]);
break;
case 'updateNewMessage':
if (isset($update['message']['out']) && $update['message']['out']) {
break;
}
$this->eventManager()->fire('NewMessage',[$update]);
break;
}
$this->eventManager()->fire('onUpdate',[$update]);
$this->updates[$this->updates_key++] = $update;
$this->should_serialize = true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants