Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Rias500/craft-telegram-notificati…
Browse files Browse the repository at this point in the history
…on-channel
  • Loading branch information
Rias committed Jan 23, 2018
2 parents 0d37d9c + fec258d commit f92689f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 31 deletions.
25 changes: 12 additions & 13 deletions src/TelegramNotificationChannel.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php
/**
* Telegram Notification Channel plugin for Craft CMS 3.x
* Telegram Notification Channel plugin for Craft CMS 3.x.
*
* A Telegram notification channel for the Craft Notifications plugin
*
* @link https://rias.be
*
* @copyright Copyright (c) 2018 Rias
*/

namespace rias\telegramnotificationchannel;

use rias\notifications\events\RegisterChannelsEvent;
use rias\notifications\services\NotificationsService;
use rias\telegramnotificationchannel\models\Settings;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;

use rias\notifications\events\RegisterChannelsEvent;
use rias\notifications\services\NotificationsService;
use rias\telegramnotificationchannel\models\Settings;
use rias\telegramnotificationchannel\models\Telegram;
use rias\telegramnotificationchannel\models\TelegramChannel;
use yii\base\Event;
Expand All @@ -34,10 +32,11 @@
* https://craftcms.com/docs/plugins/introduction
*
* @author Rias
* @package TelegramNotificationChannel
*
* @since 1.0.0
*
* @property Settings $settings
*
* @method Settings getSettings()
*/
class TelegramNotificationChannel extends Plugin
Expand All @@ -47,7 +46,7 @@ class TelegramNotificationChannel extends Plugin

/**
* Static property that is an instance of this plugin class so that it can be accessed via
* TelegramNotificationChannel::$plugin
* TelegramNotificationChannel::$plugin.
*
* @var TelegramNotificationChannel
*/
Expand All @@ -58,14 +57,13 @@ class TelegramNotificationChannel extends Plugin

/**
* Set our $plugin static property to this class so that it can be accessed via
* TelegramNotificationChannel::$plugin
* TelegramNotificationChannel::$plugin.
*
* Called after the plugin class is instantiated; do any one-time initialization
* here such as hooks and events.
*
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
* you do not need to load it in your init() method.
*
*/
public function init()
{
Expand Down Expand Up @@ -100,16 +98,17 @@ protected function createSettingsModel()
* Returns the rendered settings HTML, which will be inserted into the content
* block on the settings page.
*
* @return string The rendered settings HTML
* @throws \Twig_Error_Loader
* @throws \yii\base\Exception
*
* @return string The rendered settings HTML
*/
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'telegram-notification-channel/settings',
[
'settings' => $this->getSettings()
'settings' => $this->getSettings(),
]
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
/**
* Telegram Notification Channel plugin for Craft CMS 3.x
* Telegram Notification Channel plugin for Craft CMS 3.x.
*
* A Telegram notification channel for the Craft Notifications plugin
*
* @link https://rias.be
*
* @copyright Copyright (c) 2018 Rias
*/

/**
* Telegram Notification Channel config.php
* Telegram Notification Channel config.php.
*
* This file exists only as a template for the Telegram Notification Channel settings.
* It does nothing on its own.
Expand All @@ -24,7 +25,7 @@

return [

/**
/*
* This sets the Telegram Bot token which can be acquired by speaking with
* the Botfather (https://core.telegram.org/bots#6-botfather)
*/
Expand Down
6 changes: 5 additions & 1 deletion src/exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public static function telegramRespondedWithAnError(ClientException $exception)
if ($result = json_decode($exception->getResponse()->getBody())) {
$description = $result->description ?: $description;
}

return new static("Telegram responded with an error `{$statusCode} - {$description}`");
}

