From 508318303e0d7054ea38b3a65a37d5fc51129f41 Mon Sep 17 00:00:00 2001 From: Liza Date: Wed, 17 Jul 2019 16:19:19 +0300 Subject: [PATCH] Add telegram channel and message --- src/channels/TelegramChannel.php | 100 +++++++++++++++++++++++++++++++ src/messages/TelegramMessage.php | 7 +++ 2 files changed, 107 insertions(+) create mode 100755 src/channels/TelegramChannel.php create mode 100755 src/messages/TelegramMessage.php diff --git a/src/channels/TelegramChannel.php b/src/channels/TelegramChannel.php new file mode 100755 index 0000000..d2f01f2 --- /dev/null +++ b/src/channels/TelegramChannel.php @@ -0,0 +1,100 @@ +bot_id) || !isset($this->bot_token)){ + throw new InvalidConfigException("Bot id or bot token is undefined"); + } + + if (!isset($this->httpClient)) { + $this->httpClient = [ + 'class' => Client::className(), + ]; + } + $this->httpClient = Instance::ensure($this->httpClient, Client::className()); + } + + /** + * @param NotifiableInterface $recipient + * @param NotificationInterface $notification + * @return mixed + */ + public function send(NotifiableInterface $recipient, NotificationInterface $notification) + { + /** @var TelegramMessage $message */ + $message = $notification->exportFor('telegram'); + $text = "*{$message->subject}*\n{$message->body}"; + $chatId = $recipient->routeNotificationFor('telegram'); + + $data = [ + "chat_id" => $chatId, + "text" => $text, + ]; + if($this->parse_mode != null){ + $data["parse_mode"] = $this->parse_mode; + } + + $resultUrl = $this->createUrl(); + return $this->httpClient->createRequest() + ->setMethod('post') + ->setUrl($resultUrl) + ->setData($data) + ->send(); + } + + private function createUrl() + { + return $this->api_url . $this->bot_id . ":" . $this->bot_token . "/sendmessage"; + } +} \ No newline at end of file diff --git a/src/messages/TelegramMessage.php b/src/messages/TelegramMessage.php new file mode 100755 index 0000000..71a1a7f --- /dev/null +++ b/src/messages/TelegramMessage.php @@ -0,0 +1,7 @@ +