Skip to content

Commit

Permalink
Merge branch 'webhook' of github.com:Shadow243/cypht into webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Jul 14, 2024
2 parents 84fdd85 + 9d501bd commit 8ce1ea4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/telegram_webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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']);
}
}
}else{
Expand Down Expand Up @@ -69,7 +69,7 @@ public static function get_chat_id($webhook_token) {
$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.<br>');
Hm_Debug::add('ERRNo messages found. Please send a message to your bot first.<br>');
return '';
}
}
Expand Down
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($form['unread_message_count'], $this->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, '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);
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,
)
);
10 changes: 10 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': Number.parseInt($('.total_unread_count').text())}
],
function() {},
);
}

if (hm_list_path() == 'sent') {
Hm_Message_List.page_caches.sent = 'formatted_sent_data';
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8ce1ea4

Please sign in to comment.