โปรแกรมมิ่ง

mtAdaptor ของ TRUEMOVE

<?php
/*
MO/MT Adaptor สำหรับ TRUEMOVE

จัดทำโดย นายสราวุธ ศรีษะเกตุ
iamatt.com@gmail.com
Tel 0860070880

Version 1.0
Last revision 18 มกราคม 2555
*/

$path = “/home/enjoy/public_html/newsystem”;
$path_script = “/home/enjoy/public_html”;
$debug = true;

require_once $path . “/init.php”;
global $config;

require_once $config['site_path'].”/program/classes/class.SMS.php”;
require_once $config['site_path'].”/program/classes/class.TRUEMOVE.php”;
require_once $config['site_frontpath'].”/adaptor/includes/minixml.class.lib.inc.php”;
require_once $config['site_frontpath'].”/adaptor/includes/domit/xml_domit_include.php”;

// เรียก DB
global $DB;

// ประกาศ Class ของ SMS (Common)
$SMS = new SMS();

// ประกาศ Class ของ TRUEMOVE
$TRUEMOVE = new TRUEMOVE();

// รับ MO Data เข้ามา
$Incoming_Data = null;
$inputdata = fopen(“php://input”,”rb”);
while(!feof( $inputdata ))
$Incoming_Data .= fread($inputdata, 4096 );
fclose($inputdata); 

if ($debug){$SMS->Log(“TRUE_Adaptor”,”MO IN\n”.$Incoming_Data);}
// ถ้า MO ที่เข้ามาไม่ว่างเปล่า ก็จะทำงาน
if(!empty($Incoming_Data)){
 $method = detectXML($Incoming_Data);
 switch ($method['type'])
 {
  case “SMS”:
   $dataXML = processSMS($Incoming_Data);
   $service_number = $SMS->ServiceNumberByServiceID($dataXML['service_id']);
   if ($method['data'] == “mo”)
   {
    if (empty($service_number))
    {
     $re = 1;
     $dataXML['rsr_status'] = “failure”;
     $dataXML['code'] = “1″;
     $dataXML['description'] = “Invalid content ID or keyword.”;
     $TRUEMOVE->logDeliverMessage($dataXML);
     echo $TRUEMOVE->Acknowledge($dataXML);  
     flush();
     exit;
    }
    else
    {
     // ถ้า service number match กับ ฐานข้อมูล ให้ดำเนินการ process MO Request
     $TRUEMOVE->logDeliverMessage($dataXML);
     switch ($service_number['service_status'])
     {
      // เบอร์ใช้งานจริงแล้ว
      case “production”:
       $data['TEST'] = false;
       $data['CHARGE'] = true;
      break;
      // เบอร์อยู่ระหว่างเปิดทดสอบ
      case “implementation”:
       $data['TEST'] = true;
       $data['CHARGE'] = false;
      break;
      // เบอร์ระงับบริการชั่วคราว
      case “disabled”:
       $re = 1;
       $dataXML['rsr_status'] = “failure”;
       $dataXML['code'] = “1″;
       $dataXML['description'] = “Invalid content ID or keyword.”;
       $TRUEMOVE->logDeliverMessage($dataXML);
       echo $TRUEMOVE->Acknowledge($dataXML);  
       flush();
       exit;
      break;
     }  
     $dataXML['rsr_status'] = “success”;
     $dataXML['code'] = “0″;
     $dataXML['description'] = “Success receive request”;
     echo $ack = $TRUEMOVE->Acknowledge($dataXML);  
     flush();

     // pack data เพื่อส่งเข้าไปยัง service 2way,download,subscribe
     $data['service_number'] = $service_number;
     $data['MSISDN'] = $dataXML['source_number'];
     $data['MESSAGE'] = $dataXML['ud'];
     $data['OPERATOR'] = “TRUEMOVE”;
     $data['TYPE'] = “GSM”;
     $data['CHANNEL'] = “SMS”;
     $data['SERVICE_NUMBER'] = $dataXML['destination_number'];
 
     if ($data['CHANNEL'] == “IVR”)
     {
      $data['MESSAGE'] = substr($data['MESSAGE'],strlen($dataXML['destination_number']),strlen($data['MESSAGE']) – strlen($dataXML['destination_number']));
      $TRUEMOVE->logIVRRequest($data);
     }
     if ($data['CHANNEL'] == “SMS”)
     {
      $TRUEMOVE->logSMSRequest($data);
     }
     if ($debug){$SMS->Log(“TRUE_Adaptor”,”Process MO and send MT Back \n\n”);}
     if ($debug){$SMS->Log(“TRUE_Adaptor”,”Service Number = “.serialize($service_number).”\n\n”);}
     if ($debug){$SMS->Log(“TRUE_Adaptor”,”DATA PROCESS = “.serialize($data).”\n\n”);}
     switch ($service_number['service_type'])
     {
      case “2way” :
       require_once $config['site_path'].”/program/classes/class.SMS2Way.php”;
       $SMS2Way = new SMS2Way();
       $return = $SMS2Way->process($data);
      break;
      case “download” :
       require_once $config['site_path'].”/program/classes/class.SMSDownload.php”;
       $SMSDownload = new SMSDownload();
       $return = $SMSDownload->process($data);
      break;
      case “subscription” :
       require_once $config['site_path'].”/program/classes/class.SMSSubscription.php”;
       $SMSSubscription = new SMSSubscription();
       $return = $SMSSubscription->process($data);
      break;
     }
     if ($debug){$SMS->Log(“TRUE_Adaptor”,”Return = “.serialize($return).”\n\n”);}
     sleep(2);

     $TRUEMOVE->service_id = $dataXML['service_id'];
     $TRUEMOVE->ud = $return['MESSAGE'];
     $TRUEMOVE->destination_number = $dataXML['source_number'];
     $TRUEMOVE->orange_serviceid = $dataXML['service_id'];
     $TRUEMOVE->source_number = $dataXML['destination_number'];
     $TRUEMOVE->stcs = $dataXML['stcs'];
     $TRUEMOVE->username = $service_number['username'];
     $TRUEMOVE->password = $service_number['password'];
     $TRUEMOVE->sender = $dataXML['destination_number'];

     switch ($return['TYPE'])
     {
      case “TEXT” :
       $TRUEMOVE->ud_type= “text”;
       if ($return['LANGUAGE'] == “T”)
       {
        $TRUEMOVE->encode = “TIS-620″;
        $TRUEMOVE->char_encoding = “TIS-620″;
        $TRUEMOVE->ud = $SMS->utf82tis($TRUEMOVE->ud);
       }
       else
       {
        $TRUEMOVE->encode = “default”;
        $TRUEMOVE->char_encoding = “UTF-8″;
       }
       $TRUEMOVE->textSMS();
       $TRUEMOVE->SendSMS();
      break;
      case “URL” :
       $TRUEMOVE->ud_type= “text”;
      break;
      case “MMS” :
       $TRUEMOVE->ud_type= “text”;
       $TRUEMOVE->MMS($return,$this->recipient,$deliverMessage->getSender()->getAddress(),$deliverMessage->getLinkedID());
      break;
     }
    }
   }
  break;
  case “RSR”:
   $dataXML = processRSL($Incoming_Data);
   print_r($dataXML);
   exit;
  break;
 }
}

function detectXML($XML)
{
 $MOXMLDom =& new DOMIT_Document();
 $success = $MOXMLDom->parseXML($XML, true);

 // sms_type คือชนิดของข้อความที่ส่งเข้ามา
 $sms_tag =& $MOXMLDom->getElementsByTagName(“sms”);
 $sms_node = $sms_tag->item(0);
 if ($sms_node != “”)
 {
  $sms_type = trim($sms_node->getAttribute(“type”));
 }
 $rsr_tag =& $MOXMLDom->getElementsByTagName(“rsr”);
 $rsr_node = $rsr_tag->item(0);
 if ($rsr_node != “”)
 {
  $rsr_type = trim($rsr_node->getAttribute(“type”));
 }

 if ($sms_type != “”)
 {
  $return['type'] = “SMS”;
  $return['data'] = $sms_type;
  return $return;
 }
 else if ($rsr_type != “”)
 {
  $return['type'] = “RSR”;
  $return['data'] = $rsr_type;
  return $return;
 }
}

function processSMS($data)
{
 $MOXML = new MiniXMLDoc();
 $MOXML->fromString($data);

 // destination_number ** คือหมายเลข Service number
 $response = $MOXML->getElementByPath(“destination/address/number”);
 $Return_Data['destination_number'] =  $response->getValue();

 // source_number ** คือหมายเลขโทรศัพท์ที่ส่ง SMS เข้ามา
 $response = $MOXML->getElementByPath(“source/address/number”);
 $Return_Data['source_number'] =  $response->getValue();

 // service_id ** คือหมายเลข service id
 $response = $MOXML->getElement(“service-id”);
 $Return_Data['service_id'] =  $response->getValue();

 // ud ** คือข้อความที่พิมพ์มาจากมือถือ
 $response = $MOXML->getElement(“ud”);
 $Return_Data['ud'] =  $response->getValue();

 // scts ** คือเวลาที่ข้อความนี้ถูกส่งเข้ามา
 $response = $MOXML->getElement(“scts”);
 $Return_Data['scts'] =  $response->getValue();

 // from ** คือถูกส่งมาจาก SMPP แบบใหน
 $response = $MOXML->getElement(“from”);
 $Return_Data['data_from'] =  $response->getValue();

 // to ** คือส่งเข้า Adaptor แบบใหน
 $response = $MOXML->getElement(“to”);
 $Return_Data['data_to'] =  $response->getValue();
 
 $MOXMLDom =& new DOMIT_Document();
 $success = $MOXMLDom->parseXML($data, true);

 // message_id ** คือเลข message จาก SMPP 
 $message_tag =& $MOXMLDom->getElementsByTagName(“message”);
 $message_node = $message_tag->item(0);
 $Return_Data['message_id'] = $message_node->getAttribute(“id”);
 
 // sms_type คือชนิดของข้อความที่ส่งเข้ามา
 $sms_tag =& $MOXMLDom->getElementsByTagName(“sms”);
 $sms_node = $sms_tag->item(0);
 $Return_Data['sms_type'] = $sms_node->getAttribute(“type”);
 
 // destination_messageid คือเลข message ของ Service number
 $des_tag =& $MOXMLDom->getElementsByTagName(“destination”);
 $des_node = $des_tag->item(0);
 $Return_Data['destination_messageid'] = $des_node->getAttribute(“messageid”);
 
 // destination number type คือ ชนิดของ number ในส่วนของ Service number
 $number_tag =& $MOXMLDom->getElementsByTagName(“number”);
 $number_node = $number_tag->item(0);
 $Return_Data['number_destination_type'] = $number_node->getAttribute(“type”);

 // source number type คือ ชนิดของ number ในส่วนของผู้ใช้งานที่ส่งข้อความเข้ามา
 $number_node = $number_tag->item(1);
 $Return_Data['number_source_type'] = $number_node->getAttribute(“type”);
 
 // source number type คือ ชนิดของ number ในส่วนของผู้ใช้งานที่ส่งข้อความเข้ามา
 $ud_tag =& $MOXMLDom->getElementsByTagName(“ud”);
 $ud_node = $ud_tag->item(0);
 $Return_Data['ud_type'] = $ud_node->getAttribute(“type”);
 $Return_Data['ud_encoding'] = $ud_node->getAttribute(“encode”);
 return $Return_Data;
}

function processRSL($data)
{

 $MOXML = new MiniXMLDoc();
 $MOXML->fromString($data);

 // destination_number ** คือหมายเลข Service number
 $response = $MOXML->getElementByPath(“destination/address/number”);
 $Return_Data['destination_number'] =  $response->getValue();

 // source_number ** คือหมายเลขโทรศัพท์ที่ส่ง SMS เข้ามา
 $response = $MOXML->getElementByPath(“source/address/number”);
 $Return_Data['source_number'] =  $response->getValue();

 // service_id ** คือหมายเลข service id
 $response = $MOXML->getElement(“service-id”);
 $Return_Data['service_id'] =  $response->getValue();

 // code ** คือ status code ที่ตอบกลับมาจาก SMSG
 $response = $MOXML->getElementByPath(“rsr_detail/code”);
 $Return_Data['code'] =  $response->getValue();

 // description ** คือรายละเอียดของ code
 $response = $MOXML->getElementByPath(“rsr_detail/description”);
 $Return_Data['description'] =  $response->getValue();
 
 $MOXMLDom =& new DOMIT_Document();
 $success = $MOXMLDom->parseXML($data, true);

 // message_id ** คือเลข message จาก SMPP 
 $message_tag =& $MOXMLDom->getElementsByTagName(“message”);
 $message_node = $message_tag->item(0);
 $Return_Data['message_id'] = $message_node->getAttribute(“id”);
 
 // rsr_type คือ status ที่ตอบกลับมาจาก SMSG
 $rsr_tag =& $MOXMLDom->getElementsByTagName(“rsr”);
 $rsr_node = $rsr_tag->item(0);
 $Return_Data['rsr_type'] = $rsr_node->getAttribute(“type”);

 // rsr_detail คือ status ที่ตอบกลับมาจาก SMSG
 $rsr_tag =& $MOXMLDom->getElementsByTagName(“rsr_detail”);
 $rsr_node = $rsr_tag->item(0);
 $Return_Data['rsr_detail'] = $rsr_node->getAttribute(“status”);
 
 // destination_messageid คือเลข message ของ Service number
 $des_tag =& $MOXMLDom->getElementsByTagName(“destination”);
 $des_node = $des_tag->item(0);
 $Return_Data['destination_messageid'] = $des_node->getAttribute(“messageid”);
 
 // destination number type คือ ชนิดของ number ในส่วนของ Service number
 $number_tag =& $MOXMLDom->getElementsByTagName(“number”);
 $number_node = $number_tag->item(0);
 $Return_Data['number_destination_type'] = $number_node->getAttribute(“type”);

 // source number type คือ ชนิดของ number ในส่วนของผู้ใช้งานที่ส่งข้อความเข้ามา
 $number_node = $number_tag->item(1);
 $Return_Data['number_source_type'] = $number_node->getAttribute(“type”);
 
 return $Return_Data;
}
?>


CPA TrueMove วิธีส่ง SMS เข้า TrueMove ครับ

ในขั้นตอนการตอบกลับสำหรับ CPA Truemove นั้น การทำ Adaptor คุณจะต้องคำนึงถึงการกำหนดค่าต่างๆขึ้นมาเอง

function  Acknowledge($MO_Data);
{
$xml = null;
$xml .= “<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;
$xml .= “<message id=\”".$MO_Data['message_id'].”\”>”;
$xml .= “<rsr type=\”reply\”>”;
$xml .= “<service-id>”.$MO_Data['service_id'].”</service-id>”;
$xml .= “<destination messageid=\”".$MO_Data['destination_messageid'].”\”>”;
$xml .= “<address>”;
$xml .= “<number type=\”".$MO_Data['number_destination_type'].”\”>”.$MO_Data['destination_number'].”</number>”;
$xml .= “</address>”;
$xml .= “</destination>”;
$xml .= “<source>”;
$xml .= “<address>”;
$xml .= “<number type=\”".$MO_Data['number_source_type'].”\”>”.$MO_Data['source_number'].”</number>”;
$xml .= “</address>”;
$xml .= “</source>”;
$xml .= “<rsr_detail status=\”".$MO_Data['rsr_status'].”\”>”;
$xml .= “<code>”.$MO_Data['code'].”</code>”;
$xml .= “<description>”.$MO_Data['description'].”</description>”;
$xml .= “</rsr_detail>”;
$xml .= “</rsr>”;
$xml .= “</message>”;
return  $xml;
}

parameter สำหรับ AIS CDG ที่ควรรู้ครับ

ผมมารวบรวม parameter สำหรับ AIS CDG ให้นะครับ สำหรับใครที่ใช้ firsthop_MM7 นะครับ

$deliverMessage->getObject();
Username = $o['MIBIdentification'];
Password = $o['MIBPassword'];
Transaction ID = $deliverMessage->getTransactionID();
LinkedID = $deliverMessage->getLinkedID();
Sender = $deliverMessage->getSender()->getAddress();
Recipient = $deliverMessage->getRecipients()->getTo();
Sender Name = ’4′.substr($deliverMessage->getRecipients()->getTo()[0]->getAddress(), 1, 6);
MSISDN = $deliverMessage->getMSISDN();
Extra Data= $deliverMessage->getMsgExtraData($deliverMessage);


AIS CDG send SMS text with PHP

วันนี้จะมาเขียนถึงวิธีการเขียนต่อกับ AIS CDG ผ่านภาษา PHP นะครับ
สาเหตุที่เขียนเพราะผมต้องการทำ paper online ไว้ด้วย เวลาที่ผมลืม จะได้ย้อนกลับมาดูได้ด้วย

ขั้นตอนที่เราต้องใช้ คือเราต้องมี firsthop_MM7 ก่อน สามารถหาดาวน์โหลดได้จากในหน้า VMAP ของ AIS CP นะครับ
เมื่อเรา extract file .zip ออกมาแล้ว คุณจะพบกับ 5 Folders ดังนี้

อธิบายตาม Folder นะครับ
1. firsthop_MM7 เป็น folder ที่เก็บ class library สำหรับใช้ต่อเข้า AIS CDG ทั้งหมดครับ
2. fs เป็น folder  ที่เอาไว้ save DeliveryReport ครับ
3. inbox อันนี้ผมคิดว่าไม่ได้ใช้นะครับ
4. libext เป็น 3rd Party library ครับ
5. tpls เป็น template สำหรับ SOAP request ครับ

เอาล่ะ มาดูกัน ในขั้นตอนการส่ง MT ไปยัง AIS CDG นั้น คุณต้องกำหนด Parameter ดังนี้

  // create sender Identification
  $si = new firsthop_MM7_SenderIdentification();
  $si->setVASPId(‘xxxxxx’);
  $si->setVASId(‘xxxxx’);
  $si->setPassword(‘yyyyyy’);
  $si->setSenderAddress(‘zzzzz’);

 โดย x คือ เลข 3 ตัว ที่คุณใช้ login เข้า vsmap ครับ เช่น service number คุณคือ 4896666 ก็ต้องตัดตัวหน้าออก คือ 4 จะเหลือ 896666 ให้เราใช้แค่ 3 ตัวหน้าครับ คือ 896 ใส่แทน xxxxx ครับ
และ y ก็คือรหัสผ่านที่ได้มาที่ใช้สำหรับ authorize เข้า AIS CDG ครับ
z ก็คือ service number คุณครับ เช่น 4896666

จากนั้นต้องกำหนดตัว Recipicents คือ ผู้รับข้อความครับ สำหรับ AIS CDG ถ้าเป็น subscription คุณใส่เลขอะไรไปก็ได้ครับ 1 เบอร์ ที่เหลือทั้งหมดใน set group มันจะส่งให้เองครับ แต่ถ้าเป็นการส่ง MT แบบ MO/MT คุณต้องระบุเป็นเบอร์ๆไปครับ แตก array ได้นะครับ

  // create Recipient List
  $recipients = new firsthop_MM7_Recipients();
  $recipients->setTo(array(new firsthop_MM7_Address(‘Number’,$sender)));

จากนั้น ให้คุณ initialize เพื่อจะส่ง SMS ครับ

  // create SMS message object————————————————————————————————
  $msg = new firsthop_MM7_SMS();
  $msg->setSenderIdentification($si);
  $msg->setRecipientAddress($recipients);

จากนั้นกำหนด Parameter ที่จำเป็นสำหรับการส่ง SMS ครับ

  $msg->setSMSText(‘TEST SMS’);
  $msg->setExtraData(‘UserDataEncoding’,'UTF-8′);
หากคุณส่งเป็นภาษาไทย ต้อง set DataEncoding เป็น UNICODE นะครับอย่าลืม แต่ถ้าส่งภาษาอังกฤษให้ใช้ UTF-8 ครับ 
 $msg->setSubject(‘TEST’);
  $msg->setReadReply(‘false’);
กำหนดให้ผู้อ่านต้องกดรับว่าอ่านแล้ว 
  $msg->setDeliveryReport(‘true’);
  $msg->setTransactionID($transactionID);
  $msg->setLinkedID($LinkedID);
  $msg->setExtraData(‘AISVersion’, ’1.0′);
  $msg->setExtraData(‘CCT’, ’10′);
  $msg->setExtraData(‘sender_name’,$sender_name);

ยังมี parameter ที่แถมมานะครับ เช่น ส่ง sms free , กำหนดเลข call center
  //$msg->setExtraData(‘RouterID’,$RouterID);
  //$msg->setExtraData(‘ContentURL’, ‘http://abc.com/image.jpg’);
  //$msg->setExtraData(‘WAPPushText’, ‘ทดสอบบริการ’);
  //$msg->setExtraData(‘call_center_no’, ’029999999′);
  //$msg->setExtraData(‘free_text’,'test free SMS’);

จากนั้นให้ submit เข้าระบบครับ

  $url=’http://10.104.129.229:9500/mm7adapter’;
  // create SOAPClient
  $client =& new firsthop_MM7_SOAPClient($url);
  $mylogger->debug(‘send req to ‘.$url);

  // submit Request
  $res = $client->submitReq($msg); 
  $mylogger->debug(‘message request submited’);
  if(isset($res)) {
    if($client->isFault()) {
   $f = $client->getFault();
   $mylogger->debug(‘Fault Detail: ‘ .$f->getFaultDetail());
    }
    else {
   $mylogger->debug(‘DeliverRespond Status Details: ‘ .$res->getStatusDetails());
   /*$mylogger->debug(‘Message ID: ‘.$res->getMessageID());
   $mylogger->debug( ‘Status Code: ‘.$res->getStatusCode());
   $mylogger->debug( ‘Status Text: ‘.$res->getStatusText());
   $mylogger->debug( ‘Status Details: ‘.$res->getStatusDetails());*/
   return $res->getStatusDetails();
   }
  }

โดย Error กับ Success log จะอยู่ที่ SMSLogs.txt ครับ ลองดูนะครับ ไม่ยากเลย


เวบส่ง SMS (เกือบ)ฟรี

กำลังจะนำเอา SMS ที่มีอยู่ฟรี 850,000 ข้อความมาทำให้ส่ง SMS ฟรีครับ ที่  YOD.NET โดยคุณต้อง Activate เบอร์โทรก่อนส่ง ที่ราคา 9 บาทต่อ 10 ข้อความแรก หลังจากนั้นจะฟรีทุกๆ 1 วัน วันละ 1 ข้อความ และหากต้องการเติมเครดิต คิดที่ 9 บาท / 10 ข้อความครับ


  • เรื่องล่าสุด

  • ขายสินค้านิดหน่อย

  • เมนูการใช้งาน

  • Copyright © 1996-2010 ไอ แอม แอ๊ต. All rights reserved.
    Jarrah theme by Templates Next | Powered by WordPress