2017-03-11 19:54:51 +01:00
#!/usr/bin/env php
< ? php
/*
2019-05-31 12:18:10 +02:00
Copyright 2016 - 2019 Daniil Gentili
2017-03-11 19:54:51 +01:00
( 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
2017-03-11 19:54:51 +01:00
try {
2017-11-08 13:04:40 +01:00
$Lua = \danog\MadelineProto\Serialization :: deserialize ( 'td.madeline' );
2017-03-11 19:54:51 +01:00
} 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-29 22:41:31 +01:00
$sentCode = $MadelineProto -> phoneLogin ( \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 );
2019-10-29 22:41:31 +01:00
$authorization = $MadelineProto -> completePhoneLogin ( $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-29 22:41:31 +01:00
$authorization = $MadelineProto -> completeSignup ( \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-29 22:41:31 +01:00
$authorization = $MadelineProto -> botLogin ( \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-03-11 19:54:51 +01:00
}
}
2017-09-03 16:26:26 +02:00
2017-03-11 19:54:51 +01:00
$offset = 0 ;
while ( true ) {
2019-10-29 22:41:31 +01:00
$updates = $Lua -> MadelineProto -> getUpdates ([ 'offset' => $offset , 'limit' => 50 , 'timeout' => 0 ]); // Just like in the bot API, you can specify an offset, a limit and a timeout
2017-03-11 19:54:51 +01:00
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
2019-10-29 22:41:31 +01:00
$Lua -> tdcliUpdateCallback ( $update [ 'update' ]);
2017-03-11 19:54:51 +01:00
}
echo 'Wrote ' . \danog\MadelineProto\Serialization :: serialize ( 'td.madeline' , $Lua ) . ' bytes' . PHP_EOL ;
}