forked from tuyakhov/yii2-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tuyakhov#23 from Julielesss/telegram
Telegram
- Loading branch information
Showing
10 changed files
with
307 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace tuyakhov\notifications\channels; | ||
|
||
use tuyakhov\notifications\NotifiableInterface; | ||
use tuyakhov\notifications\NotificationInterface; | ||
use tuyakhov\notifications\messages\TelegramMessage; | ||
use yii\base\Component; | ||
use yii\di\Instance; | ||
use yii\helpers\ArrayHelper; | ||
use yii\httpclient\Client; | ||
|
||
class TelegramChannel extends Component implements ChannelInterface | ||
{ | ||
/** | ||
* @var Client|array|string | ||
*/ | ||
public $httpClient; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $apiUrl = "https://api.telegram.org/"; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $bot_id; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $botToken; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $parseMode = null; | ||
|
||
const PARSE_MODE_HTML = "HTML"; | ||
|
||
const PARSE_MODE_MARKDOWN = "Markdown"; | ||
|
||
/** | ||
* @var bool | ||
* If you need to change silentMode, you can use this code before calling telegram channel | ||
* | ||
* \Yii::$container->set('\app\additional\notification\TelegramChannel', [ | ||
* 'silentMode' => true, | ||
* ]); | ||
*/ | ||
public $silentMode = false; | ||
|
||
/** | ||
* @throws \yii\base\InvalidConfigException | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if(!isset($this->bot_id) || !isset($this->botToken)){ | ||
throw new InvalidConfigException("Bot id or bot token is undefined"); | ||
} | ||
|
||
if (!isset($this->httpClient)) { | ||
$this->httpClient = [ | ||
'class' => Client::className(), | ||
'baseUrl' => $this->apiUrl | ||
]; | ||
} | ||
$this->httpClient = Instance::ensure($this->httpClient, Client::className()); | ||
} | ||
|
||
|
||
/** | ||
* @param NotifiableInterface $recipient | ||
* @param NotificationInterface $notification | ||
* @return mixed | ||
* @throws \Exception | ||
*/ | ||
public function send(NotifiableInterface $recipient, NotificationInterface $notification) | ||
{ | ||
/** @var TelegramMessage $message */ | ||
$message = $notification->exportFor('telegram'); | ||
$text = "*{$message->subject}*\n{$message->body}"; | ||
$chat_id = $recipient->routeNotificationFor('telegram'); | ||
if(!$chat_id){ | ||
throw new \Exception("User doesn't have telegram_id"); | ||
} | ||
|
||
$data = [ | ||
"chat_id" => $chat_id, | ||
"text" => $text, | ||
'disable_notification' => $this->silentMode | ||
]; | ||
if($this->parseMode != null){ | ||
$data["parse_mode"] = $this->parseMode; | ||
} | ||
|
||
return $this->httpClient->createRequest() | ||
->setMethod('post') | ||
->setUrl($this->createUrl()) | ||
->setData($data) | ||
->send(); | ||
} | ||
|
||
private function createUrl() | ||
{ | ||
return "bot" . $this->bot_id . ":" . $this->botToken . "/sendmessage"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace tuyakhov\notifications\messages; | ||
|
||
class TelegramMessage extends AbstractMessage | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* @copyright Anton Tuyakhov <[email protected]> | ||
*/ | ||
|
||
namespace tuyakhov\notifications\migrations; | ||
|
||
use yii\db\Migration; | ||
|
||
class m181112_171335_add_data_column extends Migration | ||
{ | ||
|
||
public function up() | ||
{ | ||
$this->addColumn('notification', 'data', $this->text()); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->dropColumn('notification', 'data'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.