/**
* Thrown when there's no bot token provided.
*
Expand All @@ -33,6 +35,7 @@ public static function telegramBotTokenNotProvided($message)
{
return new static($message);
}

/**
* Thrown when we're unable to communicate with Telegram.
*
Expand All @@ -42,6 +45,7 @@ public static function couldNotCommunicateWithTelegram()
{
return new static('The communication with Telegram failed.');
}

/**
* Thrown when there is no chat id provided.
*
Expand All @@ -51,4 +55,4 @@ public static function chatIdNotProvided()
{
return new static('Telegram notification chat ID was not provided. Please refer usage docs.');
}
}
}
12 changes: 6 additions & 6 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php
/**
* Telegram Notification Channel plugin for Craft CMS 3.x
* Telegram Notification Channel plugin for Craft CMS 3.x.
*
* A Telegram notification channel for the Craft Notifications plugin
*
* @link https://rias.be
*
* @copyright Copyright (c) 2018 Rias
*/

namespace rias\telegramnotificationchannel\models;

use rias\telegramnotificationchannel\TelegramNotificationChannel;

use Craft;
use craft\base\Model;
use rias\telegramnotificationchannel\TelegramNotificationChannel;

/**
* TelegramNotificationChannel Settings Model
* TelegramNotificationChannel Settings Model.
*
* This is a model used to define the plugin's settings.
*
Expand All @@ -26,7 +26,7 @@
* https://craftcms.com/docs/plugins/models
*
* @author Rias
* @package TelegramNotificationChannel
*
* @since 1.0.0
*/
class Settings extends Model
Expand All @@ -35,7 +35,7 @@ class Settings extends Model
// =========================================================================

/**
* Some field model attribute
* Some field model attribute.
*
* @var string
*/
Expand Down
5 changes: 3 additions & 2 deletions src/models/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function httpClient()
*
* @link https://core.telegram.org/bots/api#sendmessage
*
* @param array $params
* @param array $params
*
* @var int|string $params ['chat_id']
* @var string $params ['text']
Expand All @@ -62,8 +62,9 @@ protected function httpClient()
* @var int $params ['reply_to_message_id']
* @var string $params ['reply_markup']
*
* @return mixed
* @throws CouldNotSendNotification
*
* @return mixed
*/
public function sendMessage($params)
{
Expand Down
4 changes: 2 additions & 2 deletions src/models/TelegramChannel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace rias\telegramnotificationchannel\models;

use rias\notifications\models\Notification;
Expand All @@ -12,6 +11,7 @@ class TelegramChannel
* @var Telegram
*/
protected $telegram;

/**
* Channel constructor.
*
Expand Down Expand Up @@ -48,4 +48,4 @@ public function send($notifiable, Notification $notification)
$params = $message->toArray();
$this->telegram->sendMessage($params);
}
}
}
6 changes: 5 additions & 1 deletion src/models/TelegramMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct($content = '')
public function to($chatId)
{
$this->payload['chat_id'] = $chatId;

return $this;
}

Expand All @@ -58,6 +59,7 @@ public function to($chatId)
public function content($content)
{
$this->payload['text'] = $content;

return $this;
}

Expand All @@ -74,6 +76,7 @@ public function button($text, $url)
$this->buttons[] = compact('text', 'url');
$replyMarkup['inline_keyboard'] = array_chunk($this->buttons, 2);
$this->payload['reply_markup'] = json_encode($replyMarkup);

return $this;
}

Expand All @@ -87,6 +90,7 @@ public function button($text, $url)
public function options(array $options)
{
$this->payload = array_merge($this->payload, $options);

return $this;
}

Expand All @@ -109,4 +113,4 @@ public function toArray()
{
return $this->payload;
}
}
}
7 changes: 4 additions & 3 deletions src/translations/en/telegram-notification-channel.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?php
/**
* Telegram Notification Channel plugin for Craft CMS 3.x
* Telegram Notification Channel plugin for Craft CMS 3.x.
*
* A Telegram notification channel for the Craft Notifications plugin
*
* @link https://rias.be
*
* @copyright Copyright (c) 2018 Rias
*/

/**
* Telegram Notification Channel en Translation
* Telegram Notification Channel en Translation.
*
* Returns an array with the string to be translated (as passed to `Craft::t('telegram-notification-channel', '...')`) as
* the key, and the translation as the value.
*
* http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html
*
* @author Rias
* @package TelegramNotificationChannel
*
* @since 1.0.0
*/
return [
Expand Down

0 comments on commit f92689f

Please sign in to comment.