diff --git a/lib/telegram_webhook.php b/lib/telegram_webhook.php index 7642426bf..9fd3dbbd1 100644 --- a/lib/telegram_webhook.php +++ b/lib/telegram_webhook.php @@ -8,111 +8,65 @@ class Hm_Telegram_Webhook { - /* webhook_token value */ - private $webhook_token; - - private $prefix_ui = 'https://api.telegram.org/'; + const PREFIX_URI = 'https://api.telegram.org/'; - /** - * Load webhook token - * @param string $webhook_token - */ - public function __construct($webhook_token) { - $this->webhook_token = $webhook_token; - } - - // Function to send Telegram notification /** * send telegram notiofication using curl * @param array $extracted_msgs */ - public function send(array $extracted_msgs) { - // Delete the webhook - if ($this->delete_webhook($this->webhook_token)) { - // Get the chat ID - $chatId = $this->get_chat_id(); - if ($chatId) { - $text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nContent: {$extracted_msgs['body']}"; - - $curl_handle = curl_init(); - curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_uri}bot{$this->webhook_token}/sendMessage"); - curl_setopt($curl_handle, CURLOPT_POST, true); - curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text])); - curl_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]); - - $response = curl_exec($curl_handle); - if (curl_errno($ch)) { - Hm_Msgs::add('ERRError:' . curl_error($curl_handle)); - } else { - $response_data = json_decode($response, true); - if (!$response_data['ok']) { - Hm_Msgs::add('ERRFailed to send message: ' . $response_data['description']); - } + public static function send(array $extracted_msgs, $webhook_token) { + self::delete_webhook($webhook_token); + // Get the chat ID + $chatId = self::get_chat_id($webhook_token); + if (!empty($chatId)) { + $text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nTo: {$extracted_msgs['to']}"; + $curl_handle = Hm_Functions::c_init(); + Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/sendMessage'); + Hm_Functions::c_setopt($curl_handle, CURLOPT_POST, true); + Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); + Hm_Functions::c_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text])); + Hm_Functions::c_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]); + $curl_result = Hm_Functions::c_exec($curl_handle); + if (trim($curl_result)) { + $response_data = json_decode($curl_result, true); + if (!$response_data['ok']) { + } - curl_close($curl_handle); - unset($curl_handle); } } } /** * get the chat ID using webhook_token + * @param string $webhook_token */ - private function get_chat_id() { - $curl_handle = curl_init(); - curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}/bot{$this->webhook_token}/getUpdates"); - curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($curl_handle); - - if (curl_errno($curl_handle)) { - Hm_Msgs::add('ERRError:' . curl_error($curl_handle)); - return false; - } else { - $response_data = json_decode($response, true); - if ($response_data['ok']) { - if (!empty($response_data['result'])) { - $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.
'); - return false; - } + private static function get_chat_id($webhook_token) { + $curl_handle = Hm_Functions::c_init(); + Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/getUpdates'); + Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); + $curl_result = Hm_Functions::c_exec($curl_handle); + if (trim($curl_result)) { + $response_data = json_decode($curl_result, true); + if(!empty($chatId = $response_data['result'][0]['message']['chat']['id'])){ + return $chatId; } else { - Hm_Msgs::add('ERRFailed to get chat ID: ' . $response_data['description'] . '
'); - return false; + Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.
'); + return ''; } } - - curl_close($curl_handle); - unset($curl_handle); } /** * This function is usefull when trying to resend, we need to delete old webhook before we send a new one - * delete the webhook using webhook_token + * delete the webhook using webhook_token if there is one + * sometimes the webhook is not deleted, so we need to delete it manually + * and sometines we are gettiting not found error + * @param string $webhook_token */ - private function delete_webhook() { - $curl_handle = curl_init(); - curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}bot{$this->webhook_token}/delete_webhook"); - curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($curl_handle); - - if (curl_errno($curl_handle)) { - Hm_Msgs::add('ERRError:' . curl_error($curl_handle)); - return false; - } else { - $response_data = json_decode($response, true); - if ($response_data['ok']) { - Hm_Msgs::add('ERRWebhook was deleted successfully.
'); - return true; - } else { - Hm_Msgs::add('ERRFailed to delete webhook: ' . $response_data['description'] . '
'); - return false; - } - } - - curl_close($curl_handle); - unset($curl_handle); + private static function delete_webhook($webhook_token) { + $curl_handle = Hm_Functions::c_init(); + Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, static::PREFIX_URI.'bot'.$webhook_token.'/delete_webhook'); + Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); + Hm_Functions::c_exec($curl_handle); } } diff --git a/modules/imap/handler_modules.php b/modules/imap/handler_modules.php index 8243cfc27..542212f92 100644 --- a/modules/imap/handler_modules.php +++ b/modules/imap/handler_modules.php @@ -7,6 +7,8 @@ if (!defined('DEBUG_MODE')) { die(); } +require_once APP_PATH .'lib/telegram_webhook.php'; + /** * Check for attachments when forwarding a message * @subpackage imap/handler @@ -1312,14 +1314,19 @@ public function process() { $this->out('folder_status', $status); $this->out('imap_unread_data', $msg_list); $this->out('imap_server_ids', $form['imap_server_ids']); + + $webhook_token = $this->user_config->get('webhook_token_setting'); + if($webhook_token && !empty($webhook_token)) { + $extracted_msgs = array(); + foreach ($msg_list as $msg) { + $extracted_msgs['from'] = $msg['from']; + $extracted_msgs['subject'] = $msg['subject']; + $extracted_msgs['to'] = $msg['subject']; + Hm_Telegram_Webhook::send($extracted_msgs,$webhook_token); + } + } } } - - - - - - } /**