Support: Send new ticket notifications to Telegram

This commit is contained in:
Maxim Leshchenko 2021-12-05 23:30:57 +02:00
parent cdc130ecc2
commit d7e3d2d77d
No known key found for this signature in database
GPG key ID: BB9C44A8733FBEEE
3 changed files with 50 additions and 0 deletions

View file

@ -7,6 +7,7 @@ use openvk\Web\Models\Entities\TicketComment;
use openvk\Web\Models\Repositories\TicketComments; use openvk\Web\Models\Repositories\TicketComments;
// use openvk\Web\Models\Repositories\Users; // use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Util\Telegram;
use Nette\Database\Table\ActiveRow; use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use Chandler\Session\Session; use Chandler\Session\Session;
@ -52,6 +53,17 @@ final class SupportPresenter extends OpenVKPresenter
$ticket->setText($this->postParam("text")); $ticket->setText($this->postParam("text"));
$ticket->setcreated(time()); $ticket->setcreated(time());
$ticket->save(); $ticket->save();
$helpdeskChat = OPENVK_ROOT_CONF["openvk"]["credentials"]["telegram"]["helpdeskChat"];
if($helpdeskChat) {
$ticketText = ovk_proc_strtr($this->postParam("text"), 1500);
$telegramText = "<b>📬 Новый тикет!</b>\n\n";
$telegramText .= "<a href='$serverUrl/support/reply/{$ticket->getId()}'>{$ticket->getName()}</a>\n";
$telegramText .= "$ticketText\n\n";
$telegramText .= "Автор: <a href='$serverUrl{$ticket->getUser()->getURL()}'>{$ticket->getUser()->getCanonicalName()}</a> ({$ticket->getUser()->getRegistrationIP()})\n";
Telegram::send($helpdeskChat, $telegramText);
}
header("HTTP/1.1 302 Found"); header("HTTP/1.1 302 Found");
header("Location: /support/view/" . $ticket->getId()); header("Location: /support/view/" . $ticket->getId());
} else { } else {

34
Web/Util/Telegram.php Normal file
View file

@ -0,0 +1,34 @@
<?php declare(strict_types=1);
namespace openvk\Web\Util;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException as GuzzleClientException;
class Telegram
{
static function send(string $to, string $text, bool $webPagePreview = false): bool
{
$conf = (object) OPENVK_ROOT_CONF["openvk"]["credentials"]["telegram"];
if(!$conf->enable)
return false;
try {
(new GuzzleClient)->request(
"POST",
"https://api.telegram.org/bot{$conf->token}/sendMessage",
[
"form_params" => [
"chat_id" => $to,
"text" => $text,
"disable_web_page_preview" => $webPagePreview ? "true" : "false",
"parse_mode" => "HTML",
]
]
);
} catch (GuzzleClientException $ex) {
trigger_error("Could not send Telegram message to $to: {$ex->getMessage()}", E_USER_WARNING);
return false;
}
return true;
}
}

View file

@ -65,6 +65,10 @@ openvk:
enable: false enable: false
client: "" client: ""
secret: "SECRET_KEY_HERE" secret: "SECRET_KEY_HERE"
telegram:
enable: false
token: "TOKEN_HERE"
helpdeskChat: ""
eventDB: eventDB:
enable: false # Better enable this enable: false # Better enable this
database: database: