Skip to content

Commit 60448b4

Browse files
author
darkdarin
committed
fix: correct way to multibot configuration
1 parent 6c367a0 commit 60448b4

File tree

4 files changed

+61
-59
lines changed

4 files changed

+61
-59
lines changed

src/Telegram.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace DarkDarin\TelegramBotSdk;
44

5+
use DarkDarin\Serializer\ApiSerializer\ApiSerializerInterface;
56
use DarkDarin\TelegramBotSdk\Exceptions\MisconfiguredClientException;
7+
use DarkDarin\TelegramBotSdk\TransportClient\TransportClientInterface;
68
use Psr\Container\ContainerExceptionInterface;
79
use Psr\Container\ContainerInterface;
810
use Psr\Container\NotFoundExceptionInterface;
@@ -26,6 +28,9 @@ public function __construct(
2628
private readonly ContainerInterface $container,
2729
) {
2830
foreach ($bots as $botName => $config) {
31+
if (empty($config['token'])) {
32+
throw new MisconfiguredClientException(sprintf('Need set token in configuration for bot [%s]', $botName));
33+
}
2934
$this->clients[$botName] = $this->makeClientInstance($config['token']);
3035
}
3136
}
@@ -65,13 +70,12 @@ private function getClientInstance(?string $botName = null): TelegramClient
6570
* @throws ContainerExceptionInterface
6671
* @throws NotFoundExceptionInterface
6772
*/
68-
private function makeClientInstance(string $token = null): TelegramClient
73+
protected function makeClientInstance(string $token): TelegramClient
6974
{
70-
$client = $this->container->get(TelegramClient::class);
71-
if ($token !== null) {
72-
$client->setToken($token);
73-
}
74-
75-
return $client;
75+
return new TelegramClient(
76+
$this->container->get(TransportClientInterface::class),
77+
$this->container->get(ApiSerializerInterface::class),
78+
$token
79+
);
7680
}
7781
}

src/TelegramClient.php

+31-34
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@
3535
public function __construct(
3636
private TransportClientInterface $client,
3737
private ApiSerializerInterface $serializer,
38-
) {}
39-
40-
public function setToken(string $token): void
41-
{
42-
$this->client->setToken($token);
38+
private string $token,
39+
) {
4340
}
4441

4542
public function getWebhookUpdate(string $requestBody): Update
@@ -81,7 +78,7 @@ public function getUpdates(
8178
int $timeout = null,
8279
array $allowed_updates = []
8380
): array {
84-
return $this->client->executeMethod(__METHOD__, func_get_args(), Update::class . '[]');
81+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Update::class . '[]');
8582
}
8683

8784
/**
@@ -112,7 +109,7 @@ public function setWebhook(
112109
bool $drop_pending_updates = null,
113110
string $secret_token = null,
114111
): bool {
115-
return $this->client->executeMethod(__METHOD__, func_get_args(), multipartField: 'certificate');
112+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), multipartField: 'certificate');
116113
}
117114

118115
/**
@@ -125,7 +122,7 @@ public function setWebhook(
125122
*/
126123
public function deleteWebhook(bool $drop_pending_updates = null): bool
127124
{
128-
return $this->client->executeMethod(__METHOD__, func_get_args());
125+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args());
129126
}
130127

131128
/**
@@ -136,7 +133,7 @@ public function deleteWebhook(bool $drop_pending_updates = null): bool
136133
*/
137134
public function getWebhookInfo(): WebhookInfo
138135
{
139-
return $this->client->executeMethod(__METHOD__, func_get_args(), WebhookInfo::class);
136+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), WebhookInfo::class);
140137
}
141138

142139
/**
@@ -146,7 +143,7 @@ public function getWebhookInfo(): WebhookInfo
146143
*/
147144
public function getMe(): User
148145
{
149-
return $this->client->executeMethod(__METHOD__, func_get_args(), User::class);
146+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), User::class);
150147
}
151148

