Skip to content

Commit 2705664

Browse files
Update Bot.php
1 parent 4094312 commit 2705664

1 file changed

Lines changed: 101 additions & 58 deletions

File tree

src/Bot.php

Lines changed: 101 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ public function file(string $path): static
108108
$this->builder_file_type = null;
109109
return $this;
110110
}
111-
111+
112112
public function file_id(string $file_id): static
113113
{
114114
$this->builder_file_id = $file_id;
115115
return $this;
116116
}
117-
117+
118118
public function file_type(string $file_type): static
119119
{
120120
$this->builder_file_type = $file_type;
121121
return $this;
122122
}
123-
123+
124124
public function caption(string $caption): static
125125
{
126126
$this->builder_caption = $caption;
@@ -147,20 +147,20 @@ public function contact(string $first_name, string $phone_number): static
147147
$this->builder_contact_phone = $phone_number;
148148
return $this;
149149
}
150-
150+
151151
public function inlineKeypad(array $keypad): static
152152
{
153153
$this->builder_inline_keypad = $keypad;
154154
return $this;
155155
}
156-
156+
157157
public function chatKeypad(array $keypad, ?string $keypad_type = 'New'): static
158158
{
159159
$this->builder_chat_keypad = $keypad;
160160
$this->builder_chat_keypad_type = $keypad_type;
161161
return $this;
162162
}
163-
163+
164164
public function forwardFrom(string $from_chat_id): static
165165
{
166166
$this->builder_from_chat_id = $from_chat_id;
@@ -172,43 +172,43 @@ public function forwardTo(string $to_chat_id): static
172172
$this->builder_to_chat_id = $to_chat_id;
173173
return $this;
174174
}
175-
175+
176176
public function messageId(string $message_id): static
177177
{
178178
$this->builder_message_id = $message_id;
179179
return $this;
180180
}
181-
181+
182182
public function setMaxMessages(int $maxMessages): void
183-
{
184-
$this->maxMessages = $maxMessages;
185-
}
183+
{
184+
$this->maxMessages = $maxMessages;
185+
}
186186

187-
public function setTimeWindow(int $timeWindow): void
188-
{
189-
$this->timeWindow = $timeWindow;
190-
}
187+
public function setTimeWindow(int $timeWindow): void
188+
{
189+
$this->timeWindow = $timeWindow;
190+
}
191191

192-
public function setCooldown(int $cooldown): void
193-
{
194-
$this->cooldown = $cooldown;
195-
}
192+
public function setCooldown(int $cooldown): void
193+
{
194+
$this->cooldown = $cooldown;
195+
}
196196

197-
public function getMaxMessages(): int
198-
{
199-
return $this->maxMessages;
200-
}
197+
public function getMaxMessages(): int
198+
{
199+
return $this->maxMessages;
200+
}
201201

202-
public function getTimeWindow(): int
203-
{
204-
return $this->timeWindow;
205-
}
202+
public function getTimeWindow(): int
203+
{
204+
return $this->timeWindow;
205+
}
206+
207+
public function getCooldown(): int
208+
{
209+
return $this->cooldown;
210+
}
206211

