From 9d501bd12f8fa9f5f15cad9f63b503bb693c224a Mon Sep 17 00:00:00 2001 From: Baraka Kinywa Date: Wed, 3 Jul 2024 09:12:09 +0200 Subject: [PATCH] [ENH]Telegram webhook interval: making it work --- lib/telegram_webhook.php | 4 ++-- modules/core/handler_modules.php | 1 + modules/core/output_modules.php | 1 + modules/imap/handler_modules.php | 22 ++++++++++------------ modules/imap/setup.php | 5 +++++ modules/imap/site.js | 10 ++++++++++ 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/telegram_webhook.php b/lib/telegram_webhook.php index 299658ab62..1706b7e299 100644 --- a/lib/telegram_webhook.php +++ b/lib/telegram_webhook.php @@ -32,7 +32,7 @@ public static function send($msg_count, $email_to, $webhook_token) { if (trim($curl_result)) { $response_data = json_decode($curl_result, true); if (!$response_data['ok']) { - + Hm_Debug::add("ERRMessage not sent: ".$response_data['description']); } } } @@ -52,7 +52,7 @@ private static function get_chat_id($webhook_token) { if(!empty($chatId = $response_data['result'][0]['message']['chat']['id'])){ return $chatId; } else { - Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.
'); + Hm_Debug::add('ERRNo messages found. Please send a message to your bot first.
'); return ''; } } diff --git a/modules/core/handler_modules.php b/modules/core/handler_modules.php index adf089e71c..f2494e5809 100644 --- a/modules/core/handler_modules.php +++ b/modules/core/handler_modules.php @@ -679,6 +679,7 @@ public function process() { } $this->out('mailto_handler', $this->user_config->get('mailto_handler_setting', false)); $this->out('warn_for_unsaved_changes', $this->user_config->get('warn_for_unsaved_changes_setting', false)); + $this->out('telegram_webhook_interval', $this->user_config->get('interval_webhook_notification_setting', false)); $this->out('no_password_save', $this->user_config->get('no_password_save_setting', false)); if (!strstr($this->request->server['REQUEST_URI'], 'page=') && $this->page == 'home') { $start_page = $this->user_config->get('start_page_setting', false); diff --git a/modules/core/output_modules.php b/modules/core/output_modules.php index 81284bbce7..a98dcadd35 100644 --- a/modules/core/output_modules.php +++ b/modules/core/output_modules.php @@ -619,6 +619,7 @@ protected function output() { 'var hm_web_root_path = function() { return "'.WEB_ROOT.'"; };'. 'var hm_flag_image_src = function() { return ""; };'. 'var hm_check_dirty_flag = function() { return '.($this->get('warn_for_unsaved_changes', '') ? '1' : '0').'; };'. + 'var hm_telegram_webhook_interval = function() { return '.$this->get('telegram_webhook_interval', '').'; };'. format_data_sources($this->get('data_sources', array()), $this); if (!$this->get('disable_delete_prompt')) { diff --git a/modules/imap/handler_modules.php b/modules/imap/handler_modules.php index 65d1ae7c86..a25ab72fac 100644 --- a/modules/imap/handler_modules.php +++ b/modules/imap/handler_modules.php @@ -1332,19 +1332,17 @@ public function process() { $this->out('imap_unread_data', $msg_list); $this->out('imap_server_ids', $form['imap_server_ids']); + } + } +} + +class Hm_Handler_send_telegram_webhook extends Hm_Handler_Module { + public function process() { + list($success, $form) = $this->process_form(array('unread_message_count')); + if ($success) { $webhook_token = $this->user_config->get('webhook_token_setting'); - $interval_webhook_notification = $this->user_config->get('interval_webhook_notification_setting'); - $msg_count = count($msg_list); - $email_to = $msg_list[0]['to']; - if ($msg_count > 0) { - $interval = $interval_webhook_notification * 60; - set_time_limit(0); - while (true) { - if($webhook_token && !empty($webhook_token)) { - Hm_Telegram_Webhook::send($msg_count, $email_to, $webhook_token); - sleep($interval); - } - } + if ($form['unread_message_count'] && !empty($webhook_token)) { + Hm_Telegram_Webhook::send($form['unread_message_count'], $this->config->get('app_name'), $webhook_token); } } } diff --git a/modules/imap/setup.php b/modules/imap/setup.php index 509a41f01c..9e5415ce0e 100644 --- a/modules/imap/setup.php +++ b/modules/imap/setup.php @@ -176,6 +176,9 @@ add_handler('ajax_imap_unread', 'save_imap_cache', true); add_output('ajax_imap_unread', 'filter_unread_data', true); +setup_base_ajax_page('ajax_send_telegram_webhook', 'core'); +add_handler('ajax_send_telegram_webhook', 'send_telegram_webhook', true, 'imap'); + /* ajax add/remove to combined view */ setup_base_ajax_page('ajax_imap_update_combined_source', 'core'); add_handler('ajax_imap_update_combined_source', 'load_imap_servers_from_config', true); @@ -335,6 +338,7 @@ 'ajax_imap_unsnooze', 'ajax_imap_junk', 'message_source', + 'ajax_send_telegram_webhook', ), 'allowed_output' => array( @@ -423,5 +427,6 @@ 'auto_advance_email' => FILTER_VALIDATE_BOOLEAN, 'webhook_token' => FILTER_DEFAULT, 'interval_webhook_notification' => FILTER_VALIDATE_INT, + 'unread_message_count' => FILTER_VALIDATE_INT, ) ); diff --git a/modules/imap/site.js b/modules/imap/site.js index 711c36b530..f253797890 100644 --- a/modules/imap/site.js +++ b/modules/imap/site.js @@ -1134,6 +1134,15 @@ var imap_unsnooze_messages = function() { ); } +var imap_send_telegram_webhook = function() { + Hm_Ajax.request( + [{'name': 'hm_ajax_hook', 'value': 'ajax_send_telegram_webhook'}, + {'name': 'unread_message_count', 'value': Number.parseInt($('.total_unread_count').text())} + ], + function() {}, + ); +} + if (hm_list_path() == 'sent') { Hm_Message_List.page_caches.sent = 'formatted_sent_data'; } @@ -1190,6 +1199,7 @@ $(function() { if (hm_is_logged()) { imap_unsnooze_messages(); setInterval(imap_unsnooze_messages, 60000); + setInterval(imap_send_telegram_webhook, 60000 * hm_telegram_webhook_interval()); } if ($('.imap_move').length > 0) {