[Vtigercrm-developers] Does anything wrong in below code?
jacs
kiranraju.jakka at gmail.com
Tue Apr 19 03:31:59 GMT 2016
Hi,
I'm adding new provider to SMS Notifier, by doing so i added IndiaSMS.php
under SMSNotifier/providers/ this folder the file seems to be like this,
/* start of IndiaSMS.php */
<?php
class SMSNotifier_IndiaSMS_Provider implements
SMSNotifier_ISMSProvider_Model {
private $userName;
private $password;
private $parameters = array();
const SERVICE_URI = 'https://app.indiasms.com/sendsms/sendsms.php?';
private static $REQUIRED_PARAMETERS = array('Sender Id','Character Set');
/**
* Function to get provider name
* @return <String> provider name
*/
public function getName() {
return 'IndiaSMS';
}
/**
* Function to get required parameters other than (userName, password)
* @return <array> required parameters list
*/
public function getRequiredParams() {
return self::$REQUIRED_PARAMETERS;
}
/**
* Function to get service URL to use for a given type
* @param <String> $type like SEND, PING, QUERY
*/
public function getServiceURL($type = false) {
if($type) {
switch(strtoupper($type)) {
case self::SERVICE_AUTH: return self::SERVICE_URI . '/http/auth';
case self::SERVICE_SEND: return self::SERVICE_URI . '/http/send';
case self::SERVICE_QUERY: return self::SERVICE_URI . '/http/querymsg';
}
}
return false;
}
/**
* Function to set authentication parameters
* @param <String> $userName
* @param <String> $password
*/
public function setAuthParameters($userName, $password) {
$this->userName = $userName;
$this->password = $password;
}
/**
* Function to set non-auth parameter.
* @param <String> $key
* @param <String> $value
*/
public function setParameter($key, $value) {
$this->parameters[$key] = $value;
}
/**
* Function to get parameter value
* @param <String> $key
* @param <String> $defaultValue
* @return <String> value/$default value
*/
public function getParameter($key, $defaultValue = false) {
if(isset($this->parameters[$key])) {
return $this->parameters[$key];
}
return $defaultValue;
}
/**
* Function to prepare parameters
* @return <Array> parameters
*/
protected function prepareParameters() {
$params = array('user' => $this->userName, 'password' => $this->password);
foreach (self::$REQUIRED_PARAMETERS as $key) {
$params[$key] = $this->getParameter($key);
}
return $params;
}
/**
* Function to handle SMS Send operation
* @param <String> $message
* @param <Mixed> $toNumbers One or Array of numbers
*/
public function send($message, $toNumbers) {
global $log;
if(!is_array($toNumbers)) {
$toNumbers = array($toNumbers);
}
$params = $this->prepareParameters();
$params['text'] = $message;
$params['to'] = implode(',', $toNumbers);
$log->debug('params array
values'.$params['user'].$params['password'].$params['text'].$params['to']);
$serviceURL = $this->getServiceURL(self::SERVICE_SEND);
$httpClient = new Vtiger_Net_Client($serviceURL);
$response = $httpClient->doPost($params);
$log->debug('dopost result'.$response);
$responseLines = split("\n", $response);
$results = array();
$i=0;
foreach($responseLines as $responseLine) {
$responseLine = trim($responseLine);
if(empty($responseLine)) continue;
$result = array( 'error' => false, 'statusmessage' => '' );
if(preg_match("/ERR:(.*)/", trim($responseLine), $matches)) {
$result['error'] = true;
$result['to'] = $toNumbers[$i++];
$result['statusmessage'] = $matches[0]; // Complete error message
} else if(preg_match("/ID: ([^ ]+)TO:(.*)/", $responseLine, $matches)) {
$result['id'] = trim($matches[1]);
$result['to'] = trim($matches[2]);
$result['status'] = self::MSG_STATUS_PROCESSING;
} else if(preg_match("/ID: (.*)/", $responseLine, $matches)) {
$result['id'] = trim($matches[1]);
$result['to'] = $toNumbers[0];
$result['status'] = self::MSG_STATUS_PROCESSING;
}
$results[] = $result;
}
return $results;
}
/**
* Function to get query for status using messgae id
* @param <Number> $messageId
*/
public function query($messageId) {
$params = $this->prepareParameters();
$params['apimsgid'] = $messageId;
$serviceURL = $this->getServiceURL(self::SERVICE_QUERY);
$httpClient = new Vtiger_Net_Client($serviceURL);
$response = $httpClient->doPost($params);
$response = trim($response);
$log->debug('reply from query method'.$response);
$result = array( 'error' => false, 'needlookup' => 1, 'statusmessage' =>
'' );
if(preg_match("/ERR: (.*)/", $response, $matches)) {
$result['error'] = true;
$result['needlookup'] = 0;
$result['statusmessage'] = $matches[0];
} else if(preg_match("/ID: ([^ ]+) Status: ([^ ]+)/", $response,
$matches)) {
$result['id'] = trim($matches[1]);
$status = trim($matches[2]);
// Capture the status code as message by default.
$result['statusmessage'] = "CODE: $status";
if($status == '002' || $status == '008' || $status == '011' ) {
$result['status'] = self::MSG_STATUS_PROCESSING;
} else if($status == '003' || $status == '004') {
$result['status'] = self::MSG_STATUS_DISPATCHED;
$result['needlookup'] = 0;
} else {
$statusMessage = '';
switch($status) {
case '001': $statusMessage = 'Message unknown'; break;
case '005': $statusMessage = 'Error with message'; break;
case '006': $statusMessage = 'User cancelled message delivery'; break;
case '007': $statusMessage = 'Error delivering message'; break;
case '009': $statusMessage = 'Routing error'; break;
case '010': $statusMessage = 'Message expired'; break;
case '012': $statusMessage = 'Out of credit'; break;
}
if(!empty($statusMessage)) {
$result['error'] = true;
$result['needlookup'] = 0;
$result['statusmessage'] = $statusMessage;
}
}
}
return $result;
}
}
?>
/*END*/
Whenever i send a message to one contact new record is creating in
SMSNotifier module but SMS is not happening to desired mobile. *Anyone
please help me to get it workout.*
Thanks and Regards
--
View this message in context: http://vtiger-crm.2324883.n4.nabble.com/Does-anything-wrong-in-below-code-tp18840.html
Sent from the vtigercrm-developers mailing list archive at Nabble.com.
More information about the vtigercrm-developers
mailing list