2017-01-06 17:18:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram;
|
|
|
|
|
|
|
|
use GuzzleHttp\Exception\ClientException;
|
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
|
2017-01-06 19:19:44 +00:00
|
|
|
use unreal4u\TelegramAPI\Abstracts\KeyboardMethods;
|
2017-01-06 17:18:39 +00:00
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage;
|
|
|
|
use unreal4u\TelegramAPI\TgLog;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Service which sends simple messages to Telegram users
|
|
|
|
*/
|
|
|
|
class MessageSender
|
|
|
|
{
|
2017-01-06 21:38:20 +00:00
|
|
|
const PARSE_PLAIN = '';
|
|
|
|
const PARSE_MARKDOWN = 'Markdown';
|
|
|
|
const PARSE_HTML5 = 'HTML';
|
2017-01-06 17:18:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TgLog
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
/**
|
|
|
|
* @var \Twig_Environment
|
|
|
|
*/
|
|
|
|
private $twig;
|
|
|
|
|
2017-01-06 17:18:39 +00:00
|
|
|
/**
|
|
|
|
* @param TgLog $client
|
|
|
|
*/
|
2017-01-06 21:38:20 +00:00
|
|
|
public function __construct(TgLog $client, \Twig_Environment $twig)
|
2017-01-06 17:18:39 +00:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2017-01-06 21:38:20 +00:00
|
|
|
$this->twig = $twig;
|
2017-01-06 17:18:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
/**
|
|
|
|
* @param Account[] $accounts
|
|
|
|
* @param string $template
|
|
|
|
* @param array $templateData
|
|
|
|
* @param KeyboardMethods|null $keyboardMarkup
|
|
|
|
* @param bool $disableWebPreview
|
|
|
|
* @param bool $disableNotifications
|
|
|
|
* @param string $parseMode
|
|
|
|
*/
|
|
|
|
public function sendMassTemplatedMessage(
|
|
|
|
array $accounts,
|
|
|
|
string $template,
|
|
|
|
array $templateData = [],
|
|
|
|
KeyboardMethods $keyboardMarkup = null,
|
|
|
|
bool $disableWebPreview = true,
|
|
|
|
bool $disableNotifications = false,
|
|
|
|
string $parseMode = self::PARSE_MARKDOWN
|
|
|
|
) {
|
|
|
|
$text = $this->twig->render($template, $templateData);
|
|
|
|
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$this->sendMessage($account, $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendTemplatedMessage(
|
|
|
|
Account $account,
|
|
|
|
string $template,
|
|
|
|
array $templateData = [],
|
|
|
|
KeyboardMethods $keyboardMarkup = null,
|
|
|
|
bool $disableWebPreview = true,
|
|
|
|
bool $disableNotifications = false,
|
|
|
|
string $parseMode = self::PARSE_MARKDOWN
|
|
|
|
): bool {
|
|
|
|
$text = $this->twig->render($template, $templateData);
|
|
|
|
|
|
|
|
return $this->sendMessage($account, $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendMessage(
|
2017-01-06 17:18:39 +00:00
|
|
|
Account $account,
|
|
|
|
string $text,
|
2017-01-06 21:38:20 +00:00
|
|
|
string $parseMode = self::PARSE_PLAIN,
|
2017-01-06 19:19:44 +00:00
|
|
|
KeyboardMethods $keyboardMarkup = null,
|
2017-01-06 17:18:39 +00:00
|
|
|
bool $disableWebPreview = false,
|
2017-01-06 17:24:40 +00:00
|
|
|
bool $disableNotifications = false
|
2017-01-06 21:38:20 +00:00
|
|
|
): bool {
|
2017-01-06 17:18:39 +00:00
|
|
|
return $this->sendMessageToChat($account->getChatId(), $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:38:20 +00:00
|
|
|
public function sendMessageToChat(
|
2017-01-06 17:18:39 +00:00
|
|
|
int $chatId,
|
|
|
|
string $text,
|
2017-01-06 21:38:20 +00:00
|
|
|
string $parseMode = self::PARSE_PLAIN,
|
2017-01-06 19:19:44 +00:00
|
|
|
KeyboardMethods $keyboardMarkup = null,
|
2017-01-06 17:18:39 +00:00
|
|
|
bool $disableWebPreview = false,
|
2017-01-06 17:24:40 +00:00
|
|
|
bool $disableNotifications = false
|
2017-01-06 21:38:20 +00:00
|
|
|
): bool {
|
2017-01-06 17:18:39 +00:00
|
|
|
$sendMessage = new SendMessage();
|
2017-01-06 21:38:20 +00:00
|
|
|
$sendMessage->chat_id = (string)$chatId;
|
2017-01-06 17:18:39 +00:00
|
|
|
$sendMessage->text = $text;
|
2017-01-06 17:24:40 +00:00
|
|
|
$sendMessage->parse_mode = $parseMode;
|
|
|
|
$sendMessage->disable_web_page_preview = $disableWebPreview;
|
|
|
|
$sendMessage->disable_notification = $disableNotifications;
|
|
|
|
$sendMessage->reply_markup = $keyboardMarkup;
|
2017-01-06 17:18:39 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->client->performApiRequest($sendMessage);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (ClientException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|