1 | <?php |
||||
2 | /** |
||||
3 | * @copyright Anton Tuyakhov <[email protected]> |
||||
4 | */ |
||||
5 | |||||
6 | namespace tuyakhov\notifications\channels; |
||||
7 | |||||
8 | use tuyakhov\notifications\NotifiableInterface; |
||||
9 | use tuyakhov\notifications\NotificationInterface; |
||||
10 | use tuyakhov\notifications\messages\TelegramMessage; |
||||
11 | use yii\base\Component; |
||||
12 | use yii\base\InvalidArgumentException; |
||||
13 | use yii\base\InvalidConfigException; |
||||
14 | use yii\di\Instance; |
||||
15 | use yii\helpers\Json; |
||||
16 | use yii\httpclient\Client; |
||||
17 | |||||
18 | /** |
||||
19 | * See an example flow of sending notifications in Telegram |
||||
20 | * @see https://core.telegram.org/bots#deep-linking-example |
||||
21 | */ |
||||
22 | class TelegramChannel extends Component implements ChannelInterface |
||||
23 | { |
||||
24 | /** |
||||
25 | * @var Client|array|string |
||||
26 | */ |
||||
27 | public $httpClient; |
||||
28 | |||||
29 | /** |
||||
30 | * @var string |
||||
31 | */ |
||||
32 | public $apiUrl = "https://api.telegram.org"; |
||||
33 | |||||
34 | /** |
||||
35 | * Each bot is given a unique authentication token when it is created. |
||||
36 | * The token looks something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 |
||||
37 | * @var string |
||||
38 | */ |
||||
39 | public $botToken; |
||||
40 | |||||
41 | /** |
||||
42 | * @var string |
||||
43 | */ |
||||
44 | public $parseMode = self::PARSE_MODE_MARKDOWN; |
||||
45 | |||||
46 | const PARSE_MODE_HTML = "HTML"; |
||||
47 | |||||
48 | const PARSE_MODE_MARKDOWN = "Markdown"; |
||||
49 | |||||
50 | /** |
||||
51 | * @throws \yii\base\InvalidConfigException |
||||
52 | */ |
||||
53 | public function init() |
||||
54 | { |
||||
55 | parent::init(); |
||||
56 | |||||
57 | if(!isset($this->botToken)){ |
||||
58 | throw new InvalidConfigException('Bot token is undefined'); |
||||
59 | } |
||||
60 | |||||
61 | if (!isset($this->httpClient)) { |
||||
62 | $this->httpClient = [ |
||||
63 | 'class' => Client::className(), |
||||
0 ignored issues
–
show
|
|||||
64 | 'baseUrl' => $this->apiUrl, |
||||
65 | ]; |
||||
66 | } |
||||
67 | $this->httpClient = Instance::ensure($this->httpClient, Client::className()); |
||||
0 ignored issues
–
show
The function
yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
68 | } |
||||
69 | |||||
70 | |||||
71 | /** |
||||
72 | * @inheritDoc |
||||
73 | */ |
||||
74 | public function send(NotifiableInterface $recipient, NotificationInterface $notification) |
||||
75 | { |
||||
76 | /** @var TelegramMessage $message */ |
||||
77 | $message = $notification->exportFor('telegram'); |
||||
78 | $text = $message->body; |
||||
79 | if (!empty($message->subject)) { |
||||
80 | $text = "*{$message->subject}*\n{$message->body}"; |
||||
81 | } |
||||
82 | $chatId = $recipient->routeNotificationFor('telegram'); |
||||
83 | if(!$chatId){ |
||||
84 | throw new InvalidArgumentException( 'No chat ID provided'); |
||||
85 | } |
||||
86 | |||||
87 | $data = [ |
||||
88 | 'chat_id' => $chatId, |
||||
89 | 'text' => $text, |
||||
90 | 'disable_notification' => $message->silentMode, |
||||
91 | 'disable_web_page_preview' => $message->withoutPagePreview, |
||||
92 | ]; |
||||
93 | |||||
94 | if ($message->replyToMessageId) { |
||||
95 | $data['reply_to_message_id'] = $message->replyToMessageId; |
||||
96 | } |
||||
97 | |||||
98 | if ($message->replyMarkup) { |
||||
99 | $data['reply_markup'] = Json::encode($message->replyMarkup); |
||||
100 | } |
||||
101 | |||||
102 | if(isset($this->parseMode)){ |
||||
103 | $data['parse_mode'] = $this->parseMode; |
||||
104 | } |
||||
105 | |||||
106 | return $this->httpClient->createRequest() |
||||
107 | ->setUrl($this->createUrl()) |
||||
108 | ->setData($data) |
||||
109 | ->send(); |
||||
110 | } |
||||
111 | |||||
112 | private function createUrl() |
||||
113 | { |
||||
114 | return "bot{$this->botToken}/sendMessage"; |
||||
115 | } |
||||
116 | } |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.