forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.php
177 lines (140 loc) · 4.33 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
## Installing
1. Create telegram bot with [BotFather](https://t.me/BotFather) and grab the token provided
2. Send `/start` to your bot and open https://api.telegram.org/bot{$TELEGRAM_TOKEN_HERE}/getUpdates
3. Take chat_id from response
Require telegram recipe in your `deploy.php` file:
```php
require 'contrib/telegram.php';
```
Add hook on deploy:
```php
before('deploy', 'telegram:notify');
```
## Configuration
- `telegram_token` – telegram bot token, **required**
- `telegram_chat_id` — chat ID to push messages to
- `telegram_proxy` - proxy connection string in [CURLOPT_PROXY](https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html) form like:
```
http://proxy:80
socks5://user:password@host:3128
```
- `telegram_title` – the title of application, default `{{application}}`
- `telegram_text` – notification message template
```
_{{user}}_ deploying `{{branch}}` to *{{target}}*
```
- `telegram_success_text` – success template, default:
```
Deploy to *{{target}}* successful
```
- `telegram_failure_text` – failure template, default:
```
Deploy to *{{target}}* failed
```
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'telegram:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('success', 'telegram:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'telegram:notify:failure');
*/
namespace Deployer;
use Deployer\Utility\Httpie;
// Title of project
set('telegram_title', function () {
return get('application', 'Project');
});
// Telegram settings
set('telegram_token', function () {
throw new \Exception('Please, configure "telegram_token" parameter.');
});
set('telegram_chat_id', function () {
throw new \Exception('Please, configure "telegram_chat_id" parameter.');
});
set('telegram_url', function () {
return 'https://api.telegram.org/bot' . get('telegram_token') . '/sendmessage';
});
// Deploy message
set('telegram_text', '_{{user}}_ deploying `{{branch}}` to *{{target}}*');
set('telegram_success_text', 'Deploy to *{{target}}* successful');
set('telegram_failure_text', 'Deploy to *{{target}}* failed');
desc('Notifying Telegram');
task('telegram:notify', function () {
if (!get('telegram_token', false)) {
return;
}
if (!get('telegram_chat_id', false)) {
return;
}
$telegramUrl = get('telegram_url') . '?' . http_build_query (
Array (
'chat_id' => get('telegram_chat_id'),
'text' => get('telegram_text'),
'parse_mode' => 'Markdown',
)
);
$httpie = Httpie::get($telegramUrl);
if (get('telegram_proxy', '') !== '') {
$httpie = $httpie->setopt(CURLOPT_PROXY, get('telegram_proxy'));
}
$httpie->send();
})
->once()
->shallow()
->hidden();
desc('Notifying Telegram about deploy finish');
task('telegram:notify:success', function () {
if (!get('telegram_token', false)) {
return;
}
if (!get('telegram_chat_id', false)) {
return;
}
$telegramUrl = get('telegram_url') . '?' . http_build_query (
Array (
'chat_id' => get('telegram_chat_id'),
'text' => get('telegram_success_text'),
'parse_mode' => 'Markdown',
)
);
$httpie = Httpie::get($telegramUrl);
if (get('telegram_proxy', '') !== '') {
$httpie = $httpie->setopt(CURLOPT_PROXY, get('telegram_proxy'));
}
$httpie->send();
})
->once()
->shallow()
->hidden();
desc('Notifying Telegram about deploy failure');
task('telegram:notify:failure', function () {
if (!get('telegram_token', false)) {
return;
}
if (!get('telegram_chat_id', false)) {
return;
}
$telegramUrl = get('telegram_url') . '?' . http_build_query (
Array (
'chat_id' => get('telegram_chat_id'),
'text' => get('telegram_failure_text'),
'parse_mode' => 'Markdown',
)
);
$httpie = Httpie::get($telegramUrl);
if (get('telegram_proxy', '') !== '') {
$httpie = $httpie->setopt(CURLOPT_PROXY, get('telegram_proxy'));
}
$httpie->send();
})
->once()
->shallow()
->hidden();