|
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\Telegram\Methods\SendMessage; |
|
8
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup; |
|
9
|
|
|
use unreal4u\TelegramAPI\TgLog; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Service which sends simple messages to Telegram users |
|
13
|
|
|
*/ |
|
14
|
|
|
class MessageSender |
|
15
|
|
|
{ |
|
16
|
|
|
const PARSE_MODE_NOPARSE = ''; |
|
17
|
|
|
const PARSE_MODE_MARKDOWN = 'Markdown'; |
|
18
|
|
|
const PARSE_MODE_HTML5 = 'HTML'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var TgLog |
|
22
|
|
|
*/ |
|
23
|
|
|
private $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param TgLog $client |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(TgLog $client) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->client = $client; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function sendMessageToUser( |
|
34
|
|
|
Account $account, |
|
35
|
|
|
string $text, |
|
36
|
|
|
string $parseMode = self::PARSE_MODE_NOPARSE, |
|
37
|
|
|
ReplyKeyboardMarkup $keyboardMarkup = null, |
|
38
|
|
|
bool $disableWebPreview = false, |
|
39
|
|
|
$disableNotifications = false |
|
40
|
|
|
): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->sendMessageToChat($account->getChatId(), $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function sendMessageToChat( |
|
46
|
|
|
int $chatId, |
|
47
|
|
|
string $text, |
|
48
|
|
|
string $parseMode = self::PARSE_MODE_NOPARSE, |
|
|
|
|
|
|
49
|
|
|
ReplyKeyboardMarkup $keyboardMarkup = null, |
|
|
|
|
|
|
50
|
|
|
bool $disableWebPreview = false, |
|
|
|
|
|
|
51
|
|
|
$disableNotifications = false |
|
|
|
|
|
|
52
|
|
|
): bool |
|
53
|
|
|
{ |
|
54
|
|
|
$sendMessage = new SendMessage(); |
|
55
|
|
|
$sendMessage->chat_id = (string) $chatId; |
|
56
|
|
|
$sendMessage->text = $text; |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
$this->client->performApiRequest($sendMessage); |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} catch (ClientException $e) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.