Skip to content

Commit 2360c12

Browse files
committed
add Telegram notifications support
1 parent f1aa4dc commit 2360c12

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/Compiler.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Compiler
4444
'ErrorStop',
4545
'Slack',
4646
'Discord',
47+
'Telegram',
4748
];
4849

4950
/**
@@ -428,6 +429,19 @@ protected function compileDiscord($value)
428429
return preg_replace($pattern, '$1 if (! isset($task)) $task = null; Laravel\Envoy\Discord::make$2->task($task)->send();', $value);
429430
}
430431

432+
/**
433+
* Compile Envoy Telegram statements into valid PHP.
434+
*
435+
* @param string $value
436+
* @return string
437+
*/
438+
protected function compileTelegram($value)
439+
{
440+
$pattern = $this->createMatcher('telegram');
441+
442+
return preg_replace($pattern, '$1 if (! isset($task)) $task = null; Laravel\Envoy\Telegram::make$2->task($task)->send();', $value);
443+
}
444+
431445
/**
432446
* Initialize the variables included in the Envoy template.
433447
*

src/Telegram.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)