Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix128 committed Mar 2, 2018
0 parents commit ec02323
Show file tree
Hide file tree
Showing 20 changed files with 1,090 additions and 0 deletions.
16 changes: 16 additions & 0 deletions COPYING.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* MageSpecialist
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @copyright Copyright (c) 2018 Skeeller srl (http://www.magespecialist.it)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
72 changes: 72 additions & 0 deletions Command/Telegram/GetChatIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © MageSpecialist - Skeeller srl. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace MSP\NotifierCoreAdapters\Command\Telegram;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Console\Helper\Table;

class GetChatIds extends Command
{
/**
* @var \MSP\NotifierCoreAdapters\Model\AdapterEngine\Telegram\GetChatIds
*/
private $getChatIds;

/**
* SendMessage constructor.
* @param \MSP\NotifierCoreAdapters\Model\AdapterEngine\Telegram\GetChatIds $getChatIds
*/
public function __construct(
\MSP\NotifierCoreAdapters\Model\AdapterEngine\Telegram\GetChatIds $getChatIds
) {
$this->getChatIds = $getChatIds;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('msp:notifier:telegram:chat_ids');
$this->setDescription('Print chat IDs for a TelegramBot token');

$this->addArgument('token', InputArgument::REQUIRED, 'BOT Token');

parent::configure();
}

/**
* @inheritdoc
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$token = $input->getArgument('token');
$chatIds = $this->getChatIds->execute($token);

// @codingStandardsIgnoreStart
$table = new Table($output);
// @codingStandardsIgnoreEnd
$table->setHeaders(['Chat ID', 'Name']);

$tableRows = [];
foreach ($chatIds as $chatId => $title) {
$tableRows[] = [$chatId, $title];
}

$table->setRows($tableRows);
$table->render();
}
}
72 changes: 72 additions & 0 deletions Model/AdapterEngine/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © MageSpecialist - Skeeller srl. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace MSP\NotifierCoreAdapters\Model\AdapterEngine;

use Magento\Framework\Mail\MessageInterface;
use MSP\Notifier\Model\AdapterEngine\AdapterEngineInterface;
use Magento\Framework\Mail\MessageInterfaceFactory;
use Magento\Framework\Mail\TransportInterfaceFactory;

class Email implements AdapterEngineInterface
{
const ADAPTER_CODE = 'email';

const ADAPTER_FROM = 'from';
const ADAPTER_FROM_NAME = 'from_name';
const ADAPTER_TO = 'to';

/**
* @var MessageInterfaceFactory
*/
private $messageFactory;

/**
* @var TransportInterfaceFactory
*/
private $transportFactory;

/**
* Email constructor.
* @param MessageInterfaceFactory $messageFactory
* @param TransportInterfaceFactory $transportFactory
* @SuppressWarnings(PHPMD.LongVariables)
*/
public function __construct(
MessageInterfaceFactory $messageFactory,
TransportInterfaceFactory $transportFactory
) {
$this->messageFactory = $messageFactory;
$this->transportFactory = $transportFactory;
}

/**
* Execute engine and return true on success. Throw exception on failure.
* @param string $message
* @param array $params
* @return bool
* @throws \Magento\Framework\Exception\MailException
*/
public function execute(string $message, array $params = []): bool
{
$lines = explode("\n", $message);

$emailMessage = $this->messageFactory->create();

$emailMessage->setFrom($params['from'], $params['from_name']);
$emailMessage->addTo($params['to']);
$emailMessage->setMessageType(MessageInterface::TYPE_TEXT);
$emailMessage->setBody($message);
$emailMessage->setSubject($lines[0]);

$transport = $this->transportFactory->create(['message' => $emailMessage]);
$transport->sendMessage();

return true;
}
}
85 changes: 85 additions & 0 deletions Model/AdapterEngine/Slack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © MageSpecialist - Skeeller srl. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace MSP\NotifierCoreAdapters\Model\AdapterEngine;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Maknz\Slack\Client;
use MSP\Notifier\Model\AdapterEngine\AdapterEngineInterface;

class Slack implements AdapterEngineInterface
{
const PARAM_COLOR = 'color';
const PARAM_EMOJI = 'emoji';
const PARAM_CHANNEL = 'channel';
const PARAM_WEBHOOK = 'webhook';

const DEFAULT_EMOJI = ':bear:';
const DEFAULT_COLOR = '#333333';

const ADAPTER_CODE = 'slack';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Slack constructor.
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

/**
* Execute engine and return true on success. Throw exception on failure.
* @param string $emailMessage
* @param array $params
* @return bool
*/
public function execute(string $emailMessage, array $params = []): bool
{
$client = $this->getClient($params);

$client->attach([
'fallback' => $emailMessage,
'text' => $emailMessage,
'color' => $params[static::PARAM_COLOR] ?: static::DEFAULT_COLOR
])->send();

return true;
}

/**
* @param array $params
* @return array
*/
private function paramsToSettings(array $params): array
{
return [
'userName' => $this->scopeConfig->getValue('general/store_information/name'),
'channel' => $params[static::PARAM_CHANNEL],
'icon' => $params[static::PARAM_EMOJI] ?: static::DEFAULT_EMOJI
];
}

/**
* @param array $params
* @return Client
*/
private function getClient(array $params): Client
{
$settings = $this->paramsToSettings($params);
// @codingStandardsIgnoreStart
return new Client($params[static::PARAM_WEBHOOK], $settings);
// @codingStandardsIgnoreEnd
}
}
53 changes: 53 additions & 0 deletions Model/AdapterEngine/Telegram.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © MageSpecialist - Skeeller srl. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace MSP\NotifierCoreAdapters\Model\AdapterEngine;

use MSP\Notifier\Model\AdapterEngine\AdapterEngineInterface;
use MSP\NotifierCoreAdapters\Model\AdapterEngine\Telegram\ClientRepository;

class Telegram implements AdapterEngineInterface
{
const ADAPTER_CODE = 'telegram';
const PARAM_TOKEN = 'token';
const PARAM_CHAT_ID = 'chat_id';

/**
* @var ClientRepository
*/
private $clientRepository;

/**
* Telegram constructor.
* @param ClientRepository $clientRepository
*/
public function __construct(
ClientRepository $clientRepository
) {
$this->clientRepository = $clientRepository;
}

/**
* Execute engine and return true on success. Throw exception on failure.
* @param string $emailMessage
* @param array $params
* @return bool
* @throws \Telegram\Bot\Exceptions\TelegramSDKException
*/
public function execute(string $emailMessage, array $params = []): bool
{
$client = $this->clientRepository->get($params[self::PARAM_TOKEN]);
$client->sendMessage([
'chat_id' => $params[self::PARAM_CHAT_ID],
'text' => $emailMessage,
'parse_mode' => 'HTML'
]);

return true;
}
}
33 changes: 33 additions & 0 deletions Model/AdapterEngine/Telegram/ClientRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © MageSpecialist - Skeeller srl. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace MSP\NotifierCoreAdapters\Model\AdapterEngine\Telegram;

use Telegram\Bot\Api;

class ClientRepository
{
private $clients = [];

/**
* Get a telegram client by token
* @param string $token
* @return Api
* @throws \Telegram\Bot\Exceptions\TelegramSDKException
*/
public function get(string $token): Api
{
if (!isset($this->clients[$token])) {
// @codingStandardsIgnoreStart
$this->clients[$token] = new Api($token);
// @codingStandardsIgnoreEnd
}

return $this->clients[$token];
}
}
56 changes: 56 additions & 0 deletions Model/AdapterEngine/Telegram/GetChatIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © MageSpecialist - Skeeller srl. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace MSP\NotifierCoreAdapters\Model\AdapterEngine\Telegram;

class GetChatIds
{
/**
* @var ClientRepository
*/
private $clientRepository;

/**
* GetChatIds constructor.
* @param ClientRepository $clientRepository
*/
public function __construct(
ClientRepository $clientRepository
) {
$this->clientRepository = $clientRepository;
}

/**
* Get a telegram client by token
* @param string $token
* @return array
* @throws \Telegram\Bot\Exceptions\TelegramSDKException
*/
public function execute(string $token): array
{
$bot = $this->clientRepository->get($token);
$updates = $bot->getUpdates();

$res = [];
foreach ($updates as $update) {
$message = $update->get('message');
if ($message) {
$chat = $message->get('chat');
$chatId = $chat->get('id');

if ($chat->get('title')) {
$res[$chatId] = $chat->get('title');
} else {
$res[$chatId] = $chat->get('username');
}
}
}

return $res;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# notifier-core-adapters
Loading

0 comments on commit ec02323

Please sign in to comment.