|
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 BaseActiveRecord|string |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
|
|
public $model = 'app\additional\notification\Notification'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Client|array|string |
|
22
|
|
|
*/ |
|
23
|
|
|
public $httpClient; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $api_url = "https://api.telegram.org/bot"; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $bot_id; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $bot_token; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $parse_mode = null; |
|
44
|
|
|
|
|
45
|
|
|
const PARSE_MODE_HTML = "HTML"; |
|
46
|
|
|
|
|
47
|
|
|
const PARSE_MODE_MARKDOWN = "Markdown"; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var bool |
|
51
|
|
|
* If you need to change silent_mode, you can use this code before calling telegram channel |
|
52
|
|
|
* |
|
53
|
|
|
* \Yii::$container->set('\app\additional\notification\TelegramChannel', [ |
|
54
|
|
|
* 'silent_mode' => true, |
|
55
|
|
|
* ]); |
|
56
|
|
|
*/ |
|
57
|
|
|
public $silent_mode = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @throws \yii\base\InvalidConfigException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function init() |
|
63
|
|
|
{ |
|
64
|
|
|
parent::init(); |
|
65
|
|
|
|
|
66
|
|
|
if(!isset($this->bot_id) || !isset($this->bot_token)){ |
|
67
|
|
|
throw new InvalidConfigException("Bot id or bot token is undefined"); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!isset($this->httpClient)) { |
|
71
|
|
|
$this->httpClient = [ |
|
72
|
|
|
'class' => Client::className(), |
|
|
|
|
|
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
$this->httpClient = Instance::ensure($this->httpClient, Client::className()); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param NotifiableInterface $recipient |
|
81
|
|
|
* @param NotificationInterface $notification |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
* @throws \Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function send(NotifiableInterface $recipient, NotificationInterface $notification) |
|
86
|
|
|
{ |
|
87
|
|
|
/** @var TelegramMessage $message */ |
|
88
|
|
|
$message = $notification->exportFor('telegram'); |
|
89
|
|
|
$text = "*{$message->subject}*\n{$message->body}"; |
|
90
|
|
|
$chat_id = $recipient->routeNotificationFor('telegram'); |
|
91
|
|
|
if(!$chat_id){ |
|
92
|
|
|
throw new \Exception("User doesn't have telegram_id"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$data = [ |
|
96
|
|
|
"chat_id" => $chat_id, |
|
97
|
|
|
"text" => $text, |
|
98
|
|
|
'disable_notification' => $this->silent_mode |
|
99
|
|
|
]; |
|
100
|
|
|
if($this->parse_mode != null){ |
|
101
|
|
|
$data["parse_mode"] = $this->parse_mode; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$resultUrl = $this->createUrl(); |
|
105
|
|
|
return $this->httpClient->createRequest() |
|
106
|
|
|
->setMethod('post') |
|
107
|
|
|
->setUrl($resultUrl) |
|
108
|
|
|
->setData($data) |
|
109
|
|
|
->send(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function createUrl() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->api_url . $this->bot_id . ":" . $this->bot_token . "/sendmessage"; |
|
115
|
|
|
} |
|
116
|
|
|
} |
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