1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\TelegramGitNotifier\Structures; |
4
|
|
|
|
5
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\BotException; |
6
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\CallbackException; |
7
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException; |
8
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; |
9
|
|
|
use Exception; |
10
|
|
|
use Telegram; |
11
|
|
|
|
12
|
|
|
trait App |
13
|
|
|
{ |
14
|
|
|
public Telegram $telegram; |
15
|
|
|
|
16
|
|
|
public string $chatBotId; |
17
|
|
|
|
18
|
|
|
public function setCurrentChatBotId(string $chatBotId = null): void |
19
|
|
|
{ |
20
|
|
|
$this->chatBotId = $chatBotId ?? config('telegram-git-notifier.bot.chat_id'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function createTelegramBaseContent(): array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
'chat_id' => $this->chatBotId, |
27
|
|
|
'disable_web_page_preview' => true, |
28
|
|
|
'parse_mode' => 'HTML', |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function sendMessage(?string $message = '', array $options = []): void |
33
|
|
|
{ |
34
|
|
|
if (empty($message)) { |
35
|
|
|
throw MessageIsEmptyException::create(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$content = $this->createTelegramBaseContent(); |
39
|
|
|
$content['text'] = $message; |
40
|
|
|
|
41
|
|
|
if (!empty($options['reply_markup'])) { |
42
|
|
|
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard( |
43
|
|
|
$options['reply_markup'] |
44
|
|
|
); |
45
|
|
|
unset($options['reply_markup']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$content = $content + $options; |
49
|
|
|
|
50
|
|
|
$this->telegram->sendMessage($content); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function sendPhoto(string $photo = '', array $options = []): void |
54
|
|
|
{ |
55
|
|
|
if (empty($photo)) { |
56
|
|
|
throw EntryNotFoundException::fileNotFound(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$content = $this->createTelegramBaseContent(); |
60
|
|
|
$content['photo'] = curl_file_create($photo); |
61
|
|
|
|
62
|
|
|
$content = $content + $options; |
63
|
|
|
|
64
|
|
|
$this->telegram->sendPhoto($content); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function answerCallbackQuery(string $text = null, array $options = []): void |
68
|
|
|
{ |
69
|
|
|
if (empty($text)) { |
70
|
|
|
throw MessageIsEmptyException::create(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$options = array_merge([ |
75
|
|
|
'callback_query_id' => $this->telegram->Callback_ID(), |
76
|
|
|
'text' => $text, |
77
|
|
|
'show_alert' => true, |
78
|
|
|
], $options); |
79
|
|
|
$this->telegram->answerCallbackQuery($options); |
80
|
|
|
} catch (Exception $e) { |
81
|
|
|
throw CallbackException::answer($e); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function editMessageText( |
86
|
|
|
?string $text = null, |
87
|
|
|
array $options = [] |
88
|
|
|
): void { |
89
|
|
|
try { |
90
|
|
|
$content = array_merge([ |
91
|
|
|
'text' => $text ?? $this->getCallbackTextMessage(), |
92
|
|
|
], $this->setCallbackContentMessage($options)); |
93
|
|
|
|
94
|
|
|
$this->telegram->editMessageText($content); |
95
|
|
|
} catch (Exception $e) { |
96
|
|
|
throw BotException::editMessageText($e); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function editMessageReplyMarkup(array $options = []): void |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
$this->telegram->editMessageReplyMarkup( |
104
|
|
|
$this->setCallbackContentMessage($options) |
105
|
|
|
); |
106
|
|
|
} catch (Exception $e) { |
107
|
|
|
throw BotException::editMessageReplyMarkup($e); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getCallbackTextMessage(): string |
112
|
|
|
{ |
113
|
|
|
return $this->telegram->Callback_Message()['text']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function setCallbackContentMessage(array $options = []): array |
117
|
|
|
{ |
118
|
|
|
$content = [ |
119
|
|
|
'chat_id' => $this->telegram->Callback_ChatID(), |
120
|
|
|
'message_id' => $this->telegram->MessageID(), |
121
|
|
|
'disable_web_page_preview' => true, |
122
|
|
|
'parse_mode' => 'HTML', |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
$content['reply_markup'] = $options['reply_markup'] |
126
|
|
|
? $this->telegram->buildInlineKeyBoard($options['reply_markup']) |
127
|
|
|
: null; |
128
|
|
|
|
129
|
|
|
return $content; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getBotName(): ?string |
133
|
|
|
{ |
134
|
|
|
return $this->telegram->getMe()['result']['username'] ?? null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getCommandMessage(): string |
138
|
|
|
{ |
139
|
|
|
$text = $this->telegram->Text(); |
140
|
|
|
|
141
|
|
|
return str_replace('@' . $this->getBotName(), '', $text); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|