152149
/**
@@ -180,7 +177,7 @@ public function sendMessage(
180177
bool $allow_sending_without_reply = null,
181178
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
182179
): Message {
183-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
180+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
184181
}
185182

186183
/**
@@ -204,7 +201,7 @@ public function forwardMessage(
204201
bool $disable_notification = null,
205202
bool $protect_content = null,
206203
): Message {
207-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
204+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
208205
}
209206

210207
/**
@@ -242,7 +239,7 @@ public function copyMessage(
242239
bool $allow_sending_without_reply = null,
243240
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
244241
): MessageId {
245-
return $this->client->executeMethod(__METHOD__, func_get_args(), MessageId::class);
242+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), MessageId::class);
246243
}
247244

248245
/**
@@ -279,7 +276,7 @@ public function sendPhoto(
279276
bool $allow_sending_without_reply = null,
280277
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
281278
): Message {
282-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'photo');
279+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'photo');
283280
}
284281

285282
/**
@@ -324,7 +321,7 @@ public function sendAudio(
324321
bool $allow_sending_without_reply = null,
325322
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
326323
): Message {
327-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'audio');
324+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'audio');
328325
}
329326

330327
/**
@@ -364,7 +361,7 @@ public function sendDocument(
364361
bool $allow_sending_without_reply = null,
365362
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
366363
): Message {
367-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'document');
364+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'document');
368365
}
369366

370367
/**
@@ -412,7 +409,7 @@ public function sendVideo(
412409
bool $allow_sending_without_reply = null,
413410
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
414411
): Message {
415-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'video');
412+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'video');
416413
}
417414

418415
/**
@@ -458,7 +455,7 @@ public function sendAnimation(
458455
bool $allow_sending_without_reply = null,
459456
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
460457
): Message {
461-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'animation');
458+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'animation');
462459
}
463460

464461
/**
@@ -497,7 +494,7 @@ public function sendVoice(
497494
bool $allow_sending_without_reply = null,
498495
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
499496
): Message {
500-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'voice');
497+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'voice');
501498
}
502499

503500
/**
@@ -533,7 +530,7 @@ public function sendVideoNote(
533530
bool $allow_sending_without_reply = null,
534531
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
535532
): Message {
536-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class, 'video_note');
533+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class, 'video_note');
537534
}
538535

539536
/**
@@ -561,7 +558,7 @@ public function sendMediaGroup(
561558
int $reply_to_message_id = null,
562559
bool $allow_sending_without_reply = null,
563560
): array {
564-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class . '[]', true);
561+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class . '[]', true);
565562
}
566563

567564
/**
@@ -600,7 +597,7 @@ public function sendLocation(
600597
bool $allow_sending_without_reply = null,
601598
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
602599
): Message {
603-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
600+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
604601
}
605602

606603
/**
@@ -643,7 +640,7 @@ public function sendVenue(
643640
bool $allow_sending_without_reply = null,
644641
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
645642
): Message {
646-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
643+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
647644
}
648645

649646
/**
@@ -678,7 +675,7 @@ public function sendContact(
678675
bool $allow_sending_without_reply = null,
679676
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
680677
): Message {
681-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
678+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
682679
}
683680

684681
/**
@@ -729,7 +726,7 @@ public function sendPoll(
729726
bool $allow_sending_without_reply = null,
730727
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
731728
): Message {
732-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
729+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
733730
}
734731

735732
/**
@@ -758,7 +755,7 @@ public function sendDice(
758755
bool $allow_sending_without_reply = null,
759756
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply $reply_markup = null,
760757
): Message {
761-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
758+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
762759
}
763760

764761
/**
@@ -778,7 +775,7 @@ public function sendChatAction(
778775
ChatActionEnum $action,
779776
int $message_thread_id = null,
780777
): bool {
781-
return $this->client->executeMethod(__METHOD__, func_get_args());
778+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args());
782779
}
783780

784781
/**
@@ -807,7 +804,7 @@ public function editMessageText(
807804
bool $disable_web_page_preview = null,
808805
InlineKeyboardMarkup $reply_markup = null,
809806
): Message|bool {
810-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
807+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
811808
}
812809

813810
/**
@@ -834,7 +831,7 @@ public function editMessageCaption(
834831
array $caption_entities = null,
835832
InlineKeyboardMarkup $reply_markup = null,
836833
): Message|bool {
837-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
834+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
838835
}
839836

840837
/**
@@ -866,7 +863,7 @@ public function editMessageLiveLocation(
866863
int $proximity_alert_radius = null,
867864
InlineKeyboardMarkup $reply_markup = null,
868865
): Message|bool {
869-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
866+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
870867
}
871868

872869
/**
@@ -887,7 +884,7 @@ public function stopMessageLiveLocation(
887884
string $inline_message_id = null,
888885
InlineKeyboardMarkup $reply_markup = null,
889886
): Message|bool {
890-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
887+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
891888
}
892889

893890
/**
@@ -908,7 +905,7 @@ public function editMessageReplyMarkup(
908905
int $inline_message_id = null,
909906
InlineKeyboardMarkup $reply_markup = null,
910907
): Message|bool {
911-
return $this->client->executeMethod(__METHOD__, func_get_args(), Message::class);
908+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Message::class);
912909
}
913910

914911
/**
@@ -927,7 +924,7 @@ public function stopPoll(
927924
int $message_id,
928925
InlineKeyboardMarkup $reply_markup = null,
929926
): Poll {
930-
return $this->client->executeMethod(__METHOD__, func_get_args(), Poll::class);
927+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args(), Poll::class);
931928
}
932929

933930
/**
@@ -952,6 +949,6 @@ public function deleteMessage(
952949
int|string $chat_id,
953950
int $message_id,
954951
): bool {
955-
return $this->client->executeMethod(__METHOD__, func_get_args());
952+
return $this->client->executeMethod($this->token, __METHOD__, func_get_args());
956953
}
957954
}

0 commit comments

Comments
 (0)