|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
6
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account; |
|
7
|
|
|
use unreal4u\TelegramAPI\Abstracts\KeyboardMethods; |
|
8
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage; |
|
9
|
|
|
use unreal4u\TelegramAPI\TgLog; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Service which sends simple messages to Telegram users |
|
13
|
|
|
*/ |
|
14
|
|
|
class MessageSender |
|
15
|
|
|
{ |
|
16
|
|
|
const PARSE_PLAIN = ''; |
|
17
|
|
|
const PARSE_MARKDOWN = 'Markdown'; |
|
18
|
|
|
const PARSE_HTML5 = 'HTML'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var TgLog |
|
22
|
|
|
*/ |
|
23
|
|
|
private $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Twig_Environment |
|
27
|
|
|
*/ |
|
28
|
|
|
private $twig; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param TgLog $client |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(TgLog $client, \Twig_Environment $twig) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->client = $client; |
|
36
|
|
|
$this->twig = $twig; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Account[] $accounts |
|
41
|
|
|
* @param string $template |
|
42
|
|
|
* @param array $templateData |
|
43
|
|
|
* @param KeyboardMethods|null $keyboardMarkup |
|
44
|
|
|
* @param bool $disableWebPreview |
|
45
|
|
|
* @param bool $disableNotifications |
|
46
|
|
|
* @param string $parseMode |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function sendMassTemplatedMessage( |
|
|
|
|
|
|
49
|
|
|
array $accounts, |
|
50
|
|
|
string $template, |
|
51
|
|
|
array $templateData = [], |
|
52
|
|
|
KeyboardMethods $keyboardMarkup = null, |
|
53
|
|
|
bool $disableWebPreview = true, |
|
54
|
|
|
bool $disableNotifications = false, |
|
55
|
|
|
string $parseMode = self::PARSE_MARKDOWN |
|
56
|
|
|
) { |
|
57
|
|
|
$text = $this->twig->render($template, $templateData); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($accounts as $account) { |
|
60
|
|
|
$this->sendMessage($account, $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
public function sendTemplatedMessage( |
|
|
|
|
|
|
65
|
|
|
Account $account, |
|
66
|
|
|
string $template, |
|
67
|
|
|
array $templateData = [], |
|
68
|
|
|
KeyboardMethods $keyboardMarkup = null, |
|
69
|
|
|
bool $disableWebPreview = true, |
|
70
|
|
|
bool $disableNotifications = false, |
|
71
|
|
|
string $parseMode = self::PARSE_MARKDOWN |
|
72
|
|
|
): bool { |
|
73
|
|
|
$text = $this->twig->render($template, $templateData); |
|
74
|
|
|
|
|
75
|
|
|
return $this->sendMessage($account, $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function sendMessage( |
|
79
|
|
|
Account $account, |
|
80
|
|
|
string $text, |
|
81
|
|
|
string $parseMode = self::PARSE_PLAIN, |
|
82
|
|
|
KeyboardMethods $keyboardMarkup = null, |
|
83
|
|
|
bool $disableWebPreview = false, |
|
84
|
|
|
bool $disableNotifications = false |
|
85
|
|
|
): bool { |
|
86
|
|
|
return $this->sendMessageToChat($account->getChatId(), $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function sendMessageToChat( |
|
90
|
|
|
int $chatId, |
|
91
|
|
|
string $text, |
|
92
|
|
|
string $parseMode = self::PARSE_PLAIN, |
|
93
|
|
|
KeyboardMethods $keyboardMarkup = null, |
|
94
|
|
|
bool $disableWebPreview = false, |
|
95
|
|
|
bool $disableNotifications = false |
|
96
|
|
|
): bool { |
|
97
|
|
|
$sendMessage = new SendMessage(); |
|
98
|
|
|
$sendMessage->chat_id = (string)$chatId; |
|
99
|
|
|
$sendMessage->text = $text; |
|
100
|
|
|
$sendMessage->parse_mode = $parseMode; |
|
101
|
|
|
$sendMessage->disable_web_page_preview = $disableWebPreview; |
|
102
|
|
|
$sendMessage->disable_notification = $disableNotifications; |
|
103
|
|
|
$sendMessage->reply_markup = $keyboardMarkup; |
|
104
|
|
|
|
|
105
|
|
|
try { |
|
106
|
|
|
$this->client->performApiRequest($sendMessage); |
|
107
|
|
|
|
|
108
|
|
|
return true; |
|
109
|
|
|
} catch (ClientException $e) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.