207-
public function getCooldown(): int
208-
{
209-
return $this->cooldown;
210-
}
211-
212212
private function resetBuilder(): void
213213
{
214214
$this->builder_text = null;
@@ -280,7 +280,7 @@ public function sendFile(): array
280280
$file_type = $this->builder_file_type ?? 'Image';
281281
$file_id = $this->builder_file_id ?? null;
282282
}
283-
// sendFile
283+
284284
$params = [
285285
'chat_id' => $this->builder_chat_id,
286286
'file_id' => $file_id,
@@ -304,7 +304,7 @@ public function sendFile(): array
304304
$this->resetBuilder();
305305
return ['api' => $res, 'file_id' => $file_id, 'type' => $file_type];
306306
}
307-
307+
308308
public function sendPoll(): array
309309
{
310310
if (!$this->builder_chat_id) {
@@ -323,7 +323,7 @@ public function sendPoll(): array
323323
$this->resetBuilder();
324324
return $res;
325325
}
326-
326+
327327
public function sendLocation(): array
328328
{
329329
if (!$this->builder_chat_id) {
@@ -381,7 +381,7 @@ public function sendContact(): array
381381
$this->resetBuilder();
382382
return $res;
383383
}
384-
384+
385385
public function forward(): array
386386
{
387387
if (!$this->builder_from_chat_id || !$this->builder_message_id || !$this->builder_to_chat_id) {
@@ -398,37 +398,63 @@ public function forward(): array
398398
return $res;
399399
}
400400

401-
private function sendEditText(): array
401+
402+
public function sendEditText(): array
402403
{
403404
if (!$this->builder_chat_id || !$this->builder_message_id || $this->builder_text === null) {
404405
throw new \InvalidArgumentException("chat_id, message_id and text are required for edit");
405406
}
407+
406408
$params = [
407409
'chat_id' => $this->builder_chat_id,
408410
'message_id' => $this->builder_message_id,
409411
'text' => $this->builder_text,
410412
];
413+
411414
$res = $this->apiRequest('editMessageText', $params);
415+
$this->lastResponse = $res;
416+
$this->resetBuilder();
412417
return $res;
413418
}
414-
415-
private function sendEditChatKeypad(): array
419+
420+
421+
private function sendEditInlineKeypad(): array
416422
{
417-
if (!$this->builder_chat_keypad || !$this->builder_chat_id) {
418-
throw new \InvalidArgumentException("inline keypad or message_id | chat id are required for edit inline keypad");
423+
if (!$this->builder_chat_id || !$this->builder_message_id || !$this->builder_inline_keypad) {
424+
throw new \InvalidArgumentException("chat_id, message_id and inline_keypad are required for edit inline keypad");
419425
}
426+
420427
$params = [
421428
'chat_id' => $this->builder_chat_id,
422429
'message_id' => $this->builder_message_id,
423430
'inline_keypad' => $this->builder_inline_keypad,
424431
];
432+
425433
$res = $this->apiRequest('editMessageKeypad', $params);
426434
return $res;
427435
}
428-
436+
437+
438+
private function sendEditChatKeypad(): array
439+
{
440+
if (!$this->builder_chat_id || !$this->builder_message_id || !$this->builder_chat_keypad) {
441+
throw new \InvalidArgumentException("chat_id, message_id and chat_keypad are required for edit chat keypad");
442+
}
443+
444+
$params = [
445+
'chat_id' => $this->builder_chat_id,
446+
'message_id' => $this->builder_message_id,
447+
'chat_keypad' => $this->builder_chat_keypad,
448+
'chat_keypad_type' => $this->builder_chat_keypad_type ?? 'New',
449+
];
450+
451+
$res = $this->apiRequest('editMessageKeypad', $params);
452+
return $res;
453+
}
454+
429455
public function editMessage(): array {
430456
if (!$this->builder_chat_id || !$this->builder_message_id) {
431-
throw new \InvalidArgumentException("chat_id, message_id and text are required for edit");
457+
throw new \InvalidArgumentException("chat_id and message_id are required for edit");
432458
}
433459
$arr = [];
434460
if ($this->builder_text) $arr = array_merge($this->sendEditText(), $arr);
@@ -438,22 +464,39 @@ public function editMessage(): array {
438464
$this->resetBuilder();
439465
return $arr;
440466
}
441-
467+
468+
442469
public function sendDelete(): array
443470
{
444471
if (!$this->builder_chat_id || !$this->builder_message_id) {
445472
throw new \InvalidArgumentException("chat_id and message_id are required for delete");
446473
}
474+
447475
$params = [
448476
'chat_id' => $this->builder_chat_id,
449477
'message_id' => $this->builder_message_id,
450478
];
479+
451480
$res = $this->apiRequest('deleteMessage', $params);
452481
$this->lastResponse = $res;
453482
$this->resetBuilder();
454483
return $res;
455484
}
456-
485+
486+
487+
public function edit(string $message_id): array
488+
{
489+
$this->builder_message_id = $message_id;
490+
return $this->sendEditText();
491+
}
492+
493+
494+
public function delete(string $message_id): array
495+
{
496+
$this->builder_message_id = $message_id;
497+
return $this->sendDelete();
498+
}
499+
457500
private function uploadFileToUrl(string $url, string $file_path): string
458501
{
459502
$mime_type = mime_content_type($file_path);
@@ -482,7 +525,7 @@ private function uploadFileToUrl(string $url, string $file_path): string
482525
}
483526
return $data['data']['file_id'];
484527
}
485-
528+
486529
private function apiRequest(string $method, array $params = []): array
487530
{
488531
$url = $this->baseUrl . $method;
@@ -542,7 +585,7 @@ public function getUpdates(array $data = []): array
542585
{
543586
return $this->apiRequest('getUpdates', $data);
544587
}
545-
588+
546589
public function requestSendFile(string $type): string
547590
{
548591
$validTypes = ['File', 'Image', 'Voice', 'Music', 'Gif', 'Video'];
@@ -610,7 +653,7 @@ public function getUserMessageCount(string $userId): int
610653
{
611654
return $this->userMessageCounters[$userId] ?? 0;
612655
}
613-
656+
614657
public function cleanupSpamData(int $expireTime = 86400): void
615658
{
616659
$now = time();
@@ -667,7 +710,7 @@ public function updateBotEndpoints(string $url, string $type): array
667710
];
668711
return $this->apiRequest('updateBotEndpoints', $data);
669712
}
670-
713+
671714
public function setEndpoint(string $url): array
672715
{
673716
$data = [];
@@ -676,7 +719,7 @@ public function setEndpoint(string $url): array
676719
}
677720
return $data;
678721
}
679-
722+
680723
private function detectFileType(string $mime_type): string
681724
{
682725
$map = [
@@ -704,7 +747,7 @@ private function validateParams(array $params, array $required): void
704747
}
705748
}
706749
}
707-
750+
708751
private function captureUpdate(): void
709752
{
710753
$input = @file_get_contents("php://input");
@@ -719,7 +762,7 @@ public function getUpdate(): array
719762
{
720763
return $this->update;
721764
}
722-
765+
723766
public function onMessage($filter, callable $callback): void
724767
{
725768
if (!($filter instanceof Filters\Filter)) {
@@ -737,7 +780,7 @@ public function run(): void
737780
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
738781
$message = new Message($this->update);
739782
$message->loadChatInfo($this);
740-
783+
741784
$senderId = $message->sender_id;
742785
if ($senderId) {
743786
if ($this->isUserSpamDetected($senderId)) {
@@ -756,7 +799,7 @@ public function run(): void
756799
return;
757800
}
758801
}
759-
802+
760803
foreach ($this->handlers as $handler) {
761804
if ($handler['filter']($this)) {
762805
$handler['callback']($this, $message);
@@ -767,7 +810,7 @@ public function run(): void
767810
if (file_exists($this->hashedToken . '.txt')) {
768811
$offset_id = file_get_contents($this->hashedToken . '.txt');
769812
}
770-
813+
771814
while (true) {
772815
try {
773816
$params = ['limit' => 100];
@@ -790,9 +833,9 @@ public function run(): void
790833
$this->update = ['update' => $update];
791834
$message = new Message($this->update);
792835
$message->loadChatInfo($this);
793-
836+
794837
$this->chat($message->chat_id ?? '');
795-
838+
796839
$senderId = $message->sender_id;
797840
if ($senderId) {
798841
if ($this->isUserSpamDetected($senderId)) {
@@ -826,7 +869,7 @@ public function run(): void
826869
}
827870
}
828871
}
829-
872+
830873
public function getLastResponse(): array
831874
{
832875
return $this->lastResponse;

0 commit comments

Comments
 (0)