-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
84 lines (78 loc) · 2.36 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Rubika;
use Rubika\Exception\internetConnectionError;
use Rubika\Http\Kernel;
use Rubika\Tools\Color;
use Rubika\Tools\Brain;
use WebSocket\Client as websocket;
/**
* run client with getupdates
*/
abstract class Client extends Bot
{
/**
* @param integer $phone phone number
* @param boolean $runWeb web mode
* @param boolean $log logging errors
* @throws internetConnectionError error in testing socket links
*/
public function __construct(int $phone, bool $runWeb = false, bool $log = true)
{
parent::__construct($phone, $runWeb, $log);
$url = Kernel::get_socket_links();
if (count($url) == 0) {
throw new internetConnectionError(Color::color(' error in testing socket links ', background: 'red'));
}
$url = $url[array_rand($url, 1)];
$this->onStart();
$this->onUpdateMessage($url, $runWeb);
}
/**
* function to anwser updates
*
* @param array $update
* @return void
*/
abstract function runBot(array $update);
/**
* run codes when client started
*
* @return void
*/
abstract function onStart(): void;
/**
* when get update
*
* @param string $url webSocket url
* @return void
*/
private function onUpdateMessage(string $url): void
{
$socket = new websocket($url, [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'origin' => 'https://web.rubika.ir',
],
'timeout' => 60
]);
$socket->text(json_encode([
'api_version' => '5',
'auth' => $this->account->auth,
'data' => '',
'method' => 'handShake'
]));
$second = 0;
do {
if (($second % 15) === 0) {
$socket->text('{}');
}
usleep(500000);
$second++;
$update = json_decode($socket->receive(), true);
if (isset($update['data_enc'])) {
$update = json_decode((new Brain($this->account->encryptKey))->decrypt($update['data_enc']), true);
}
$this->runBot(isset($update['message_updates']) ? $update['message_updates'] : []);
} while (true);
}
}