Skip to content

Commit 1897c00

Browse files
committed
boting: Version 3.5
Fixes exception and catch function.
1 parent 63fbade commit 1897c00

2 files changed

Lines changed: 41 additions & 24 deletions

File tree

example.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
$Bot = new Boting();
77

8+
$Bot->catch(function ($e) {
9+
echo $e;
10+
11+
// $e->getErrorDescription();
12+
// $e->getErrorCode();
13+
});
14+
815
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
916
$ChatId = $Update["message"]["chat"]["id"];
1017
try {
@@ -79,4 +86,4 @@
7986
}
8087
});
8188

82-
$Bot->handler("1145282131:AAESHOxq5LTBEbGu8kc0EWaPQRFZ5e9nz-E");
89+
$Bot->handler("1145282131:AAF5JVPTV0iJPQbpO17eAZ_qbVNd_wXS-kc");

src/Boting/Boting.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,26 @@
33
use GuzzleHttp\Client;
44
use Spatie\Regex\Regex;
55

6-
class Exception extends \Exception
7-
{
6+
class Exception extends \Exception {
87
private $Message = '';
98
private $Description = '';
109

11-
public function __construct($Message, $Code = 0, \Exception $Previous = null, $Error = '', $Description = '')
12-
{
10+
public function __construct($Message, $Code = 0, \Exception $Previous = null, $Error = '', $Description = '') {
1311
$this->Code = $Code;
1412
$this->Message = $Error;
1513
$this->Description = $Description;
1614
parent::__construct($Message, $Code, $Previous);
1715
}
1816

19-
public function getErrorCode(): string
20-
{
17+
public function getErrorCode(): string {
2118
return $this->Code;
2219
}
2320

24-
public function getErrorDescription(): string
25-
{
21+
public function getErrorDescription(): string {
2622
return $this->Description;
2723
}
2824

29-
public function __toString()
30-
{
25+
public function __toString() {
3126
return 'Telegram returned an error: ' . $this->Description.PHP_EOL.'Backtrace:'.PHP_EOL.$this->getTraceAsString();
3227
}
3328
}
@@ -47,26 +42,33 @@ public function __construct() {
4742
$this->Type = ["animation", "audio", "document", "photo", "sticker", "video", "video_note", "voice", "contact", "dice", "game", "poll", "venue", "location", "new_chat_members", "left_chat_member", "new_chat_title", "new_chat_photo", "delete_chat_photo", "group_chat_created", "supergroup_chat_created", "pinned_message", "text"];
4843
$this->Types = [];
4944
$this->Request = [];
50-
45+
$this->errorHandler = false;
5146
}
5247

5348
public function getUpdates() {
5449
if ($this->WebHook == false) {
5550
$Request = $this->Client->getAsync('getUpdates?timeout=10&offset=' . $this->Offset, ["verify" => false])->wait();
5651
$Body = $Request->getBody()->getContents();
5752

58-
$sonuc = json_decode($Body, true)["result"];
59-
if (!is_array($sonuc)) echo $sonuc;
60-
if (count($sonuc) >= 1) {
61-
$sonuc = array_reverse($sonuc);
62-
if ($sonuc[0]["update_id"] > $this->LatUpdate) {
63-
$this->LatUpdate = $sonuc[0]["update_id"];
64-
$this->Offset = $sonuc[0]["update_id"] + 1;
65-
return $sonuc;
66-
} else {
67-
return false;
53+
$sonuc = json_decode($Body, true);
54+
if ($sonuc["ok"] === false) {
55+
if ($sonuc["error_code"] === 401) {
56+
throw new Exception("ERROR_" . $sonuc["error_code"], $sonuc["error_code"], NULL, "Invalid Bot Token", "Invalid Bot Token");
6857
}
69-
}
58+
} else {
59+
$sonuc = $sonuc["result"];
60+
if (!is_array($sonuc)) return;
61+
if (count($sonuc) >= 1) {
62+
$sonuc = array_reverse($sonuc);
63+
if ($sonuc[0]["update_id"] > $this->LatUpdate) {
64+
$this->LatUpdate = $sonuc[0]["update_id"];
65+
$this->Offset = $sonuc[0]["update_id"] + 1;
66+
return $sonuc;
67+
} else {
68+
return false;
69+
}
70+
}
71+
}
7072
} else {
7173
$Body = file_get_contents("php://input");
7274
return json_decode($Body, true);
@@ -92,13 +94,21 @@ public function downloadFile($FileId, $fileName = NULL) {
9294
return $fileName;
9395
}
9496

97+
public function catch($function) {
98+
return $this->errorHandler = [$function];
99+
}
100+
95101

96102
public function __call($method, $args) {
97103
$Request = $this->Client->postAsync($method, ["form_params" => $args[0]])->wait();
98104
$Json = json_decode($Request->getBody()->getContents(), true);
99105

100106
if ($Json["ok"] === false) {
101-
throw new Exception("ERROR_" . $Json["error_code"], $Json["error_code"], NULL, $Json["description"], $Json["description"]);
107+
if ($this->errorHandler === false) {
108+
throw new Exception("ERROR_" . $Json["error_code"], $Json["error_code"], NULL, $Json["description"], $Json["description"]);
109+
} else {
110+
$this->errorHandler[0](new Exception("ERROR_" . $Json["error_code"], $Json["error_code"], NULL, $Json["description"], $Json["description"]));
111+
}
102112
} else {
103113
return $Json;
104114
}

0 commit comments

Comments
 (0)