Skip to content

Commit 9d501bd

Browse files
committed
[ENH]Telegram webhook interval: making it work
1 parent c9792fe commit 9d501bd

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

lib/telegram_webhook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function send($msg_count, $email_to, $webhook_token) {
3232
if (trim($curl_result)) {
3333
$response_data = json_decode($curl_result, true);
3434
if (!$response_data['ok']) {
35-
35+
Hm_Debug::add("ERRMessage not sent: ".$response_data['description']);
3636
}
3737
}
3838
}
@@ -52,7 +52,7 @@ private static function get_chat_id($webhook_token) {
5252
if(!empty($chatId = $response_data['result'][0]['message']['chat']['id'])){
5353
return $chatId;
5454
} else {
55-
Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
55+
Hm_Debug::add('ERRNo messages found. Please send a message to your bot first.<br>');
5656
return '';
5757
}
5858
}

modules/core/handler_modules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ public function process() {
679679
}
680680
$this->out('mailto_handler', $this->user_config->get('mailto_handler_setting', false));
681681
$this->out('warn_for_unsaved_changes', $this->user_config->get('warn_for_unsaved_changes_setting', false));
682+
$this->out('telegram_webhook_interval', $this->user_config->get('interval_webhook_notification_setting', false));
682683
$this->out('no_password_save', $this->user_config->get('no_password_save_setting', false));
683684
if (!strstr($this->request->server['REQUEST_URI'], 'page=') && $this->page == 'home') {
684685
$start_page = $this->user_config->get('start_page_setting', false);

modules/core/output_modules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ protected function output() {
619619
'var hm_web_root_path = function() { return "'.WEB_ROOT.'"; };'.
620620
'var hm_flag_image_src = function() { return "<i class=\"bi bi-star-half\"></i>"; };'.
621621
'var hm_check_dirty_flag = function() { return '.($this->get('warn_for_unsaved_changes', '') ? '1' : '0').'; };'.
622+
'var hm_telegram_webhook_interval = function() { return '.$this->get('telegram_webhook_interval', '').'; };'.
622623
format_data_sources($this->get('data_sources', array()), $this);
623624

624625
if (!$this->get('disable_delete_prompt')) {

modules/imap/handler_modules.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,19 +1332,17 @@ public function process() {
13321332
$this->out('imap_unread_data', $msg_list);
13331333
$this->out('imap_server_ids', $form['imap_server_ids']);
13341334

1335+
}
1336+
}
1337+
}
1338+
1339+
class Hm_Handler_send_telegram_webhook extends Hm_Handler_Module {
1340+
public function process() {
1341+
list($success, $form) = $this->process_form(array('unread_message_count'));
1342+
if ($success) {
13351343
$webhook_token = $this->user_config->get('webhook_token_setting');
1336-
$interval_webhook_notification = $this->user_config->get('interval_webhook_notification_setting');
1337-
$msg_count = count($msg_list);
1338-
$email_to = $msg_list[0]['to'];
1339-
if ($msg_count > 0) {
1340-
$interval = $interval_webhook_notification * 60;
1341-
set_time_limit(0);
1342-
while (true) {
1343-
if($webhook_token && !empty($webhook_token)) {
1344-
Hm_Telegram_Webhook::send($msg_count, $email_to, $webhook_token);
1345-
sleep($interval);
1346-
}
1347-
}
1344+
if ($form['unread_message_count'] && !empty($webhook_token)) {
1345+
Hm_Telegram_Webhook::send($form['unread_message_count'], $this->config->get('app_name'), $webhook_token);
13481346
}
13491347
}
13501348
}

modules/imap/setup.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
add_handler('ajax_imap_unread', 'save_imap_cache', true);
177177
add_output('ajax_imap_unread', 'filter_unread_data', true);
178178

179+
setup_base_ajax_page('ajax_send_telegram_webhook', 'core');
180+
add_handler('ajax_send_telegram_webhook', 'send_telegram_webhook', true, 'imap');
181+
179182
/* ajax add/remove to combined view */
180183
setup_base_ajax_page('ajax_imap_update_combined_source', 'core');
181184
add_handler('ajax_imap_update_combined_source', 'load_imap_servers_from_config', true);
@@ -335,6 +338,7 @@
335338
'ajax_imap_unsnooze',
336339
'ajax_imap_junk',
337340
'message_source',
341+
'ajax_send_telegram_webhook',
338342
),
339343

340344
'allowed_output' => array(
@@ -423,5 +427,6 @@
423427
'auto_advance_email' => FILTER_VALIDATE_BOOLEAN,
424428
'webhook_token' => FILTER_DEFAULT,
425429
'interval_webhook_notification' => FILTER_VALIDATE_INT,
430+
'unread_message_count' => FILTER_VALIDATE_INT,
426431
)
427432
);

modules/imap/site.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,15 @@ var imap_unsnooze_messages = function() {
11341134
);
11351135
}
11361136

1137+
var imap_send_telegram_webhook = function() {
1138+
Hm_Ajax.request(
1139+
[{'name': 'hm_ajax_hook', 'value': 'ajax_send_telegram_webhook'},
1140+
{'name': 'unread_message_count', 'value': Number.parseInt($('.total_unread_count').text())}
1141+
],
1142+
function() {},
1143+
);
1144+
}
1145+
11371146
if (hm_list_path() == 'sent') {
11381147
Hm_Message_List.page_caches.sent = 'formatted_sent_data';
11391148
}
@@ -1190,6 +1199,7 @@ $(function() {
11901199
if (hm_is_logged()) {
11911200
imap_unsnooze_messages();
11921201
setInterval(imap_unsnooze_messages, 60000);
1202+
setInterval(imap_send_telegram_webhook, 60000 * hm_telegram_webhook_interval());
11931203
}
11941204

11951205
if ($('.imap_move').length > 0) {

0 commit comments

Comments
 (0)