Skip to content

Commit 83f8870

Browse files
committed
Add api base uri support for web bridge.
- Revise README with instructions for Proxy or Bridge support. - Remove redundant test. - Fix test typo.
1 parent 802dc7e commit 83f8870

File tree

7 files changed

+54
-32
lines changed

7 files changed

+54
-32
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ Then, configure your Telegram Bot API Token:
5151

5252
```php
5353
// config/services.php
54-
...
5554
'telegram-bot-api' => [
5655
'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE')
5756
],
58-
...
5957
```
6058

59+
#### (Optional) Proxy or Bridge Support
60+
61+
You may not be able to send notifications if Telegram Bot API is not accessible in your country,
62+
you can either set a proxy by following the instructions [here](http://docs.guzzlephp.org/en/stable/quickstart.html#environment-variables) or
63+
use a web bridge by setting the `base_uri` config above with the bridge uri.
64+
6165
## Usage
6266

6367
You can now use the channel in your `via()` method inside the Notification class.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
}
2020
],
2121
"require": {
22-
"php": "^7.1",
22+
"php": ">=7.1",
23+
"ext-json": "*",
2324
"guzzlehttp/guzzle": "^6.2",
2425
"illuminate/notifications": "^5.5 || ^6.0 || ^7.0",
2526
"illuminate/support": "^5.5 || ^6.0 || ^7.0"

src/Exceptions/CouldNotSendNotification.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,4 @@ public static function couldNotCommunicateWithTelegram($message): self
5454
{
5555
return new static("The communication with Telegram failed. `{$message}`");
5656
}
57-
58-
/**
59-
* Thrown when there is no chat id provided.
60-
*
61-
* @return static
62-
*/
63-
public static function chatIdNotProvided(): self
64-
{
65-
return new static('Telegram notification chat ID was not provided. Please refer usage docs.');
66-
}
6757
}

src/Telegram.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/TelegramServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public function boot(): void
2020
->give(static function () {
2121
return new Telegram(
2222
config('services.telegram-bot-api.token'),
23-
new HttpClient()
23+
app(HttpClient::class),
24+
config('services.telegram-bot-api.base_uri')
2425
);
2526
});
2627
}

tests/TelegramChannelTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ public function it_can_send_a_message(): void
5252

5353
self::assertSame($expectedResponse, $actualResponse);
5454
}
55-
56-
/** @test */
57-
public function it_throws_an_exception_when_it_could_not_send_the_notification_because_no_chat_id_provided(): void
58-
{
59-
$this->expectException(CouldNotSendNotification::class);
60-
$this->channel->send(new TestNotifiable(), new TestNotificationNoChatId());
61-
}
6255
}
6356

6457
/**

tests/TelegramMessageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function the_notification_message_can_be_set(): void
4141
}
4242

4343
/** @test */
44-
public function an_inline_button_can_be_added_to_the_messsage(): void
44+
public function an_inline_button_can_be_added_to_the_message(): void
4545
{
4646
$message = new TelegramMessage();
4747
$message->button('Laravel', 'https://laravel.com');

0 commit comments

Comments
 (0)