@@ -17,17 +17,22 @@ class Telegram
1717 /** @var HttpClient HTTP Client */
1818 protected $ http ;
1919
20- /** @var null| string Telegram Bot API Token. */
20+ /** @var string|null Telegram Bot API Token. */
2121 protected $ token ;
2222
23+ /** @var string Telegram Bot API Base URI */
24+ protected $ apiBaseUri ;
25+
2326 /**
24- * @param null $token
27+ * @param string| null $token
2528 * @param HttpClient|null $httpClient
29+ * @param string|null $apiBaseUri
2630 */
27- public function __construct ($ token = null , HttpClient $ httpClient = null )
31+ public function __construct ($ token = null , HttpClient $ httpClient = null , $ apiBaseUri = null )
2832 {
2933 $ this ->token = $ token ;
30- $ this ->http = $ httpClient ;
34+ $ this ->http = $ httpClient ?? new HttpClient ();
35+ $ this ->setApiBaseUri ($ apiBaseUri ?? 'https://api.telegram.org ' );
3136 }
3237
3338 /**
@@ -43,11 +48,39 @@ public function getToken(): string
4348 /**
4449 * Token setter.
4550 *
46- * @param string
51+ * @param string $token
52+ *
53+ * @return $this
4754 */
48- public function setToken ($ token ): void
55+ public function setToken (string $ token ): self
4956 {
5057 $ this ->token = $ token ;
58+
59+ return $ this ;
60+ }
61+
62+ /**
63+ * API Base URI getter.
64+ *
65+ * @return string
66+ */
67+ public function getApiBaseUri (): string
68+ {
69+ return $ this ->apiBaseUri ;
70+ }
71+
72+ /**
73+ * API Base URI setter.
74+ *
75+ * @param string $apiBaseUri
76+ *
77+ * @return $this
78+ */
79+ public function setApiBaseUri (string $ apiBaseUri ): self
80+ {
81+ $ this ->apiBaseUri = rtrim ($ apiBaseUri , '/ ' );
82+
83+ return $ this ;
5184 }
5285
5386 /**
@@ -57,7 +90,7 @@ public function setToken($token): void
5790 */
5891 protected function httpClient (): HttpClient
5992 {
60- return $ this ->http ?? new HttpClient () ;
93+ return $ this ->http ;
6194 }
6295
6396 /**
@@ -101,7 +134,7 @@ public function sendMessage(array $params): ?ResponseInterface
101134 */
102135 public function sendFile (array $ params , string $ type , bool $ multipart = false ): ?ResponseInterface
103136 {
104- return $ this ->sendRequest ('send ' . Str::studly ($ type ), $ params , $ multipart );
137+ return $ this ->sendRequest ('send ' . Str::studly ($ type ), $ params , $ multipart );
105138 }
106139
107140 /**
@@ -135,10 +168,10 @@ protected function sendRequest(string $endpoint, array $params, bool $multipart
135168 throw CouldNotSendNotification::telegramBotTokenNotProvided ('You must provide your telegram bot token to make any API requests. ' );
136169 }
137170
138- $ endPointUrl = ' https://api.telegram.org/ bot' . $ this ->token . ' / ' . $ endpoint ;
171+ $ apiUri = sprintf ( ' %s/ bot%s/%s ' , $ this ->apiBaseUri , $ this -> token , $ endpoint) ;
139172
140173 try {
141- return $ this ->httpClient ()->post ($ endPointUrl , [
174+ return $ this ->httpClient ()->post ($ apiUri , [
142175 $ multipart ? 'multipart ' : 'form_params ' => $ params ,
143176 ]);
144177 } catch (ClientException $ exception ) {
0 commit comments