Skip to content

Commit 191f0df

Browse files
committed
Revise and improve
1 parent 175db47 commit 191f0df

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function toTelegram($notifiable)
103103
->content('Awesome *bold* text and [inline URL](http://www.example.com/)')
104104
->file('/storage/archive/6029014.jpg', 'photo'); // local photo
105105

106-
// OR using a helper method with remote file.
106+
// OR using a helper method with or without a remote file.
107107
// ->photo('https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_1MB.jpg');
108108
}
109109
```
@@ -220,13 +220,13 @@ public function routeNotificationForTelegram()
220220
- `to($chatId)`: (integer) Recipient's chat id.
221221
- `content('')`: (string) File caption, supports markdown. For more information on supported markdown styles, check out these [docs](https://telegram-bot-sdk.readme.io/reference#section-formatting-options).
222222
- `file($file, $type, $filename = null)`: Local file path or remote URL, `$type` of the file (Ex:`photo`, `audio`, `document`, `video`, `animation`, `voice`, `video_note_`) and optionally filename with extension. Ex: `sample.pdf`. You can use helper methods instead of using this to make it easier to work with file attachment.
223-
- `photo($file, $filename = null)`: Helper method to attach a photo.
224-
- `audio($file, $filename = null)`: Helper method to attach an audio file (MP3 file).
223+
- `photo($file)`: Helper method to attach a photo.
224+
- `audio($file)`: Helper method to attach an audio file (MP3 file).
225225
- `document($file, $filename = null)`: Helper method to attach a document or any file as document.
226-
- `video($file, $filename = null)`: Helper method to attach a video file.
227-
- `animation($file, $filename = null)`: Helper method to attach an animated gif file.
228-
- `voice($file, $filename = null)`: Helper method to attach a voice note (`.ogg` file with OPUS encoded).
229-
- `videoNote($file, $filename = null)`: Helper method to attach a video note file (Upto 1 min long, rounded square video).
226+
- `video($file)`: Helper method to attach a video file.
227+
- `animation($file)`: Helper method to attach an animated gif file.
228+
- `voice($file)`: Helper method to attach a voice note (`.ogg` file with OPUS encoded).
229+
- `videoNote($file)`: Helper method to attach a video note file (Upto 1 min long, rounded square video).
230230
- `button($text, $url)`: (string) Adds an inline "Call to Action" button. You can add as many as you want and they'll be placed 2 in a row.
231231
- `disableNotification()`: Send the message silently. Users will receive a notification with no sound.
232232
- `options([])`: (array) Allows you to add additional or override the payload.

src/TelegramChannel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function send($notifiable, Notification $notification): void
5656
} elseif ($message instanceof TelegramLocation) {
5757
$this->telegram->sendLocation($params);
5858
} elseif ($message instanceof TelegramFile) {
59-
$this->telegram->sendFile($message->toMultipart(), $message->type, true);
59+
$this->telegram->sendFile($params, $message->type, $message->hasFile());
6060
}
6161
}
6262
}

src/TelegramFile.php

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ public function file($file, string $type, string $filename = null): self
6565
{
6666
$this->type = $type;
6767

68-
$this->payload['file'] = [
69-
'name' => $type,
70-
'contents' => (is_file($file) && is_readable($file)) ? fopen($file, 'rb') : $file,
71-
];
72-
73-
if ($filename !== null) {
74-
$this->payload['file']['filename'] = $filename;
68+
$isLocalFile = $this->isReadableFile($file);
69+
70+
if($filename !== null || $isLocalFile) {
71+
$this->payload['file'] = [
72+
'filename' => $filename,
73+
'name' => $type,
74+
'contents' => $isLocalFile ? fopen($file, 'rb') : $file,
75+
];
76+
} else {
77+
$this->payload[$type] = $file;
7578
}
7679

7780
return $this;
@@ -82,14 +85,13 @@ public function file($file, string $type, string $filename = null): self
8285
*
8386
* Use this method to send photos.
8487
*
85-
* @param string $file
86-
* @param string|null $filename
88+
* @param string $file
8789
*
8890
* @return $this
8991
*/
90-
public function photo(string $file, string $filename = null): self
92+
public function photo(string $file): self
9193
{
92-
return $this->file($file, 'photo', $filename);
94+
return $this->file($file, 'photo');
9395
}
9496

9597
/**
@@ -98,14 +100,13 @@ public function photo(string $file, string $filename = null): self
98100
* Use this method to send audio files, if you want Telegram clients to display them in the music player.
99101
* Your audio must be in the .mp3 format.
100102
*
101-
* @param string $file
102-
* @param string|null $filename
103+
* @param string $file
103104
*
104105
* @return $this
105106
*/
106-
public function audio(string $file, string $filename = null): self
107+
public function audio(string $file): self
107108
{
108-
return $this->file($file, 'audio', $filename);
109+
return $this->file($file, 'audio');
109110
}
110111

111112
/**
@@ -128,29 +129,27 @@ public function document(string $file, string $filename = null): self
128129
*
129130
* Use this method to send video files, Telegram clients support mp4 videos.
130131
*
131-
* @param string $file
132-
* @param string|null $filename
132+
* @param string $file
133133
*
134134
* @return $this
135135
*/
136-
public function video(string $file, string $filename = null): self
136+
public function video(string $file): self
137137
{
138-
return $this->file($file, 'video', $filename);
138+
return $this->file($file, 'video');
139139
}
140140

141141
/**
142142
* Attach an animation file.
143143
*
144144
* Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound).
145145
*
146-
* @param string $file
147-
* @param string|null $filename
146+
* @param string $file
148147
*
149148
* @return $this
150149
*/
151-
public function animation(string $file, string $filename = null): self
150+
public function animation(string $file): self
152151
{
153-
return $this->file($file, 'animation', $filename);
152+
return $this->file($file, 'animation');
154153
}
155154

156155
/**
@@ -159,14 +158,13 @@ public function animation(string $file, string $filename = null): self
159158
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice
160159
* message. For this to work, your audio must be in an .ogg file encoded with OPUS.
161160
*
162-
* @param string $file
163-
* @param string|null $filename
161+
* @param string $file
164162
*
165163
* @return $this
166164
*/
167-
public function voice(string $file, string $filename = null): self
165+
public function voice(string $file): self
168166
{
169-
return $this->file($file, 'voice', $filename);
167+
return $this->file($file, 'voice');
170168
}
171169

172170
/**
@@ -175,14 +173,33 @@ public function voice(string $file, string $filename = null): self
175173
* Telegram clients support rounded square mp4 videos of up to 1 minute long.
176174
* Use this method to send video messages.
177175
*
178-
* @param string $file
179-
* @param string|null $filename
176+
* @param string $file
180177
*
181178
* @return $this
182179
*/
183-
public function videoNote(string $file, string $filename = null): self
180+
public function videoNote(string $file): self
184181
{
185-
return $this->file($file, 'video_note', $filename);
182+
return $this->file($file, 'video_note');
183+
}
184+
185+
/**
186+
* Determine there is a file.
187+
*
188+
* @return bool
189+
*/
190+
public function hasFile(): bool
191+
{
192+
return isset($this->payload['file']);
193+
}
194+
195+
/**
196+
* Returns params payload.
197+
*
198+
* @return array
199+
*/
200+
public function toArray(): array
201+
{
202+
return $this->hasFile() ? $this->toMultipart() : $this->payload;
186203
}
187204

188205
/**
@@ -199,4 +216,16 @@ public function toMultipart(): array
199216

200217
return $data;
201218
}
219+
220+
/**
221+
* Determine if it's a regular and readable file.
222+
*
223+
* @param string $file
224+
*
225+
* @return bool
226+
*/
227+
protected function isReadableFile(string $file): bool
228+
{
229+
return is_file($file) && is_readable($file);
230+
}
202231
}

0 commit comments

Comments
 (0)