forked from mantisbt-plugins/TelegramBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelegramBot.php
309 lines (280 loc) · 18.8 KB
/
TelegramBot.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
# Copyright (c) 2018 Grigoriy Ermolaev ([email protected])
# TelegramBot for MantisBT is free software:
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation,
# either version 2 of the License, or (at your option) any later version.
#
# TelegramBot plugin for for MantisBT is distributed in the hope
# that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Customer management plugin for MantisBT.
# If not, see <http://www.gnu.org/licenses/>.
class TelegramBotPlugin extends MantisPlugin {
function register() {
$this->name = 'TelegramBot';
$this->description = plugin_lang_get( 'description' );
$this->version = '1.2.0-dev';
$this->requires = array(
'MantisCore' => '2.14.0',
);
$this->author = 'Grigoriy Ermolaev';
$this->contact = '[email protected]';
$this->url = 'http://github.com/mantisbt-plugins/TelegramBot';
$this->page = 'config_page';
}
function schema() {
return array(
// version 0.0.1
array( "CreateTableSQL", array( plugin_table( "user_relationship" ), "
mantis_user_id INT(10) NOTNULL PRIMARY,
telegram_user_id INT(10) NOTNULL
" ) )
);
}
function init() {
require_once __DIR__ . '/api/vendor/autoload.php';
require_once 'core/TelegramBot_bug_api.php';
require_once 'core/TelegramBot_authentication_api.php';
require_once 'core/TelegramBot_user_api.php';
require_once 'core/TelegramBot_helper_api.php';
require_once 'core/TelegramBot_keyboard_api.php';
require_once 'core/TelegramBot_fields_api.php';
require_once 'core/TelegramBot_message_api.php';
require_once 'core/TelegramBot_message_format_api.php';
global $g_skip_sending_bugnote, $g_account_telegram_menu_active;
$g_skip_sending_bugnote = FALSE;
$g_account_telegram_menu_active = FALSE;
}
function config() {
return array(
'api_token' => NULL,
'bot_name' => NULL,
'bot_father_url' => 'https://t.me/BotFather',
'telegram_url' => 'https://telegram.me/',
'download_path' => '/tmp/',
/**
* The following two config options allow you to control who should get email
* notifications on different actions/statuses. The first option
* (default_notify_flags) sets the default values for different user
* categories. The user categories are:
*
* 'reporter': the reporter of the bug
* 'handler': the handler of the bug
* 'monitor': users who are monitoring a bug
* 'bugnotes': users who have added a bugnote to the bug
* 'category': category owners
* 'explicit': users who are explicitly specified by the code based on the
* action (e.g. user added to monitor list).
* 'threshold_max': all users with access <= max
* 'threshold_min': ..and with access >= min
*
* The second config option (notify_flags) sets overrides for specific
* actions/statuses. If a user category is not listed for an action, the
* default from the config option above is used. The possible actions are:
*
* 'new': a new bug has been added
* 'owner': a bug has been assigned to a new owner
* 'reopened': a bug has been reopened
* 'deleted': a bug has been deleted
* 'updated': a bug has been updated
* 'bugnote': a bugnote has been added to a bug
* 'sponsor': sponsorship has changed on this bug
* 'relation': a relationship has changed on this bug
* 'monitor': an issue is monitored.
* '<status>': eg: 'resolved', 'closed', 'feedback', 'acknowledged', etc.
* this list corresponds to $g_status_enum_string
*
* If you wanted to have all developers get notified of new bugs you might add
* the following lines to your config file:
*
* $g_notify_flags['new']['threshold_min'] = DEVELOPER;
* $g_notify_flags['new']['threshold_max'] = DEVELOPER;
*
* You might want to do something similar so all managers are notified when a
* bug is closed. If you did not want reporters to be notified when a bug is
* closed (only when it is resolved) you would use:
*
* $g_notify_flags['closed']['reporter'] = OFF;
*
* @global array $g_default_notify_flags
*/
'default_notify_flags' => array(
'reporter' => ON,
'handler' => ON,
'monitor' => ON,
'bugnotes' => ON,
'category' => ON,
'explicit' => ON,
'threshold_min' => NOBODY,
'threshold_max' => NOBODY
),
/**
* We don't need to send these notifications on new bugs
* (see above for info on this config option)
* @todo (though I'm not sure they need to be turned off anymore
* - there just won't be anyone in those categories)
* I guess it serves as an example and a placeholder for this
* config option
* @see $g_default_notify_flags
* @global array $g_notify_flags
*/
'notify_flags' => array(
'new' => array(
'bugnotes' => OFF,
'monitor' => OFF
),
'monitor' => array(
'reporter' => OFF,
'handler' => OFF,
'monitor' => OFF,
'bugnotes' => OFF,
'explicit' => ON,
'threshold_min' => NOBODY,
'threshold_max' => NOBODY
)
),
/**
* Whether user's should receive emails for their own actions
* @global integer $g_email_receive_own
*/
'telegram_message_receive_own' => OFF,
//
'telegram_message_on_new' => ON,
'telegram_message_on_assigned' => ON,
'telegram_message_on_feedback' => ON,
'telegram_message_on_resolved' => ON,
'telegram_message_on_closed' => ON,
'telegram_message_on_reopened' => ON,
'telegram_message_on_bugnote' => ON,
'telegram_message_on_status' => OFF,
'telegram_message_on_priority' => OFF,
//
'telegram_message_on_priority_min_severity' => 0,
'telegram_message_on_status_min_severity' => 0,
'telegram_message_on_bugnote_min_severity' => 0,
'telegram_message_on_reopened_min_severity' => 0,
'telegram_message_on_closed_min_severity' => 0,
'telegram_message_on_resolved_min_severity' => 0,
'telegram_message_on_feedback_min_severity' => 0,
'telegram_message_on_assigned_min_severity' => 0,
'telegram_message_on_new_min_severity' => 0,
//
'telegram_message_bugnote_limit' => 0,
/**
* Allow telegram message notification.
* Set to ON to enable telegram message notifications, OFF to disable them. Note that
* disabling telegram message notifications has no effect on telegram message generated as part
* of the user signup process. When set to OFF, the password reset feature
* is disabled. Additionally, notifications of administrators updating
* accounts are not sent to users.
* @global integer $g_enable_email_notification
*/
'enable_telegram_message_notification' => ON,
//
'telegram_message_separator1' => str_pad( '', 27, '=' ),
'telegram_message_separator2' => str_pad( '', 55, '-' ),
'telegram_message_padding_length' => 13,
/**
* When enabled, the email notifications will send the full issue with
* a hint about the change type at the top, rather than using dedicated
* notifications that are focused on what changed. This change can be
* overridden in the database per user.
*
* @global integer $g_email_notifications_verbose
*/
'telegram_message_notifications_verbose' => OFF,
);
}
public function hooks() {
return array(
'EVENT_REPORT_BUG' => 'telegram_message_bug_added',
'EVENT_BUGNOTE_ADD' => 'telegram_message_bugnote_add',
'EVENT_UPDATE_BUG_DATA' => 'telegram_message_skip_sending',
'EVENT_UPDATE_BUG' => 'telegram_message_update_bug',
'EVENT_MENU_ACCOUNT' => 'telegram_account_page_menu'
);
}
function telegram_message_bug_added( $p_type_event, $p_issue, $p_issue_id ) {
plugin_log_event( sprintf( 'Issue #%d reported', $p_bug_id ) );
telegram_message_generic( $p_issue_id, 'new', 'telegram_message_notification_title_for_action_bug_submitted' );
}
function telegram_message_bugnote_add( $p_type_event, $p_bug_id, $p_bugnote_id ) {
global $g_skip_sending_bugnote;
if( $g_skip_sending_bugnote == TRUE ) {
$g_skip_sending_bugnote = FALSE;
return;
}
$t_bugnote_text = bugnote_get_text( $p_bugnote_id );
# Process the mentions that have access to the issue note
$t_mentioned_user_ids = mention_get_users( $t_bugnote_text );
$t_filtered_mentioned_user_ids = access_has_bugnote_level_filter(
config_get( 'view_bug_threshold' ), $p_bugnote_id, $t_mentioned_user_ids );
$t_removed_mentions_user_ids = array_diff( $t_mentioned_user_ids, $t_filtered_mentioned_user_ids );
$t_user_ids_that_got_mention_notifications = telegram_message_user_mention( $p_bug_id, $t_filtered_mentioned_user_ids, $t_bugnote_text, $t_removed_mentions_user_ids );
telegram_message_bugnote_add_generic( $p_bugnote_id, array(), $t_user_ids_that_got_mention_notifications );
}
function telegram_message_skip_sending( $p_type_event, $p_updated_bug, $p_existing_bug ) {
global $g_skip_sending_bugnote;
$g_skip_sending_bugnote = TRUE;
return $p_updated_bug;
}
function telegram_message_update_bug( $p_type_event, $p_existing_bug, $p_updated_bug ) {
# Determine whether the new status will reopen, resolve or close the issue.
# Note that multiple resolved or closed states can exist and thus we need to
# look at a range of statuses when performing this check.
$t_resolved_status = config_get( 'bug_resolved_status_threshold' );
$t_closed_status = config_get( 'bug_closed_status_threshold' );
$t_resolve_issue = false;
$t_close_issue = false;
$t_reopen_issue = false;
if( $p_existing_bug->status < $t_resolved_status &&
$p_updated_bug->status >= $t_resolved_status &&
$p_updated_bug->status < $t_closed_status
) {
$t_resolve_issue = true;
} else if( $p_existing_bug->status < $t_closed_status &&
$p_updated_bug->status >= $t_closed_status
) {
$t_close_issue = true;
} else if( $p_existing_bug->status >= $t_resolved_status &&
$p_updated_bug->status <= config_get( 'bug_reopen_status' )
) {
$t_reopen_issue = true;
}
# Send a notification of changes via email.
if( $t_resolve_issue ) {
plugin_log_event( sprintf( 'Issue #%d resolved', $p_existing_bug->id ) );
telegram_message_generic( $p_existing_bug->id, 'resolved', 'telegram_message_notification_title_for_status_bug_resolved' );
telegram_message_relationship_child_resolved( $p_existing_bug->id );
} else if( $t_close_issue ) {
plugin_log_event( sprintf( 'Issue #%d closed', $p_existing_bug->id ) );
telegram_message_generic( $p_existing_bug->id, 'closed', 'telegram_message_notification_title_for_status_bug_closed' );
telegram_message_relationship_child_closed( $p_existing_bug->id );
} else if( $t_reopen_issue ) {
plugin_log_event( sprintf( 'Issue #%d reopened', $p_existing_bug->id ) );
telegram_message_generic( $p_existing_bug->id, 'reopened', 'telegram_message_notification_title_for_action_bug_reopened' );
} else if( $p_existing_bug->handler_id != $p_updated_bug->handler_id ) {
telegram_message_owner_changed( $p_existing_bug->id, $p_existing_bug->handler_id, $p_updated_bug->handler_id );
} else if( $p_existing_bug->status != $p_updated_bug->status ) {
$t_new_status_label = MantisEnum::getLabel( config_get( 'status_enum_string' ), $p_updated_bug->status );
$t_new_status_label = str_replace( ' ', '_', $t_new_status_label );
plugin_log_event( sprintf( 'Issue #%d status changed', $p_existing_bug->id ) );
telegram_message_generic( $p_existing_bug->id, $t_new_status_label, 'telegram_message_notification_title_for_status_bug_' . $t_new_status_label );
} else {
plugin_log_event( sprintf( 'Issue #%d updated', $p_existing_bug->id ) );
telegram_message_generic( $p_existing_bug->id, 'updated', 'telegram_message_notification_title_for_action_bug_updated' );
}
}
function telegram_account_page_menu( $p_type_event ) {
global $g_account_telegram_menu_active;
if( $g_account_telegram_menu_active == TRUE ) {
return '</li><li class="active"><a href=' . plugin_page( 'account_telegram_prefs_page' ) . '>' . plugin_lang_get( 'account_telegram_prefs_page_header' ) . '</a></li><li>';
} else {
return '<a href=' . plugin_page( 'account_telegram_prefs_page' ) . '>' . plugin_lang_get( 'account_telegram_prefs_page_header' ) . '</a>';
}
}
}