|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Channels\Telegram; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Contracts\Channels\User; |
|
8
|
|
|
use FondBot\Conversation\Buttons\UrlButton; |
|
9
|
|
|
use FondBot\Contracts\Conversation\Keyboard; |
|
10
|
|
|
use FondBot\Contracts\Channels\OutgoingMessage; |
|
11
|
|
|
use FondBot\Conversation\Buttons\PayloadButton; |
|
12
|
|
|
use FondBot\Channels\Telegram\Buttons\RequestContactButton; |
|
13
|
|
|
|
|
14
|
|
|
class TelegramOutgoingMessage implements OutgoingMessage |
|
15
|
|
|
{ |
|
16
|
|
|
private const KEYBOARD_REPLY = 'keyboard'; |
|
17
|
|
|
private const KEYBOARD_INLINE = 'inline_keyboard'; |
|
18
|
|
|
|
|
19
|
|
|
private $recipient; |
|
20
|
|
|
private $text; |
|
21
|
|
|
private $keyboard; |
|
22
|
|
|
|
|
23
|
9 |
|
public function __construct(User $recipient, string $text, Keyboard $keyboard = null) |
|
24
|
|
|
{ |
|
25
|
9 |
|
$this->recipient = $recipient; |
|
26
|
9 |
|
$this->text = $text; |
|
27
|
9 |
|
$this->keyboard = $keyboard; |
|
28
|
9 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get recipient. |
|
32
|
|
|
* |
|
33
|
|
|
* @return User |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function getRecipient(): User |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $this->recipient; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get message text. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function getText(): string |
|
46
|
|
|
{ |
|
47
|
2 |
|
return $this->text; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get keyboard. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Keyboard|null |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function getKeyboard(): ?Keyboard |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->keyboard; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the instance as an array. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
6 |
|
public function toArray(): array |
|
66
|
|
|
{ |
|
67
|
|
|
$payload = [ |
|
68
|
6 |
|
'chat_id' => $this->recipient->getId(), |
|
69
|
6 |
|
'text' => $this->text, |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
6 |
|
if ($replyMarkup = $this->getReplyMarkup()) { |
|
|
|
|
|
|
73
|
3 |
|
$payload['reply_markup'] = json_encode($this->getReplyMarkup()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
return $payload; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
private function getReplyMarkup(): ?array |
|
80
|
|
|
{ |
|
81
|
6 |
|
if ($this->keyboard !== null) { |
|
82
|
3 |
|
$type = $this->detectKeyboardType(); |
|
83
|
|
|
|
|
84
|
3 |
|
$keyboard = []; |
|
85
|
|
|
switch ($type) { |
|
86
|
3 |
|
case self::KEYBOARD_REPLY: |
|
|
|
|
|
|
87
|
2 |
|
$keyboard = $this->compileReplyKeyboard(); |
|
88
|
2 |
|
break; |
|
89
|
1 |
|
case self::KEYBOARD_INLINE: |
|
90
|
1 |
|
$keyboard = $this->compileInlineKeyboard(); |
|
91
|
1 |
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
return $keyboard; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Compile reply keyboard markup. |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
2 |
|
private function compileReplyKeyboard(): array |
|
106
|
|
|
{ |
|
107
|
2 |
|
$buttons = []; |
|
108
|
2 |
|
foreach ($this->keyboard->getButtons() as $button) { |
|
109
|
2 |
|
$parameters = ['text' => $button->getLabel()]; |
|
110
|
|
|
|
|
111
|
2 |
|
if ($button instanceof RequestContactButton) { |
|
112
|
1 |
|
$parameters['request_contact'] = true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
$buttons[] = $parameters; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return [ |
|
119
|
2 |
|
'keyboard' => [$buttons], |
|
120
|
|
|
'one_time_keyboard' => true, |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Compile inline keyboard markup. |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
1 |
|
private function compileInlineKeyboard(): array |
|
130
|
|
|
{ |
|
131
|
1 |
|
$buttons = []; |
|
132
|
1 |
|
foreach ($this->keyboard->getButtons() as $button) { |
|
133
|
1 |
|
$parameters = ['text' => $button->getLabel()]; |
|
134
|
|
|
|
|
135
|
1 |
|
if ($button instanceof UrlButton) { |
|
136
|
1 |
|
$parameters['url'] = $button->getUrl(); |
|
137
|
|
|
} elseif ($button instanceof PayloadButton) { |
|
138
|
1 |
|
$parameters['callback_data'] = $button->getPayload(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
$buttons[] = $parameters; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return [ |
|
145
|
1 |
|
'inline_keyboard' => [$buttons], |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
3 |
|
private function detectKeyboardType(): string |
|
150
|
|
|
{ |
|
151
|
3 |
|
$button = collect($this->keyboard->getButtons())->first(); |
|
152
|
|
|
|
|
153
|
3 |
|
if ($button instanceof PayloadButton || $button instanceof UrlButton) { |
|
154
|
1 |
|
return self::KEYBOARD_INLINE; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
return self::KEYBOARD_REPLY; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.