|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Envoy; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | + |
| 7 | +class Telegram |
| 8 | +{ |
| 9 | + use ConfigurationParser; |
| 10 | + |
| 11 | + /** |
| 12 | + * The Bot API Token. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + public $token; |
| 17 | + |
| 18 | + /** |
| 19 | + * The Telegram chat_id. |
| 20 | + * |
| 21 | + * @var mixed |
| 22 | + */ |
| 23 | + public $chat; |
| 24 | + |
| 25 | + /** |
| 26 | + * The message. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + public $message; |
| 31 | + |
| 32 | + /** |
| 33 | + * The options. |
| 34 | + * |
| 35 | + * @var array |
| 36 | + */ |
| 37 | + public $options; |
| 38 | + |
| 39 | + /** |
| 40 | + * The task name. |
| 41 | + * |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $task; |
| 45 | + |
| 46 | + /** |
| 47 | + * Create a new Telegram instance. |
| 48 | + * |
| 49 | + * @param string $token |
| 50 | + * @param mixed $chat |
| 51 | + * @param string $message |
| 52 | + * @param array $options |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function __construct($token, $chat, $message = null, $options = []) |
| 56 | + { |
| 57 | + $this->token = $token; |
| 58 | + $this->chat = $chat; |
| 59 | + $this->message = $message; |
| 60 | + $this->options = $options; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Create a new Telegram message instance. |
| 65 | + * |
| 66 | + * @param string $token |
| 67 | + * @param string $chat |
| 68 | + * @param string $message |
| 69 | + * @param array $options |
| 70 | + * @return \Laravel\Envoy\Telegram |
| 71 | + */ |
| 72 | + public static function make($token, $chat, $message = null, $options = []) |
| 73 | + { |
| 74 | + return new static($token, $chat, $message, $options); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Send the Telegram message. |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function send() |
| 83 | + { |
| 84 | + (new Client())->post($this->getSendMessageEndpoint(), [ |
| 85 | + 'json' => $this->buildPayload(), |
| 86 | + ]); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get the endpoint for the Send request |
| 91 | + * |
| 92 | + * @return mixed |
| 93 | + */ |
| 94 | + private function getSendMessageEndpoint() |
| 95 | + { |
| 96 | + return "https://api.telegram.org/bot{$this->token}/sendMessage"; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Build the payload to send to the endpoint |
| 101 | + * |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + private function buildPayload() |
| 105 | + { |
| 106 | + $message = $this->message ?: ($this->task ? ucwords($this->getSystemUser()) . ' ran the [' . $this->task . '] task.' : ucwords($this->getSystemUser()) . ' ran a task.'); |
| 107 | + |
| 108 | + return array_merge(['text' => $message, 'chat_id' => $this->chat], $this->options); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Set the task for the message. |
| 113 | + * |
| 114 | + * @param string $task |
| 115 | + * @return $this |
| 116 | + */ |
| 117 | + public function task($task) |
| 118 | + { |
| 119 | + $this->task = $task; |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | +} |
0 commit comments