Nah pada tutorial kali ini saya akan menyampaikan tentang cara membuat script autorespond secara sederhana. Sebagai simulasi penulis akan membuat pendaftaran no HP secara otomatis dengan mengetikkan format sms yang sudah ditentukan. Format smsnya adalah daftar[spasi]nama
1. Terlebih dahulu buatlah tabel dengan nama phonebook di database yang telah disiapkan sebelumnya untuk sms gateway [baca cara instalasi gammu].
CREATE TABLE `phonebook` (
`Id` int(11) NOT NULL auto_increment,
`no_hp` bigint(20) default NULL,
`nama` varchar(255) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
2. Copy kan script berikut dan beri nama autorespond.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
function autorespond() | |
{ | |
var | |
$http, | |
$self = arguments.callee; | |
if (window.XMLHttpRequest) { | |
$http = new XMLHttpRequest(); | |
} else if (window.ActiveXObject) { | |
try { | |
$http = new ActiveXObject('Msxml2.XMLHTTP'); | |
} catch(e) { | |
$http = new ActiveXObject('Microsoft.XMLHTTP'); | |
} | |
} | |
if ($http) { | |
$http.onreadystatechange = function() | |
{ | |
if (/4|^complete$/.test($http.readyState)) { | |
document.getElementById('autorespond').innerHTML = $http.responseText; | |
setTimeout(function(){$self();}, 10000); | |
} | |
}; | |
$http.open('GET', 'cek_autorespond.php' + '?' + new Date().getTime(), true); | |
$http.send(null); | |
} | |
} | |
setTimeout(function() {autorespond();}, 10000); | |
</script> | |
<div id="autorespond"> | |
<? | |
// koneksi ke database | |
$hostname_config = "localhost"; | |
$database_config = "ok"; | |
$username_config = "root"; | |
$password_config = "root"; | |
$config = mysql_pconnect($hostname_config, $username_config, $password_config) or trigger_error(mysql_error(),E_USER_ERROR); | |
mysql_select_db($database_config, $config); | |
// cek tabel inbox dengan isi sms awal daftar | |
$inbox1=mysql_query("SELECT * FROM inbox where Processed='false'"); | |
while($t_inbox1=mysql_fetch_array($inbox1)) | |
{ | |
// pecah isi sms | |
$pieces1 = explode(" ", $t_inbox1[TextDecoded]); | |
// konversi menjadi huruf kecil semua | |
$str = strtolower($pieces1[0]); | |
// cek apakah pecahan 0 berisi daftar | |
if($str=='daftar') | |
{ | |
//ambil no pengirim di tabel inbox dan buang 3 angka didepan no hp | |
$hp=substr($t_inbox1[SenderNumber], 3); | |
//cek di phonebook apakah sudah terdaftar | |
$pel=mysql_query("SELECT * FROM phonebook where no_hp=$hp"); | |
$tampilkan=mysql_fetch_array($pel); | |
$jum_nilai=mysql_num_rows($pel); | |
$nama=$tampilkan[nama]; | |
//jalankan ini jika sudah terdaftar | |
if($jum_nilai>0) | |
{ | |
//kirimkan ke nomer yang ada di inbox | |
$query1 = mysql_query("INSERT INTO outbox (DestinationNumber, TextDecoded, ID, MultiPart, SenderID, CreatorID) VALUES | |
('$t_inbox1[SenderNumber]', 'Maaf no HP $t_inbox1[SenderNumber] sudah terdaftar atas nama $nama', '$newID', 'true', '$t_inbox1[RecipientID]', 'Gammu')"); | |
$sql1 = mysql_query("UPDATE inbox SET Processed='true' where Id='$t_inbox1[ID]'"); | |
} | |
//jalankan ini jika belum terdaftar | |
if($jum_nilai==0) | |
{ | |
$query1 = mysql_query("INSERT INTO outbox (DestinationNumber, TextDecoded, ID, MultiPart, SenderID, CreatorID) VALUES | |
('$t_inbox1[SenderNumber]', 'Terimakasih, no HP anda telah terdaftar di database kami', '$newID', 'true', '$t_inbox1[RecipientID]', 'Gammu')"); | |
$sql1 = mysql_query("UPDATE inbox SET Processed='true' where Id='$t_inbox1[ID]'"); | |
// masukkan ke tabel phonebook | |
$query2=mysql_query("INSERT INTO phonebook (nama, no_hp) VALUES ('$pieces1[1] $pieces1[2]', '$hp')"); | |
} | |
} | |
// update tabel inbox | |
$up=mysql_query("update inbox set Processed='true' where ID='$t_inbox1[ID]'") | |
} | |
//tampilkan no HP | |
$phone=mysql_query("SELECT * FROM phonebook"); | |
while($tphone=mysql_fetch_array($phone)) | |
{ | |
echo"0$tphone[no_hp] $tphone[nama]<p>"; | |
} | |
?> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// koneksi ke database | |
$hostname_config = "localhost"; | |
$database_config = "ok"; | |
$username_config = "root"; | |
$password_config = "root"; | |
$config = mysql_pconnect($hostname_config, $username_config, $password_config) or trigger_error(mysql_error(),E_USER_ERROR); | |
mysql_select_db($database_config, $config); | |
// cek tabel inbox dengan isi sms awal daftar | |
$inbox1=mysql_query("SELECT * FROM inbox where Processed='false'"); | |
while($t_inbox1=mysql_fetch_array($inbox1)) | |
{ | |
// pecah isi sms | |
$pieces1 = explode(" ", $t_inbox1[TextDecoded]); | |
// konversi menjadi huruf kecil semua | |
$str = strtolower($pieces1[0]); | |
// cek apakah pecahan 0 berisi daftar | |
if($str=='daftar') | |
{ | |
//ambil no pengirim di tabel inbox dan buang 3 angka didepan no hp | |
$hp=substr($t_inbox1[SenderNumber], 3); | |
//cek di phonebook apakah sudah terdaftar | |
$pel=mysql_query("SELECT * FROM phonebook where no_hp=$hp"); | |
$tampilkan=mysql_fetch_array($pel); | |
$jum_nilai=mysql_num_rows($pel); | |
$nama=$tampilkan[nama]; | |
//jalankan ini jika sudah terdaftar | |
if($jum_nilai>0) | |
{ | |
//kirimkan ke nomer yang ada di inbox | |
$query1 = mysql_query("INSERT INTO outbox (DestinationNumber, TextDecoded, ID, MultiPart, SenderID, CreatorID) VALUES | |
('$t_inbox1[SenderNumber]', 'Maaf no HP $t_inbox1[SenderNumber] sudah terdaftar atas nama $nama', '$newID', 'true', '$t_inbox1[RecipientID]', 'Gammu')"); | |
$sql1 = mysql_query("UPDATE inbox SET Processed='true' where Id='$t_inbox1[ID]'"); | |
} | |
//jalankan ini jika belum terdaftar | |
if($jum_nilai==0) | |
{ | |
$query1 = mysql_query("INSERT INTO outbox (DestinationNumber, TextDecoded, ID, MultiPart, SenderID, CreatorID) VALUES | |
('$t_inbox1[SenderNumber]', 'Terimakasih, no HP anda telah terdaftar di database kami', '$newID', 'true', '$t_inbox1[RecipientID]', 'Gammu')"); | |
$sql1 = mysql_query("UPDATE inbox SET Processed='true' where Id='$t_inbox1[ID]'"); | |
// masukkan ke tabel phonebook | |
$query2=mysql_query("INSERT INTO phonebook (nama, no_hp) VALUES ('$pieces1[1] $pieces1[2]', '$hp')"); | |
} | |
} | |
// update tabel inbox | |
$up=mysql_query("update inbox set Processed='true' where ID='$t_inbox1[ID]'") | |
} | |
//tampilkan no HP | |
$phone=mysql_query("SELECT * FROM phonebook"); | |
while($tphone=mysql_fetch_array($phone)) | |
{ | |
echo"0$tphone[no_hp] $tphone[nama]<p>"; | |
} | |
?> |
Script diatas bekerja dengan cara mengecek isi sms di inbox, sistem pengecekan menggunakan teknologi ajax. Dimana program akan otomatis mengecek isi tabel inbox tanpa kita melakukan refresh manual. Jika isi sms pada tabel outbox di awali dengan kalimat daftar maka program akan mengecek terlebih dahulu di tabel phonebook. Jika no HP tersebut sudah terdaftar di tabel phonebook maka akan dikirimkan sms jika no HP tsb sudah terdaftar. Namun jika belum maka akan otomatis di insert nama dan no hp ke tabel phonebook