Skip to content

Commit

Permalink
[ENH]Telegram webhook interval: making it work
Browse files Browse the repository at this point in the history
  • Loading branch information
Baraka24 committed Jul 3, 2024
1 parent c9792fe commit 0be6008
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions modules/core/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions modules/core/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ protected function output() {
'var hm_web_root_path = function() { return "'.WEB_ROOT.'"; };'.
'var hm_flag_image_src = function() { return "<i class=\"bi bi-star-half\"></i>"; };'.
'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')) {
Expand Down
22 changes: 10 additions & 12 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($msg_count, $this->site_config->get('app_name'), $webhook_token);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions modules/imap/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, 'core');

/* 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);
Expand Down Expand Up @@ -335,6 +338,7 @@
'ajax_imap_unsnooze',
'ajax_imap_junk',
'message_source',
'ajax_send_telegram_webhook',
),

'allowed_output' => array(
Expand Down Expand Up @@ -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,
)
);
11 changes: 11 additions & 0 deletions modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': 2}
],
function() {},
);
}

if (hm_list_path() == 'sent') {
Hm_Message_List.page_caches.sent = 'formatted_sent_data';
}
Expand Down Expand Up @@ -1189,7 +1198,9 @@ $(function() {

if (hm_is_logged()) {
imap_unsnooze_messages();
imap_send_telegram_webhook();
setInterval(imap_unsnooze_messages, 60000);
//setInterval(imap_send_telegram_webhook, 60000 * hm_telegram_webhook_interval());
}

if ($('.imap_move').length > 0) {
Expand Down

0 comments on commit 0be6008

Please sign in to comment.