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