This commit is contained in:
Daniil Gentili 2017-01-29 21:42:21 +00:00
parent 3d203fc128
commit aadd278c5a
3 changed files with 25 additions and 15 deletions

View File

@ -15,12 +15,14 @@ if (file_exists('token.php') && $MadelineProto === false) {
$offset = 0;
while (true) {
$updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout
var_dump($updates);
foreach ($updates as $update) {
$offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id
var_dump($update);
//var_dump($update);
switch ($update['update']['_']) {
case 'updateNewMessage':
if ($update['update']['message']['out']) {
case 'updateNewChannelMessage':
if (isset($update['update']['message']['out']) && $update['update']['message']['out']) {
continue;
}
$res = json_encode($update, JSON_PRETTY_PRINT);
@ -28,7 +30,8 @@ while (true) {
$res = var_export($update, true);
}
try {
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
//var_dump($update);
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['to_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
}

View File

@ -66,6 +66,7 @@ trait UpdateHandler
return;
}
$this->updates[$this->updates_key++] = $update;
$this->should_serialize = true;
//\danog\MadelineProto\Logger::log(['Stored ', $update);
}
@ -91,8 +92,10 @@ trait UpdateHandler
$params['offset'] = array_reverse(array_keys($this->updates))[abs($params['offset']) - 1];
}
$updates = [];
ksort($this->updates);
foreach ($this->updates as $key => $value) {
if ($params['offset'] > $key) {
$this->should_serialize = true;
unset($this->updates[$key]);
} else if ($params['limit'] === null || count($updates) < $params['limit']) {
$updates[] = ['update_id' => $key, 'update' => $value];
@ -113,7 +116,7 @@ trait UpdateHandler
public function set_channel_state($channel, $data)
{
$this->get_channel_state($channel)['pts'] = (!isset($data['pts']) || $data['pts'] === 0) ? $this->get_channel_state($channel)['pts'] : $data['pts'];
if (isset($data['pts']) && $data['pts'] !== 0) { $this->should_serialize = true; $this->get_channel_state($channel)['pts'] = $data['pts']; };
}
public function get_channel_difference($channel)
@ -147,8 +150,6 @@ trait UpdateHandler
}
break;
case 'updates.channelDifferenceTooLong':
//unset($this->channels_state[$channel]);
//unset($this->chats[$this->get_info('channel#'.$channel)['bot_api_id']]);
$this->handle_update_messages($difference['messages'], $channel);
$this->set_channel_state($channel, $difference);
unset($difference);
@ -164,11 +165,9 @@ trait UpdateHandler
public function set_update_state($data)
{
$this->get_update_state()['pts'] = (!isset($data['pts']) || $data['pts'] === 0) ? $this->get_update_state()['pts'] : $data['pts'];
$this->get_update_state()['seq'] = (!isset($data['seq']) || $data['seq'] === 0) ? $this->get_update_state()['seq'] : $data['seq'];
$this->get_update_state()['date'] = (!isset($data['date']) || $data['date'] < $this->get_update_state()['date']) ? $this->get_update_state()['date'] : $data['date'];
return $this->get_update_state();
if (isset($data['pts']) && $data['pts'] !== 0) { $this->should_serialize = true; $this->get_update_state()['pts'] = $data['pts']; };
if (isset($data['seq']) && $data['seq'] !== 0) { $this->should_serialize = true; $this->get_update_state()['seq'] = $data['seq']; };
if (isset($data['date']) && $data['date'] > $this->get_update_state()['date']) { $this->should_serialize = true; $this->get_update_state()['date'] = $data['date']; };
}
public function &get_update_state()
@ -325,6 +324,7 @@ trait UpdateHandler
}
if ($update['pts'] > $cur_state['pts']) {
$cur_state['pts'] = $update['pts'];
$this->should_serialize = true;
$pop_pts = true;
} elseif (isset($update['pts_count'])) {
\danog\MadelineProto\Logger::log(['Duplicate update. current pts: '.$cur_state['pts'].' + pts count: '.(isset($update['pts_count']) ? $update['pts_count'] : 0).' = new pts: '.$new_pts.'. update pts: '.$update['pts'].' <= current pts '.$cur_state['pts'].', channel id: '.$channel_id], \danog\MadelineProto\Logger::ERROR);
@ -333,6 +333,8 @@ trait UpdateHandler
}
if ($channel_id !== false && isset($options['date']) && $this->get_update_state()['date'] < $options['date']) {
$this->get_update_state()['date'] = $options['date'];
$this->should_serialize = true;
}
} elseif ($channel_id === false && isset($options['seq']) && $options['seq'] > 0) {
$seq = $options['seq'];
@ -350,6 +352,7 @@ trait UpdateHandler
}
if ($cur_state['seq'] != $seq) {
$this->should_serialize = true;
$cur_state['seq'] = $seq;
if (isset($options['date']) && $cur_state['date'] < $options['date']) {
$cur_state['date'] = $options['date'];
@ -372,6 +375,7 @@ trait UpdateHandler
if (!$this->settings['updates']['handle_updates']) {
return;
}
$this->should_serialize = true;
$next_seq = $this->get_update_state()['seq'] + 1;
if (empty($this->get_update_state()['pending_seq_updates'][$next_seq]['updates'])) {
return false;
@ -394,6 +398,7 @@ trait UpdateHandler
if (!$this->settings['updates']['handle_updates']) {
return;
}
$this->should_serialize = true;
if ($channel_id === false) {
$cur_state = &$this->get_update_state();
} else {
@ -461,6 +466,9 @@ trait UpdateHandler
if (!$this->settings['updates']['handle_updates']) {
return;
}
if (isset($update['message']['_']) && $update['message']['_'] === 'messageEmpty') {
return;
}
if (isset($update['message']['from_id']) && $update['message']['from_id'] === $this->datacenter->authorization['user']['id']) {
$update['message']['out'] = true;
}

View File

@ -181,8 +181,7 @@ trait TL
}
$auto = false;
if (!is_array($object) && in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) {
//var_dump($this->get_info($object));
if ((!is_array($object) || (isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) && in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) {
$object = $this->get_info($object)[$type['type']];
}
if (!isset($object['_'])) {
@ -197,7 +196,7 @@ trait TL
$constructorData = $this->constructors->find_by_predicate($predicate);
if ($constructorData === false) {
\danog\MadelineProto\Logger::log([$object], \danog\MadelineProto\Logger::FATAL_WARNING);
\danog\MadelineProto\Logger::log([$object], \danog\MadelineProto\Logger::FATAL_ERROR);
throw new Exception('Could not extract type');
}