MadelineProto/examples/lua/td.php

66 lines
3.6 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/*
2019-05-31 12:18:10 +02:00
Copyright 2016-2019 Daniil Gentili
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
require '../vendor/autoload.php';
$settings = [];
2017-09-03 16:26:26 +02:00
$Lua = false;
2017-08-13 18:52:32 +02:00
try {
2017-11-08 13:04:40 +01:00
$Lua = \danog\MadelineProto\Serialization::deserialize('td.madeline');
} catch (\danog\MadelineProto\Exception $e) {
2017-09-03 16:26:26 +02:00
}
2019-10-28 19:48:59 +01:00
if (!\is_object($Lua)) {
2017-09-03 16:26:26 +02:00
$MadelineProto = new \danog\MadelineProto\API($settings);
2019-10-28 19:48:59 +01:00
while (!\in_array(($res = \readline('Do you want to login as a user or as a bot (u/b)? ')), ['u', 'b'])) {
2017-09-03 16:26:26 +02:00
echo 'Please write either u or b'.PHP_EOL;
}
switch ($res) {
case 'u':
2019-10-28 19:48:59 +01:00
$sentCode = $MadelineProto->phone_login(\readline('Enter your phone number: '));
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($sentCode, \danog\MadelineProto\Logger::NOTICE);
2017-09-03 16:26:26 +02:00
echo 'Enter the code you received: ';
2019-10-28 19:48:59 +01:00
$code = \fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1);
2017-09-03 16:26:26 +02:00
$authorization = $MadelineProto->complete_phone_login($code);
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($authorization, \danog\MadelineProto\Logger::NOTICE);
2017-09-03 16:26:26 +02:00
if ($authorization['_'] === 'account.noPassword') {
throw new \danog\MadelineProto\Exception('2FA is enabled but no password is set!');
}
if ($authorization['_'] === 'account.password') {
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log('2FA is enabled', \danog\MadelineProto\Logger::NOTICE);
2019-10-28 19:48:59 +01:00
$authorization = $MadelineProto->complete_2fa_login(\readline('Please enter your password (hint '.$authorization['hint'].'): '));
2017-09-03 16:26:26 +02:00
}
if ($authorization['_'] === 'account.needSignup') {
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log('Registering new user', \danog\MadelineProto\Logger::NOTICE);
2019-10-28 19:48:59 +01:00
$authorization = $MadelineProto->complete_signup(\readline('Please enter your first name: '), \readline('Please enter your last name (can be empty): '));
2017-09-03 16:26:26 +02:00
}
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($authorization, \danog\MadelineProto\Logger::NOTICE);
2017-09-03 16:26:26 +02:00
$Lua = new \danog\MadelineProto\Lua('madeline.lua', $MadelineProto);
break;
case 'b':
2019-10-28 19:48:59 +01:00
$authorization = $MadelineProto->bot_login(\readline('Please enter a bot token: '));
2018-03-02 01:38:10 +01:00
\danog\MadelineProto\Logger::log($authorization, \danog\MadelineProto\Logger::NOTICE);
2017-09-03 16:26:26 +02:00
$Lua = new \danog\MadelineProto\Lua('madeline.lua', $MadelineProto);
break;
}
}
2017-09-03 16:26:26 +02:00
$offset = 0;
while (true) {
2018-03-04 17:42:48 +01:00
$updates = $Lua->MadelineProto->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout
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
$Lua->tdcli_update_callback($update['update']);
}
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('td.madeline', $Lua).' bytes'.PHP_EOL;
}