Skip to content

Commit 78eefdc

Browse files
authored
Merge pull request #18 from brlumen/proxy-support
Proxy support
2 parents e09b9c2 + cef25a2 commit 78eefdc

File tree

10 files changed

+68
-53
lines changed

10 files changed

+68
-53
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Features
1414
- adding a comment to the bug;
1515
- mention of the user in the commentary.
1616
- Respond to chat alerts about events using the built-in function "Reply to message". The answer will be added as a comment to the bug (v. >= 1.3).
17+
- Support SOCKS5 proxy server.
1718

1819
Download
1920
--------

TelegramBot.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function register() {
2222
$this->name = 'TelegramBot';
2323
$this->description = plugin_lang_get( 'description' );
2424

25-
$this->version = '1.3.2-dev';
25+
$this->version = '1.3.3-dev';
2626
$this->requires = array(
2727
'MantisCore' => '2.14.0',
2828
);
@@ -98,6 +98,8 @@ function config() {
9898
'bot_father_url' => 'https://t.me/BotFather',
9999
'telegram_url' => 'https://telegram.me/',
100100
'download_path' => '/tmp/',
101+
'proxy_address' => '',
102+
'time_out_server_response' => 30,
101103
/**
102104
* The following two config options allow you to control who should get email
103105
* notifications on different actions/statuses. The first option

core/TelegramBot_authentication_api.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,27 @@ public static function __callStatic( $action, array $data ) {
4242

4343
}
4444

45-
function telegram_session_start() {
46-
global $g_tg;
45+
function telegram_set_webhook() {
46+
global $g_tg;
47+
telegram_session_start();
4748

48-
if( $g_tg == NULL ) {
49-
$g_tg = new \Longman\TelegramBot\Telegram( plugin_config_get( 'api_key' ), plugin_config_get( 'bot_name' ) );
50-
}
49+
return $g_tg->setWebhook( config_get_global( 'path' ) . plugin_page( 'hook', TRUE ) . '&token=' . plugin_config_get( 'api_key' ) );
50+
}
51+
52+
function telegram_session_start() {
53+
global $g_tg;
54+
55+
if( $g_tg == NULL ) {
56+
$g_tg = new \Longman\TelegramBot\Telegram( plugin_config_get( 'api_key' ), plugin_config_get( 'bot_name' ) );
57+
58+
\Longman\TelegramBot\Request::setClient( new \GuzzleHttp\Client( [
59+
'base_uri' => 'https://api.telegram.org',
60+
'timeout' => plugin_config_get( 'time_out_server_response' ),
61+
'proxy' => 'socks5://' . plugin_config_get( 'proxy_address' ),
62+
] ) );
63+
64+
$g_tg->setDownloadPath( plugin_config_get( 'download_path' ) );
65+
}
5166
}
5267

5368
function telegram_session_send_message( $p_telegram_user_id, $p_data ) {

core/TelegramBot_message_api.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ function telegram_message_user_mention( $p_bug_id, $p_mention_user_ids, $p_messa
6464
return array();
6565
}
6666

67-
// $t_tg = new \Longman\TelegramBot\Telegram( plugin_config_get( 'api_key' ), plugin_config_get( 'bot_name' ) );
68-
6967
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
7068
$t_sender_id = auth_get_current_user_id();
7169
$t_sender = user_get_name( $t_sender_id );
@@ -162,7 +160,7 @@ function telegram_message_bugnote_add_generic( $p_bugnote_id, $p_files = array()
162160
return;
163161
}
164162

165-
$t_tg = new \Longman\TelegramBot\Telegram( plugin_config_get( 'api_key' ), plugin_config_get( 'bot_name' ) );
163+
telegram_session_start();
166164

167165
ignore_user_abort( true );
168166

lang/strings_english.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ $s_plugin_TelegramBot_description = 'Adds the user interaction function to the M
2020
$s_plugin_TelegramBot_config_title = 'Settings';
2121
$s_plugin_TelegramBot_bot_name = 'Bot name (required for bot authorization when receiving messages)';
2222
$s_plugin_TelegramBot_api_key = 'API key (required for bot authorization when sending messages)';
23+
$s_plugin_TelegramBot_proxy_address_header = 'SOCKS5 proxy server address: socks5://';
24+
$s_plugin_TelegramBot_time_out_server_response_header = 'Server response timeout (30 seconds by default):';
2325
$s_plugin_TelegramBot_current_config = 'The bot will send updates to the following URL:';
2426
$s_plugin_TelegramBot_response_from_telegram = 'Telegram server response: ';
2527

lang/strings_russian.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ $s_plugin_TelegramBot_description = 'Добавляет в MantisBT функци
2020
$s_plugin_TelegramBot_config_title = 'Настройка';
2121
$s_plugin_TelegramBot_bot_name = 'Имя бота (необходимо для авторизации бота при приёме сообщений)';
2222
$s_plugin_TelegramBot_api_key = 'Ключ API (необходимо для авторизации бота при отправке сообщений)';
23+
$s_plugin_TelegramBot_proxy_address_header = 'Адрес socks5 прокси сервера: socks5://';
24+
$s_plugin_TelegramBot_time_out_server_response_header = 'Таймаут ожидания ответа сервера (по умолчанию 30 сек): ';
2325
$s_plugin_TelegramBot_current_config = 'Бот будет отправлять обновления на следующий URL:';
2426
$s_plugin_TelegramBot_response_from_telegram = 'Ответ сервера Telegram: ';
2527

lang/strings_spanish.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ $s_plugin_TelegramBot_description = 'Agrega interacción del usuario a MantisBT
2020
$s_plugin_TelegramBot_config_title = 'Settings';
2121
$s_plugin_TelegramBot_bot_name = 'Nombre del bot (requerido para la autorización del bot)';
2222
$s_plugin_TelegramBot_api_key = 'Clave de API (requerida para la autorización del bot al enviar mensajes)';
23+
$s_plugin_TelegramBot_proxy_address_header = 'Dirección del servidor proxy SOCKS5: socks5://';
24+
$s_plugin_TelegramBot_time_out_server_response_header = 'Tiempo de espera de respuesta del servidor (30 segundos de forma predeterminada):';
2325
$s_plugin_TelegramBot_current_config = 'El bot enviará actualizaciones a la siguiente URL:';
2426
$s_plugin_TelegramBot_response_from_telegram = 'Respuesta del servidor de Telegram: ';
2527

pages/config.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818
form_security_validate( 'config' );
1919

20-
$f_bot_name = gpc_get_string( 'bot_name' );
21-
$f_api_key = gpc_get_string( 'api_key' );
20+
global $g_tg;
21+
22+
$f_bot_name = gpc_get_string( 'bot_name' );
23+
$f_api_key = gpc_get_string( 'api_key' );
24+
$f_proxy_address = gpc_get_string( 'proxy_address', '' );
25+
$f_time_out_server_response = gpc_get_int( 'time_out_server_response' );
2226

2327
if( plugin_config_get( 'bot_name' ) != $f_bot_name ) {
2428
plugin_config_set( 'bot_name', $f_bot_name );
@@ -28,30 +32,23 @@
2832
plugin_config_set( 'api_key', $f_api_key );
2933
}
3034

35+
if( plugin_config_get( 'proxy_address' ) != $f_proxy_address ) {
36+
plugin_config_set( 'proxy_address', $f_proxy_address );
37+
}
38+
39+
if( plugin_config_get( 'time_out_server_response' ) != $f_time_out_server_response ) {
40+
plugin_config_set( 'time_out_server_response', $f_time_out_server_response );
41+
}
42+
3143
form_security_purge( plugin_page( 'config', true ) );
3244

3345
$t_redirect_url = plugin_page( 'config_page', true );
3446
layout_page_header( null, $t_redirect_url );
3547
layout_page_begin( $t_redirect_url );
3648

3749
try {
38-
$t_tg = new \Longman\TelegramBot\Telegram( plugin_config_get( 'api_key' ), plugin_config_get( 'bot_name' ) );
39-
$t_result = $t_tg->setWebhook( config_get_global( 'path' ) . plugin_page( 'hook', TRUE ) . '&token=' . plugin_config_get( 'api_key' ) );
40-
41-
//$t_logo_path = config_get( 'absolute_path' ) . config_get( 'logo_image' );
42-
//if( file_exists( $t_logo_path ) ) {
43-
// $t_file_content = file_get_contents( $t_logo_path );
44-
//
45-
//// $t_chat_photo = new \Longman\TelegramBot\Entities\InputMedia\InputMediaPhoto( $t_file_content );
46-
// $t_data = [];
47-
// $t_data['chat_id'] = get_telegram_user_id_from_mantis_user_id( auth_get_current_user_id() );
48-
//
49-
// $t_result = Longman\TelegramBot\Request::setChatPhoto( $t_data, $t_logo_path );
50-
////$t_result_icon = $t_tg->
51-
//}
52-
53-
html_operation_successful( $t_redirect_url, plugin_lang_get( 'response_from_telegram' ) . $t_result->getDescription() );
50+
html_operation_successful( $t_redirect_url, plugin_lang_get( 'response_from_telegram' ) . telegram_set_webhook()->getDescription() );
5451
} catch( Longman\TelegramBot\Exception\TelegramException $t_errors ) {
55-
html_operation_failure( $t_redirect_url, $t_errors->getMessage() );
52+
html_operation_failure( $t_redirect_url, $t_errors->getMessage() );
5653
}
5754
layout_page_end();

pages/config_page.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@
7575
<textarea name="api_key" id="api_key" class="form-control" rows="1" required><?php echo plugin_config_get( 'api_key' ) == NULL ? '' : plugin_config_get( 'api_key' ) ?></textarea>
7676
</td>
7777
</tr>
78+
79+
<tr <?php echo helper_alternate_class() ?>>
80+
<th class="category" width="5%">
81+
<?php echo plugin_lang_get( 'time_out_server_response_header' ) ?>
82+
</th>
83+
<td class="center" colspan="1">
84+
<input type="number" name="time_out_server_response" id="proxy_address" class="form-control" min="0" value="<?php echo plugin_config_get( 'time_out_server_response' ) == NULL ? '' : plugin_config_get( 'time_out_server_response' ) ?>">
85+
</td>
86+
</tr>
87+
88+
<tr <?php echo helper_alternate_class() ?>>
89+
<th class="category" width="5%">
90+
<?php echo plugin_lang_get( 'proxy_address_header' ) ?>
91+
</th>
92+
<td class="center" colspan="1">
93+
<textarea name="proxy_address" id="proxy_address" class="form-control" rows="1"><?php echo plugin_config_get( 'proxy_address' ) == NULL ? '' : plugin_config_get( 'proxy_address' ) ?></textarea>
94+
</td>
95+
</tr>
7896

7997
<?php
8098
$t_bot_name = plugin_config_get( 'bot_name' );
@@ -83,7 +101,7 @@
83101
if( $t_bot_name && $t_api_key ) {
84102

85103
try {
86-
$t_tg = new \Longman\TelegramBot\Telegram( $t_api_key, $t_bot_name );
104+
telegram_session_start();
87105
$t_result = Longman\TelegramBot\Request::getWebhookInfo();
88106
$t_webhook_url = $t_result->result->getUrl();
89107
} catch( Longman\TelegramBot\Exception\TelegramException $t_errors ) {

pages/hook.php

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@
1919

2020
if( plugin_config_get( 'api_key' ) && plugin_config_get( 'bot_name' ) && $f_token == plugin_config_get( 'api_key' ) ) {
2121

22-
$t_tg = new \Longman\TelegramBot\Telegram( plugin_config_get( 'api_key' ), plugin_config_get( 'bot_name' ) );
23-
24-
$t_tg->setDownloadPath( plugin_config_get( 'download_path' ) );
25-
26-
// $dwdds = $t_tg->addCommandsPath( realpath( __DIR__ . '/../core/Commands/' ));
27-
// $efvef = $t_tg->handle();
22+
telegram_session_start();
2823

2924
$post = json_decode( Longman\TelegramBot\Request::getInput(), true );
3025

31-
$t_update = new Longman\TelegramBot\Entities\Update( $post, $botname );
26+
$t_update = new Longman\TelegramBot\Entities\Update( $post );
3227
$t_update_type = $t_update->getUpdateType();
3328

3429
if( $t_update_type == 'message' ) {
@@ -179,23 +174,6 @@
179174
$t_orgl_message = $t_callback_query->getMessage();
180175
$t_data_send = telegram_action_select( $t_orgl_message->getChat()->getId(), $t_orgl_message->getMessageId() );
181176
break;
182-
// case 'get_status':
183-
// $t_bug = bug_get( $t_data['get_status']['bug_id'] );
184-
// $t_data_send = [
185-
// 'reply_markup' => keyboard_buttons_bug_change_status( $t_bug ),
186-
// ];
187-
// $t_data_send['chat_id'] = $t_message->getReplyToMessage()->getChat()->getId();
188-
// $t_data_send['message_id'] = $t_message->getMessageId();
189-
//
190-
// $t_visible_bug_data = email_build_visible_bug_data( user_get_id_by_telegram_user_id( $t_message->getReplyToMessage()->getChat()->getId() ), $t_bug->id );
191-
// $t_message = telegram_message_format_bug_message( $t_visible_bug_data, FALSE );
192-
// $t_message .= lang_get( 'bug_status_to_button' );
193-
// $t_data_send['text'] = $t_message;
194-
// break;
195-
// case 'set_status':
196-
//
197-
//
198-
// break;
199177
}
200178

201179

0 commit comments

Comments
 (0)