Custom Notification : ps

This commit is contained in:
VE10-Sanjeev 2023-09-22 19:06:22 +05:30
parent aace769f29
commit d741aa6c73
7 changed files with 389 additions and 10 deletions

View File

@ -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');

View File

@ -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);
}
}

View File

@ -1,4 +1,55 @@
<?php
function perform_whatsapp_request($url,$method,$data){
log_message('INFO',"PerformWhatsappRequest : request url = ".$url);
log_message('INFO',"PerformWhatsappRequest : request data type = ".$data);
log_message('INFO',"PerformWhatsappRequest : method = ".$method);
try{
$curl = curl_init();
$headers = array('Content-Type:application/json');
curl_setopt( $curl,CURLOPT_URL, $url);
switch ($method) {
case "POST":
curl_setopt( $curl,CURLOPT_POST, true );
if ($data) {
curl_setopt( $curl,CURLOPT_POSTFIELDS, json_encode($data));
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data) {
// print_r($data);die();
$url = sprintf("%s?%s", $url, http_build_query((array)$data));
}
}
curl_setopt( $curl,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl,CURLOPT_SSL_VERIFYPEER, true );
$curl_exec = curl_exec($curl);
log_message('INFO',"PerformWhatsappRequest : reponse = ".$curl_exec);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status === 200) {
$result = json_decode($curl_exec);
if ($result->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()." <br/>";
}
return $final_result;
}
function perform_http_request_old($method, $url, $data = false) {
$username = "ck_935889d13267b63c2341168e42ffafe3c1ee831a";
$password = "cs_6ce1321c3f08049dfb3c8098ea21137796446bc4";

View File

@ -1,5 +1,6 @@
<div class="row">
<div class="col-xl-3 col-md-6">
<a href="<?= base_url()."send_whatsapp_message"; ?>" class="btn btn-success waves-effect"> <span> <i class="mdi mdi-gesture-swipe-down"></i></span> Whatsapp Api integration </a>
<div class="card">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">

View File

@ -0,0 +1,165 @@
<style>
/* Show the form section by default */
#mailFormDiv {display: none;}
#whatsappFormDiv {display: none;}
</style>
<div class="row">
<div class="col-xl-4 col-sm-6">
<div class="card">
<div class="card-body">
<div class="media">
<div class="avatar-md mr-5">
<div class="avatar-title bg-light rounded-circle">
<img src="<?= base_url()."public/assets/images/brands/gmail.png"?>" alt="logo" class="avatar-sm rounded-circle">
</div>
</div>
<div class="media-body">
<h4 style="margin: 18px 0;"><a id="showMailForm" class="text-dark">Mail Custom Notification</a></h4>
</div>
</div>
</div>
</div>
</div>
<div class="col-xl-4 col-sm-6">
<div class="card">
<div class="card-body">
<div class="media">
<div class="avatar-md mr-5">
<div class="avatar-title bg-light rounded-circle">
<img src="<?= base_url()."public/assets/images/brands/whatsapp.png"?>" alt="logo" class="avatar-sm rounded-circle">
</div>
</div>
<div class="media-body">
<h4 style="margin: 18px 0;"><a id="showWhatsappForm" class="text-dark">Whatsapp Custom Notification</a></h4>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="mailFormDiv" class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="header-title"><?= "Mail Custom Notification" ?></h4>
<?php if (isset($validationErrors)) : ?>
<span class="widget-simple text-center">
<div class="media-body align-self-center font-24 avatar-title">
<p style="color: #f1556c!important;" class="mt-0" style><?= $validationErrors; ?></p>
</div>
</span>
<?php endif; ?>
<form role="form" class="parsley-examples" method="POST" enctype="multipart/form-data" action="<?= base_url() . "mail_notifications"; ?>">
<div class="form-group row">
<label for="recipient" class="col-4 col-form-label">Recipient Email</label>
<div class="col-7">
<input type="email" placeholder="Recipient Email" class="form-control" id="recipient" name="recipient" parsley-type="email">
</div>
</div>
<div class="form-group row">
<label for="CC" class="col-4 col-form-label">CC</label>
<div class="col-7">
<input type="text" placeholder="Enter multiple email addresses separated by commas" class="form-control" id="CC" name="carboncopy">
</div>
</div>
<div class="form-group row">
<label for="BCC" class="col-4 col-form-label">BCC</label>
<div class="col-7">
<input type="text" placeholder="Enter multiple email addresses separated by commas" class="form-control" id="BCC" name="blindcarboncopy">
</div>
</div>
<div class="form-group row">
<label for="subject" class="col-4 col-form-label">subject</label>
<div class="col-7">
<input type="text" placeholder="Subject" class="form-control" id="subject" name="subject">
</div>
</div>
<div class="form-group row">
<label for="body" class="col-4 col-form-label">Body</label>
<div class="col-7">
<textarea id="summernote-basic" name="description" class="form-control" rows="7">
<h5>Hello {User}, </h5>
<p>We create simple, flat & responsive custom mail template.</p>
<p>Please, write text here!</p>
</textarea>
</div>
</div>
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light mr-1">
Send
</button>
</div>
</div>
</form>
</div> <!-- end card-body-->
</div> <!-- end card-->
</div><!-- end col -->
</div><!-- end row -->
<div id="whatsappFormDiv" class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="header-title"><?= "Whatsapp Custom Notification" ?></h4>
<?php if (isset($validationErrors)) : ?>
<span class="widget-simple text-center">
<div class="media-body align-self-center font-24 avatar-title">
<p style="color: #f1556c!important;" class="mt-0" style><?= $validationErrors; ?></p>
</div>
</span>
<?php endif; ?>
<form role="form" class="parsley-examples" method="POST" enctype="multipart/form-data" action="<?= base_url() . "whatsapp_notifications"; ?>">
<div class="form-group row">
<label for="mobile" class="col-4 col-form-label">Mobile Number</label>
<div class="col-7">
<input type="number" placeholder="Mobile Number" class="form-control" id="recipient" name="mobile">
</div>
</div>
<div class="form-group row">
<label for="body" class="col-4 col-form-label">Message</label>
<div class="col-7">
<textarea id="summernote-basic" name="description" class="form-control" rows="7">
<h5>Hello {User}, </h5>
<p>We create simple, flat & responsive custom whatsapp template.</p>
<p>Please, write text here!</p>
</textarea>
</div>
</div>
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light mr-1">
Send
</button>
</div>
</div>
</form>
</div> <!-- end card-body-->
</div> <!-- end card-->
</div><!-- end col -->
</div><!-- end row -->
<script>
const showMailFormLink = document.getElementById('showMailForm');
const mailFormDiv = document.getElementById('mailFormDiv');
const showWhatsappFormLink = document.getElementById('showWhatsappForm');
const whatsappFormDiv = document.getElementById('whatsappFormDiv');
// Add a click event listener to the link
showMailFormLink.addEventListener('click', function () {
// Toggle the display of the div
mailFormDiv.style.display = (mailFormDiv.style.display === 'none') ? 'block' : 'none';
whatsappFormDiv.style.display = 'none';
});
// Add a click event listener to the link
showWhatsappFormLink.addEventListener('click', function () {
// Toggle the display of the div
mailFormDiv.style.display = 'none';
whatsappFormDiv.style.display = (whatsappFormDiv.style.display === 'none') ? 'block' : 'none';
});
</script>

View File

@ -467,5 +467,12 @@
<!-- App js -->
<script src="<?= base_url()."public/assets/js/app.min.js" ?>"></script>
<!-- Summernote js -->
<script src="<?= base_url()."public/assets/libs/summernote/summernote-bs4.min.js" ?>"></script>
<!-- Init js -->
<script src="<?= base_url()."public/assets/js/pages/form-summernote.init.js" ?>"></script>
</body>
</html>

View File

@ -29,6 +29,7 @@
<!-- icons -->
<link href="<?= base_url()."public/assets/css/icons.min.css" ?>" rel="stylesheet" type="text/css" />
<link href="<?= base_url()."public/assets/libs/summernote/summernote-bs4.min.css" ?>" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
@ -186,6 +187,12 @@
<span>Address Print </span>
</a>
</li>
<li>
<a href="<?= base_url()."custom_notifications"; ?>">
<i class="ri-notification-3-fill"></i>
<span> Custom Notifications </span>
</a>
</li>
</ul>