Skip to content

Commit 8bacfab

Browse files
committed
Refactor.
1 parent 5e9371b commit 8bacfab

File tree

2 files changed

+56
-76
lines changed

2 files changed

+56
-76
lines changed

src/TelegramChannel.php

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,41 @@ public function __construct(Telegram $telegram)
3131
*/
3232
public function send($notifiable, Notification $notification)
3333
{
34+
if (!$this->shouldSendMessage($notifiable, $notification)) {
35+
return;
36+
}
37+
3438
$message = $notification->toTelegram($notifiable);
3539

3640
if (is_string($message)) {
37-
$message = new TelegramMessage($message);
41+
$message = TelegramMessage::create($message);
3842
}
3943

40-
if (!$chatId = $this->chatId($message, $notifiable)) {
41-
return;
42-
}
44+
if ($message->toNotGiven()) {
45+
if (!$to = $notifiable->routeNotificationFor('telegram')) {
46+
return;
47+
}
4348

44-
$shouldSendMessage = event(new SendingMessage($notifiable, $notification), [], true) !== false;
45-
46-
if (!$shouldSendMessage) {
47-
return;
49+
$message->to($to);
4850
}
4951

50-
$params = array_merge([
51-
'chat_id' => $chatId,
52-
'text' => trim($message->content),
53-
'parse_mode' => 'Markdown',
54-
'reply_markup' => $this->getReplyMarkup($message),
55-
], $message->options);
52+
$params = $message->toArray();
5653

5754
$this->telegram->sendMessage($params);
5855

5956
event(new MessageWasSent($notifiable, $notification));
6057
}
6158

6259
/**
63-
* @param TelegramMessage $message
64-
* @param $notifiable
60+
* Check if we can send the notification.
6561
*
66-
* @return mixed
67-
*/
68-
protected function chatId(TelegramMessage $message, $notifiable)
69-
{
70-
return $message->chatId ?: $notifiable->routeNotificationFor('telegram') ?: null;
71-
}
72-
73-
/**
74-
* Get Reply Markup (Inline Keyboard).
75-
*
76-
* @param TelegramMessage $message
62+
* @param $notifiable
63+
* @param Notification $notification
7764
*
78-
* @return $this|void
65+
* @return bool
7966
*/
80-
protected function getReplyMarkup(TelegramMessage $message)
67+
protected function shouldSendMessage($notifiable, Notification $notification)
8168
{
82-
if (!$message->actionText) {
83-
return;
84-
}
85-
86-
return (new Telegram())
87-
->buttons([
88-
'text' => $message->actionText,
89-
'url' => $message->actionUrl,
90-
])
91-
->getKeyboardMarkup();
69+
return event(new SendingMessage($notifiable, $notification), [], true) !== false;
9270
}
9371
}

src/TelegramMessage.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,14 @@
55
class TelegramMessage
66
{
77
/**
8-
* The Telegram Chat ID the message should be sent to.
9-
*
10-
* @var string
11-
*/
12-
public $chatId;
13-
14-
/**
15-
* The message content.
16-
*
17-
* @var string
8+
* @var array Params payload.
189
*/
19-
public $content;
10+
public $payload = [];
2011

2112
/**
22-
* The text / label for the action.
23-
*
24-
* @var string
13+
* @var array Inline Keyboard Buttons.
2514
*/
26-
public $actionText;
27-
28-
/**
29-
* The action URL.
30-
*
31-
* @var string
32-
*/
33-
public $actionUrl;
34-
35-
/**
36-
* Additional options to be passed to the sendMessage method.
37-
*
38-
* @var array
39-
*/
40-
public $options = [];
15+
protected $buttons = [];
4116

4217
/**
4318
* @param string $content
@@ -56,45 +31,52 @@ public static function create($content = '')
5631
*/
5732
public function __construct($content = '')
5833
{
59-
$this->content = $content;
34+
$this->content($content);
35+
$this->payload['parse_mode'] = 'Markdown';
6036
}
6137

6238
/**
39+
* Recipient's Chat ID.
40+
*
6341
* @param $chatId
6442
*
6543
* @return $this
6644
*/
6745
public function to($chatId)
6846
{
69-
$this->chatId = $chatId;
47+
$this->payload['chat_id'] = $chatId;
7048

7149
return $this;
7250
}
7351

7452
/**
53+
* Notification message (Supports Markdown).
54+
*
7555
* @param $content
7656
*
7757
* @return $this
7858
*/
7959
public function content($content)
8060
{
81-
$this->content = $content;
61+
$this->payload['text'] = $content;
8262

8363
return $this;
8464
}
8565

8666
/**
87-
* Configure the "call to action" button.
67+
* Add an inline button.
8868
*
8969
* @param string $text
9070
* @param string $url
9171
*
9272
* @return $this
9373
*/
94-
public function action($text, $url)
74+
public function button($text, $url)
9575
{
96-
$this->actionText = $text;
97-
$this->actionUrl = $url;
76+
$this->buttons[] = compact('text', 'url');
77+
78+
$replyMarkup['inline_keyboard'] = array_chunk($this->buttons, 2);
79+
$this->payload['reply_markup'] = json_encode($replyMarkup);
9880

9981
return $this;
10082
}
@@ -108,8 +90,28 @@ public function action($text, $url)
10890
*/
10991
public function options(array $options)
11092
{
111-
$this->options = $options;
93+
$this->payload = array_merge($this->payload, $options);
11294

11395
return $this;
11496
}
97+
98+
/**
99+
* Determine if chat id is not given.
100+
*
101+
* @return bool
102+
*/
103+
public function toNotGiven()
104+
{
105+
return !isset($this->payload['chat_id']);
106+
}
107+
108+
/**
109+
* Returns params payload.
110+
*
111+
* @return array
112+
*/
113+
public function toArray()
114+
{
115+
return $this->payload;
116+
}
115117
}

0 commit comments

Comments
 (0)