1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tuyakhov\notifications\channels; |
4
|
|
|
|
5
|
|
|
use tuyakhov\notifications\NotifiableInterface; |
6
|
|
|
use tuyakhov\notifications\NotificationInterface; |
7
|
|
|
use tuyakhov\notifications\messages\TelegramMessage; |
8
|
|
|
use yii\base\Component; |
9
|
|
|
use yii\di\Instance; |
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use yii\httpclient\Client; |
12
|
|
|
|
13
|
|
|
class TelegramChannel extends Component implements ChannelInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Client|array|string |
17
|
|
|
*/ |
18
|
|
|
public $httpClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $apiUrl = "https://api.telegram.org/"; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $bot_id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $botToken; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $parseMode = null; |
39
|
|
|
|
40
|
|
|
const PARSE_MODE_HTML = "HTML"; |
41
|
|
|
|
42
|
|
|
const PARSE_MODE_MARKDOWN = "Markdown"; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
* If you need to change silentMode, you can use this code before calling telegram channel |
47
|
|
|
* |
48
|
|
|
* \Yii::$container->set('\app\additional\notification\TelegramChannel', [ |
49
|
|
|
* 'silentMode' => true, |
50
|
|
|
* ]); |
51
|
|
|
*/ |
52
|
|
|
public $silentMode = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws \yii\base\InvalidConfigException |
56
|
|
|
*/ |
57
|
|
|
public function init() |
58
|
|
|
{ |
59
|
|
|
parent::init(); |
60
|
|
|
|
61
|
|
|
if(!isset($this->bot_id) || !isset($this->botToken)){ |
62
|
|
|
throw new InvalidConfigException("Bot id or bot token is undefined"); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!isset($this->httpClient)) { |
66
|
|
|
$this->httpClient = [ |
67
|
|
|
'class' => Client::className(), |
|
|
|
|
68
|
|
|
'baseUrl' => $this->apiUrl |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
$this->httpClient = Instance::ensure($this->httpClient, Client::className()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param NotifiableInterface $recipient |
77
|
|
|
* @param NotificationInterface $notification |
78
|
|
|
* @return mixed |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
public function send(NotifiableInterface $recipient, NotificationInterface $notification) |
82
|
|
|
{ |
83
|
|
|
/** @var TelegramMessage $message */ |
84
|
|
|
$message = $notification->exportFor('telegram'); |
85
|
|
|
$text = "*{$message->subject}*\n{$message->body}"; |
86
|
|
|
$chat_id = $recipient->routeNotificationFor('telegram'); |
87
|
|
|
if(!$chat_id){ |
88
|
|
|
throw new \Exception("User doesn't have telegram_id"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$data = [ |
92
|
|
|
"chat_id" => $chat_id, |
93
|
|
|
"text" => $text, |
94
|
|
|
'disable_notification' => $this->silentMode |
95
|
|
|
]; |
96
|
|
|
if($this->parseMode != null){ |
97
|
|
|
$data["parse_mode"] = $this->parseMode; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->httpClient->createRequest() |
101
|
|
|
->setMethod('post') |
102
|
|
|
->setUrl($this->createUrl()) |
103
|
|
|
->setData($data) |
104
|
|
|
->send(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function createUrl() |
108
|
|
|
{ |
109
|
|
|
return "bot" . $this->bot_id . ":" . $this->botToken . "/sendmessage"; |
110
|
|
|
} |
111
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths