From d741aa6c7323aefa12980f174460ee1ec5f734f3 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Fri, 22 Sep 2023 19:06:22 +0530 Subject: [PATCH] Custom Notification : ps --- app/Config/Routes.php | 6 +- app/Controllers/Notifications.php | 162 +++++++++++++++++++++++-- app/Helpers/apiIntegration_helper.php | 51 ++++++++ app/Views/dashboard.php | 1 + app/Views/notifications_form.php | 165 ++++++++++++++++++++++++++ app/Views/template/footer.php | 7 ++ app/Views/template/header.php | 7 ++ 7 files changed, 389 insertions(+), 10 deletions(-) create mode 100644 app/Views/notifications_form.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 5614846a..fda467f0 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -104,7 +104,11 @@ $routes->add('approve_invoice/(:num)', 'Invoice::approve_invoice/$1'); $routes->get('generate_invoice_pdf/(:num)', 'Invoice::generate_invoice_pdf/$1'); -$routes->get('whatsapp/send/(:any)/(:any)', 'Notifications::index/$1/$2'); +# Api integration Routes +$routes->get('send_whatsapp_message/', 'Notifications::send_whatsapp_message'); +$routes->post('whatsapp_notifications/', 'Notifications::whatsapp_notifications'); + +$routes->match(['post', 'get'], 'custom_notifications', 'Notifications::mail_notifications'); # Api integration Routes $routes->post('api/(:any)', 'ApiIntegration::api_integration/$1'); diff --git a/app/Controllers/Notifications.php b/app/Controllers/Notifications.php index 00497dc9..39ea8f96 100755 --- a/app/Controllers/Notifications.php +++ b/app/Controllers/Notifications.php @@ -2,20 +2,164 @@ namespace App\Controllers; +use CodeIgniter\RESTful\ResourceController; +use CodeIgniter\API\ResponseTrait; +use App\Models\ApiIntegrationModel; + class Notifications extends BaseController { - - public function index($phoneNumber,$message) - { - // Sanitize phone number to remove special characters and spaces - $phoneNumber = preg_replace('/[^0-9]/', '', $phoneNumber); + // Define global variables as class properties + // protected $instance_id = ''; + // protected $instance_id = "650C050D9D849"; + // protected $access_token = "650be030cedb3"; + protected $instance_id = ""; + protected $access_token = ""; - // Construct the WhatsApp link - $whatsappLink = "https://wa.me/{$phoneNumber}?text=" . urlencode($message); + ## Whatsapp Section : + // public function get_whatsapp_instance() { + // helper('apiIntegration'); + // $instance_id = ""; + // try { + // $response = get_whatsapp_instance($this->access_token); + // $this->logger->info("Whatsapp : get instance response type = ".gettype($response)); + // if(isset($response) && !empty($response)){ + // $this->logger->info("Whatsapp : get instance message = ".$response->message); + // if($response->status == "success"){ + // $instance_id = $response->instance_id; + // $this->logger->info("Whatsapp : instance id = ".$response->instance_id); + // } + // else{ + // $this->logger->error("Whatsapp : get instance Err = ".$response->error." Err Message = ".$response->error_msg); + // throw new \Exception("Err = ".$response->error." Err Message = ".$response->error_msg); + // } + // } + // } catch (\Exception $e) { + // $this->logger->error("Whatsapp : get instance exception", ['exception' => $e]); + // } + // return $instance_id; + // } + // public function get_whatsapp_instance($url){ + // $response = get_whatsapp_instance($url); + // } - // Redirect to the WhatsApp link - return redirect()->to($whatsappLink); + public function send_whatsapp_message() { + helper('apiIntegration'); + $params = (object) Null; + $this->access_token = "650be030cedb3"; + // $this->instance_id = $this->get_whatsapp_instance(); + $this->instance_id = "650C383B67BC7"; + try { + $this->logger->info("Whatsapp : sending message instance id = ".$this->instance_id); + $params->number = 916369084112; + $params->type = "text"; + $params->message = "TestingFromVBPYUIO"; + $params->instance_id = $this->instance_id; + $params->access_token = $this->access_token; + $this->logger->info("Whatsapp : sending message request type b4 = ".gettype($params)); + //$url = "https://waai.in/api/send?number=916369084112&type=text&message=helper&instance_id=650C383B67BC7&access_token=650be030cedb3"; + $url = "https://waai.in/api/send"; + $message = perform_whatsapp_request($url,"POST",$params); + // https://waai.in/api/send?number=91639084112&type=text&message=helper&instance_id=650C383B67BC7&access_token=650be030cedb3/r/n{"status":"success","message":{"key":{"remoteJid":"91639084112@c.us","fromMe":true,"id":"BAE540383CE30F06"},"message":{"extendedTextMessage":{"text":"helper"}},"messageTimestamp":"1695362314"}} + // {"status":"success","message":{"key":{"remoteJid":"916369084112@c.us","fromMe":true,"id":"BAE500BBE10A0AC0"},"message":{"extendedTextMessage":{"text":"TestingFromVBP"}},"messageTimestamp":"1695362020"}} + // {"status":"error","message":"Access token is required"} + + } catch (\Exception $e) { + $this->logger->error("An error occurred: " . $e->getMessage()); + $message = "An error occurred: " . $e->getMessage(); + } + return $message; + } + + ## Email Section : + public function mail_notifications() + { $successMessage = session()->getFlashdata('success'); + $validationErrors = session()->getFlashdata('error'); + $data['page_name'] = 'Custom Notifications'; + $request = \Config\Services::request(); + if($request->is('post')){ + $records = $request->getVar(); + $cc = isset($records['carboncopy']) ? $records['carboncopy'] : ''; + $bcc = isset($records['blindcarboncopy']) ? $records['blindcarboncopy'] : ''; + try { + + $email = \Config\Services::email(); + $email->setTo($records['recipient']); + $email->setFrom('no-reply@tripapprovaltool.com', isset($records['company'])?$records['company']:"VBP"); + $email->setSubject($records['subject']); + $email->setMessage($records['description']); + + if (!empty($cc)) { + $ccRecipients = explode(',', $cc); + foreach ($ccRecipients as $ccRecipient) { + $email->setCC(trim($ccRecipient)); + } + } + + if (!empty($bcc)) { + $bccRecipients = explode(',', $bcc); + foreach ($bccRecipients as $bccRecipient) { + $email->setBCC(trim($bccRecipient)); + } + } + + if ($email->send()) { + $this->logger->info('Email sent successfully'); + $successMessage = 'Email sent successfully'; + } else { + throw new \Exception('Email not sent. Please try again.'); + } + } catch (\Exception $e) { + $this->logger->error('Error sending email: ' . $e->getMessage()); + $validationErrors = 'Error sending email: ' . $e->getMessage(); + } + } + + $data['successMessage'] = $successMessage; + $data['validationErrors'] = $validationErrors; + + $this->render_page('notifications_form', $data); + } + + public function whatsapp_notifications(){ + $successMessage = session()->getFlashdata('success'); + $validationErrors = session()->getFlashdata('error'); + $data['page_name'] = 'Custom Notifications'; + helper('apiIntegration'); + $request = \Config\Services::request(); + if($request->is('post')){ + $records = $request->getVar(); + $params = (object) Null; + $this->access_token = "650be030cedb3"; + $this->instance_id = "650C383B67BC7"; + $this->logger->info("Whatsapp : sending message instance id = ".$this->instance_id); + try { + $this->logger->info("Whatsapp : sending message instance id = ".$this->instance_id); + $params->number = (int)'91'.$records['mobile']; + $params->type = "text"; + $params->message = $records['description']; + $params->instance_id = $this->instance_id; + $params->access_token = $this->access_token; + $this->logger->info("Whatsapp : sending message request type b4 = ".gettype($params)); + //$url = "https://waai.in/api/send?number=916369084112&type=text&message=helper&instance_id=650C383B67BC7&access_token=650be030cedb3"; + $url = "https://waai.in/api/send"; + $successMessage = perform_whatsapp_request($url,"POST",$params); + // https://waai.in/api/send?number=91639084112&type=text&message=helper&instance_id=650C383B67BC7&access_token=650be030cedb3/r/n{"status":"success","message":{"key":{"remoteJid":"91639084112@c.us","fromMe":true,"id":"BAE540383CE30F06"},"message":{"extendedTextMessage":{"text":"helper"}},"messageTimestamp":"1695362314"}} + // {"status":"success","message":{"key":{"remoteJid":"916369084112@c.us","fromMe":true,"id":"BAE500BBE10A0AC0"},"message":{"extendedTextMessage":{"text":"TestingFromVBP"}},"messageTimestamp":"1695362020"}} + // {"status":"error","message":"Access token is required"} + + } catch (\Exception $e) { + $this->logger->error("An error occurred: " . $e->getMessage()); + $validationErrors = "An error occurred: " . $e->getMessage(); + } + + } + + + $data['successMessage'] = $successMessage; + $data['validationErrors'] = $validationErrors; + + $this->render_page('notifications_form', $data); } } diff --git a/app/Helpers/apiIntegration_helper.php b/app/Helpers/apiIntegration_helper.php index 88096f2d..731b723c 100644 --- a/app/Helpers/apiIntegration_helper.php +++ b/app/Helpers/apiIntegration_helper.php @@ -1,4 +1,55 @@ status === "error") { + log_message('ERROR',"PerformWhatsappRequest : error = ".$result->message); + throw new Exception($result->message); + }else{ + $final_result = "Success"; + } + }else{ + $curl_errno= curl_errno($curl); + log_message('ERROR',"PerformWhatsappRequest : curl error = ".$curl_errno); + throw new Error("Errno returned ".$curl_errno); + } + curl_close($curl); + }catch (Exception $e) { + $final_result = "Exception Errno returned".$e->getMessage()."
"; + } + return $final_result; +} + function perform_http_request_old($method, $url, $data = false) { $username = "ck_935889d13267b63c2341168e42ffafe3c1ee831a"; $password = "cs_6ce1321c3f08049dfb3c8098ea21137796446bc4"; diff --git a/app/Views/dashboard.php b/app/Views/dashboard.php index 157cc6c0..ea29ff20 100644 --- a/app/Views/dashboard.php +++ b/app/Views/dashboard.php @@ -1,5 +1,6 @@
+ " class="btn btn-success waves-effect"> Whatsapp Api integration
diff --git a/app/Views/notifications_form.php b/app/Views/notifications_form.php new file mode 100644 index 00000000..a8723f84 --- /dev/null +++ b/app/Views/notifications_form.php @@ -0,0 +1,165 @@ + +
+
+
+
+
+
+
+ " alt="logo" class="avatar-sm rounded-circle"> +
+
+ +
+
+
+
+
+
+
+
+
+
+ " alt="logo" class="avatar-sm rounded-circle"> +
+
+ +
+
+
+
+
+ +
+
+
+
+

+ + +
+

+
+
+ + +
"> +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+

+ + +
+

+
+
+ + +
"> +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+ +
+
+
+
+ + \ No newline at end of file diff --git a/app/Views/template/footer.php b/app/Views/template/footer.php index d9156580..fdfadf58 100644 --- a/app/Views/template/footer.php +++ b/app/Views/template/footer.php @@ -467,5 +467,12 @@ + + + + + + + diff --git a/app/Views/template/header.php b/app/Views/template/header.php index dd774f1b..619f56c3 100644 --- a/app/Views/template/header.php +++ b/app/Views/template/header.php @@ -29,6 +29,7 @@ " rel="stylesheet" type="text/css" /> + " rel="stylesheet" type="text/css" /> @@ -186,6 +187,12 @@ Address Print +
  • + "> + + Custom Notifications + +