Skip to content

Commit dd13721

Browse files
committed
[REF] Refactor webhook codebase and fix small issues
1 parent d45af97 commit dd13721

File tree

2 files changed

+53
-89
lines changed

2 files changed

+53
-89
lines changed

lib/telegram_webhook.php

Lines changed: 40 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,111 +8,68 @@
88

99
class Hm_Telegram_Webhook {
1010

11-
/* webhook_token value */
12-
private $webhook_token;
11+
private static $prefix_uri = 'https://api.telegram.org/';
1312

14-
private $prefix_ui = 'https://api.telegram.org/';
15-
16-
/**
17-
* Load webhook token
18-
* @param string $webhook_token
19-
*/
20-
public function __construct($webhook_token) {
21-
$this->webhook_token = $webhook_token;
22-
}
23-
24-
// Function to send Telegram notification
2513
/**
2614
* send telegram notiofication using curl
2715
* @param array $extracted_msgs
2816
*/
29-
public function send(array $extracted_msgs) {
17+
public static function send(array $extracted_msgs, $webhook_token) {
3018
// Delete the webhook
31-
if ($this->delete_webhook($this->webhook_token)) {
32-
// Get the chat ID
33-
$chatId = $this->get_chat_id();
34-
if ($chatId) {
35-
$text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nContent: {$extracted_msgs['body']}";
36-
37-
$curl_handle = curl_init();
38-
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_uri}bot{$this->webhook_token}/sendMessage");
39-
curl_setopt($curl_handle, CURLOPT_POST, true);
40-
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
41-
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text]));
42-
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]);
43-
44-
$response = curl_exec($curl_handle);
45-
if (curl_errno($ch)) {
46-
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
47-
} else {
48-
$response_data = json_decode($response, true);
49-
if (!$response_data['ok']) {
50-
Hm_Msgs::add('ERRFailed to send message: ' . $response_data['description']);
51-
}
19+
self::delete_webhook($webhook_token);
20+
// Get the chat ID
21+
$chatId = self::get_chat_id($webhook_token);
22+
if (!empty($chatId)) {
23+
$text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nTo: {$extracted_msgs['to']}";
24+
$curl_handle = Hm_Functions::c_init();
25+
Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, self::$prefix_uri.'bot'.$webhook_token.'/sendMessage');
26+
Hm_Functions::c_setopt($curl_handle, CURLOPT_POST, true);
27+
Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
28+
Hm_Functions::c_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text]));
29+
Hm_Functions::c_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]);
30+
$curl_result = Hm_Functions::c_exec($curl_handle);
31+
if (trim($curl_result)) {
32+
$response_data = json_decode($curl_result, true);
33+
if (!$response_data['ok']) {
34+
5235
}
53-
curl_close($curl_handle);
54-
unset($curl_handle);
5536
}
5637
}
5738
}
5839

5940
/**
6041
* get the chat ID using webhook_token
42+
* @param string $webhook_token
6143
*/
62-
private function get_chat_id() {
63-
$curl_handle = curl_init();
64-
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}/bot{$this->webhook_token}/getUpdates");
65-
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
66-
$response = curl_exec($curl_handle);
67-
68-
if (curl_errno($curl_handle)) {
69-
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
70-
return false;
71-
} else {
72-
$response_data = json_decode($response, true);
73-
if ($response_data['ok']) {
74-
if (!empty($response_data['result'])) {
75-
$chatId = $response_data['result'][0]['message']['chat']['id'];
76-
return $chatId;
77-
} else {
78-
Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
79-
return false;
80-
}
44+
private static function get_chat_id($webhook_token) {
45+
// $uri = ;
46+
$curl_handle = Hm_Functions::c_init();
47+
Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, self::$prefix_uri.'bot'.$webhook_token.'/getUpdates');
48+
Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
49+
$curl_result = Hm_Functions::c_exec($curl_handle);
50+
if (trim($curl_result)) {
51+
$response_data = json_decode($curl_result, true);
52+
if(!empty($chatId = $response_data['result'][0]['message']['chat']['id'])){
53+
return $chatId;
8154
} else {
82-
Hm_Msgs::add('ERRFailed to get chat ID: ' . $response_data['description'] . '<br>');
83-
return false;
55+
Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
56+
return '';
8457
}
8558
}
86-
87-
curl_close($curl_handle);
88-
unset($curl_handle);
8959
}
9060

9161
/**
9262
* This function is usefull when trying to resend, we need to delete old webhook before we send a new one
93-
* delete the webhook using webhook_token
63+
* delete the webhook using webhook_token if there is one
64+
* sometimes the webhook is not deleted, so we need to delete it manually
65+
* and sometines we are gettiting not found error
66+
* @param string $webhook_token
9467
*/
95-
private function delete_webhook() {
68+
private static function delete_webhook($webhook_token) {
69+
$prefix_uri = self::$prefix_uri;
9670
$curl_handle = curl_init();
97-
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}bot{$this->webhook_token}/delete_webhook");
98-
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
99-
$response = curl_exec($curl_handle);
100-
101-
if (curl_errno($curl_handle)) {
102-
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
103-
return false;
104-
} else {
105-
$response_data = json_decode($response, true);
106-
if ($response_data['ok']) {
107-
Hm_Msgs::add('ERRWebhook was deleted successfully.<br>');
108-
return true;
109-
} else {
110-
Hm_Msgs::add('ERRFailed to delete webhook: ' . $response_data['description'] . '<br>');
111-
return false;
112-
}
113-
}
114-
115-
curl_close($curl_handle);
116-
unset($curl_handle);
71+
Hm_Functions::c_setopt($curl_handle, CURLOPT_URL, "{$prefix_uri}bot{$webhook_token}/delete_webhook");
72+
Hm_Functions::c_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
73+
Hm_Functions::c_exec($curl_handle);
11774
}
11875
}

modules/imap/handler_modules.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
if (!defined('DEBUG_MODE')) { die(); }
99

10+
require_once APP_PATH .'lib/telegram_webhook.php';
11+
1012
/**
1113
* Check for attachments when forwarding a message
1214
* @subpackage imap/handler
@@ -1312,14 +1314,19 @@ public function process() {
13121314
$this->out('folder_status', $status);
13131315
$this->out('imap_unread_data', $msg_list);
13141316
$this->out('imap_server_ids', $form['imap_server_ids']);
1317+
1318+
$webhook_token = $this->user_config->get('webhook_token_setting');
1319+
if($webhook_token && !empty($webhook_token)) {
1320+
$extracted_msgs = array();
1321+
foreach ($msg_list as $msg) {
1322+
$extracted_msgs['from'] = $msg['from'];
1323+
$extracted_msgs['subject'] = $msg['subject'];
1324+
$extracted_msgs['to'] = $msg['subject'];
1325+
Hm_Telegram_Webhook::send($extracted_msgs,$webhook_token);
1326+
}
1327+
}
13151328
}
13161329
}
1317-
1318-
1319-
1320-
1321-
1322-
13231330
}
13241331

13251332
/**

0 commit comments

Comments
 (0)