diff --git a/debug.log b/debug.log
new file mode 100644
index 0000000000..043aa4e4a0
--- /dev/null
+++ b/debug.log
@@ -0,0 +1,5 @@
+Raw cURL result: Array
+Raw cURL result: Array
+Raw cURL result: Array
+Raw cURL result: Array
+Raw cURL result: Array
diff --git a/lib/telegram_webhook.php b/lib/telegram_webhook.php
index 299658ab62..1a8bd40d36 100644
--- a/lib/telegram_webhook.php
+++ b/lib/telegram_webhook.php
@@ -20,6 +20,9 @@ public static function send($msg_count, $email_to, $webhook_token) {
self::delete_webhook($webhook_token);
// Get the chat ID
$chatId = self::get_chat_id($webhook_token);
+ // var_dump($chatId);
+ // print_r('chatId response: ');
+ // print_r($chatId);
if (!empty($chatId)) {
$text = "You have received: $msg_count unread email.s\nTo: $email_to";
$curl_handle = Hm_Functions::c_init();
@@ -35,6 +38,8 @@ public static function send($msg_count, $email_to, $webhook_token) {
}
}
+ }else{
+ Hm_Debug::add('No chat found, please check your token.');
}
}
@@ -42,20 +47,61 @@ public static function send($msg_count, $email_to, $webhook_token) {
* get the chat ID using webhook_token
* @param string $webhook_token
*/
- 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);
+ public static function get_chat_id($webhook_token) {
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, static::PREFIX_URI . 'bot' . $webhook_token . '/getUpdates');
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ $curl_result = curl_exec($ch);
+
+ if ($curl_result === false) {
+ Hm_Msgs::add('cURL Error: ' . curl_error($ch) . '
');
+ curl_close($ch);
+ return '';
+ }
+
+ curl_close($ch);
if (trim($curl_result)) {
$response_data = json_decode($curl_result, true);
- if(!empty($chatId = $response_data['result'][0]['message']['chat']['id'])){
+ file_put_contents('./debug.log', 'Raw cURL result: ' . $response_data['result'] . "\n", FILE_APPEND);
+
+ // Log the decoded response data for debugging
+ if (isset($response_data['result'][0]['message']['chat']['id']) && !empty($response_data['result'][0]['message']['chat']['id'])) {
+ $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 '';
}
}
+
+
+
+
+
+ // $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);
+ // file_put_contents('./debug.log', 'Raw cURL result: ' . $curl_result . "\n", FILE_APPEND);
+
+ // if ($curl_result === false) {
+ // // Hm_Msgs::add('cURL Error: ' . Hm_Functions::c_error($curl_handle) . '
');
+ // // Hm_Functions::c_close($curl_handle);
+ // return '';
+ // }
+
+ // // Hm_Functions::c_close($curl_handle);
+
+ // if (trim($curl_result)) {
+ // $response_data = json_decode($curl_result, true);
+ // if (isset($response_data['result'][0]['message']['chat']['id']) && !empty($response_data['result'][0]['message']['chat']['id'])) {
+ // $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 '';
+ // }
+ // }
}
/**
@@ -65,7 +111,7 @@ private static function get_chat_id($webhook_token) {
* and sometines we are gettiting not found error
* @param string $webhook_token
*/
- private static function delete_webhook($webhook_token) {
+ public 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);
diff --git a/tests/phpunit/.phpunit.cache/test-results b/tests/phpunit/.phpunit.cache/test-results
new file mode 100644
index 0000000000..b9ada4cdfa
--- /dev/null
+++ b/tests/phpunit/.phpunit.cache/test-results
@@ -0,0 +1 @@
+{"version":1,"defects":{"Hm_Test_Telegram_Webhook::test_send":8,"Hm_Test_Telegram_Webhook::test_get_chat_id":5,"Hm_Test_Telegram_Webhook::test_delete_webhook":5,"Hm_Test_Telegram_Webhook::test_send_with_correct_token":8,"Hm_Test_Telegram_Webhook::test_send_with_bad_token":8,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_bad_token":8,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_correct_token":8},"times":{"Hm_Test_Telegram_Webhook::test_get_chat_id":0.001,"Hm_Test_Telegram_Webhook::test_delete_webhook":0.002,"Hm_Test_Telegram_Webhook::test_send":0.004,"Hm_Test_Telegram_Webhook::test_send_with_bad_token":2.015,"Hm_Test_Telegram_Webhook::test_send_with_correct_token":2.014,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_bad_token":2.013,"Hm_Test_Telegram_Webhook::test_get_chat_id_with_correct_token":0.885}}
\ No newline at end of file
diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php
index 46796714e7..584b44cde4 100644
--- a/tests/phpunit/bootstrap.php
+++ b/tests/phpunit/bootstrap.php
@@ -24,6 +24,7 @@
/* get the framework */
require APP_PATH.'lib/framework.php';
+require APP_PATH.'lib/telegram_webhook.php';
/* get the stubs */
require APP_PATH.'tests/phpunit/stubs.php';
diff --git a/tests/phpunit/phpunit.xml b/tests/phpunit/phpunit.xml
index f4b7b4d657..78f6ba6745 100644
--- a/tests/phpunit/phpunit.xml
+++ b/tests/phpunit/phpunit.xml
@@ -64,6 +64,9 @@
./transform.php
+
+ ./telegram_webhook.php
+
./auth.php
diff --git a/tests/phpunit/telegram_webhook.php b/tests/phpunit/telegram_webhook.php
new file mode 100644
index 0000000000..ff1eaaa384
--- /dev/null
+++ b/tests/phpunit/telegram_webhook.php
@@ -0,0 +1,74 @@
+telegram_webhook = new Hm_Telegram_Webhook();
+ }
+ /**
+ * @preserveGlobalState disabled
+ * @runInSeparateProcess
+ */
+ // public function test_send_with_bad_token() {
+ // $res = $this->telegram_webhook::send(0, 'demo@cypht.org', 'testtoken');
+ // sleep(2);
+ // return $this->assertNull($res);
+ // }
+ /**
+ * @preserveGlobalState disabled
+ * @runInSeparateProcess
+ */
+ // public function test_send_with_correct_token() {
+ // $res = $this->telegram_webhook::send(0, 'demo@cypht.org', '7253394590:AAHIY9VmoH3uTRFtEwmlfRPGyKoZIQMCX5A');
+ // sleep(2);
+ // return $this->assertNull($res);
+ // }
+ /**
+ * @preserveGlobalState disabled
+ * @runInSeparateProcess
+ */
+ // public function test_get_chat_id_with_bad_token() {
+ // $res = $this->telegram_webhook::get_chat_id('testtoken');
+ // sleep(2);
+ // return $this->assertEmpty($res);
+ // }
+
+ /**
+ * @preserveGlobalState disabled
+ * @runInSeparateProcess
+ */
+ public function test_get_chat_id_with_correct_token() {
+ // $res = $this->telegram_webhook::get_chat_id('6132059962:AAHrfhsq9kkg1CK3FsAFRaK4l5zfoCWu14s');
+ // sleep(2);
+ // var_dump($res);
+ // return $this->assertEmpty($res);
+
+ $chatId = '';
+ $maxAttempts = 5;
+ $attempt = 0;
+
+ while (empty($chatId) && $attempt < $maxAttempts) {
+ $chatId = Hm_Telegram_Webhook::get_chat_id('6132059962:AAHrfhsq9kkg1CK3FsAFRaK4l5zfoCWu14s');
+ if (empty($chatId)) {
+ sleep(1); // Wait for 1 second before retrying
+ }
+ $attempt++;
+ }
+ var_dump($chatId);
+ // $this->assertNotEmpty($chatId, 'Chat ID should not be empty');
+ }
+ /**
+ * @preserveGlobalState disabled
+ * @runInSeparateProcess
+ */
+ // public function test_delete_webhook() {
+ // $this->telegram_webhook::delete_webhook('testtoken');
+ // }
+}