📜 ⬆️ ⬇️

How to make your server for receiving, processing and sending SMS


Good day, dear habrosoobschestvo!
Recently, at the university, I was tasked with creating a service for sending SMS notifications to elders (dean's office and others) to my groups.
The main requirements for the service were as follows:
- The service must be multi-user (warden more than 1) with the ability to simultaneously process requests
- Should not be tied to online distribution services (so as not to leak the user base)
- Should be light enough and mobile
- Maximum low cost
- Must use only open source software.
- Must ensure that notifications are sent, even if the sending party does not have access to the Internet and / or computer. (i.e., not only send, but also receive incoming SMS messages from elders + determine from whom exactly they did and send out to their groups)

How did you manage to implement it - look under the cut.

So, let's begin.
')

List of ingredients.



In order to proceed with the organization of the service, we will need to collect all the necessary glands.



In principle, this can be limited. But for more comfortable work, I would advise more:



Cooling.


As for the cooling - to make it completely elementary. Take the old 80mm cooler, strip the red and black wiring coming from it. Then cut half of the unnecessary USB cable. Also strip the red and black wires on it. Wind the wires from the cooler and from the USB together, insulate and your cooling is ready. It is advisable to place the cooler under the netbook on the plasticine legs, so that it does not resonate loudly.

Actually, this is what it looks like:



If someone does not understand, here is a detailed manual .

As for stripping wires - I like to first singe their ends on a kerosene burner, and then with a knife it is easy to clean off the remnants. You should not buy a kerosene lamp either - it can be made from the usual 60 Watt Ilyich light bulb. Punch the top of the cap, pour kerosene in there, insert a piece of old rag instead of a wick, and you're done. Here's what it looks like:



Prepare the iron to work.



OS

As an operating system for a netbook, I chose EasyPeasy, a system for netbooks based on the Linux kernel and modified for weak computers. Download it, install on a USB flash drive, and then on a netbook, you can here.

Bluetooth

After the operating system has taken its place, we connect the bluetooth adapter to a free USB port. The system should recognize it automatically. Turn on Nokia and pair the phone and computer.

Install gnokii

Need to install gnokii. I have already referred to this article on installing it on Linux. I think you figure it out.
After installation, type in the console something like echo "Hello, friend!" | gnokii --sendsms '+ 7number your friend' and check whether the SMS has gone.

Hosting Preparation

It may be that some other way of organizing multi-user work will suit you, but I used the capabilities of Joomla ACL (access levels). How to set it up is described in great detail in this article . I just want to note that at the exit you should have a website where each of the users (those who will send sms) will have their own personal account, into which they can enter their mobile phone number (as login, in the format 79XX XXX XX XX) and a five-digit numeric code as a password. For example, as on the site vamsms.ru

We prepare the software for work.


Attention! I wrote all the programs myself. You can use them as you like, just give feedback if you find any serious jamb.

So, our task is to teach our server to communicate with a remote site, collect requests for sending and send messages. In addition, the server should be able to receive incoming messages (to Nokia, with which SMS messages are sent), to be aware that they were sent by the elders and also to queue for the newsletter for the group to which the particular elder belongs. Plus, all this needs to be synchronized. Well, keep logs, of course.

MySQL database organization

First you need to make a database related to the hosting on which you have organized the site. I called my database - pXXXX_sender (under XXX my user's hosting numbers are hidden)
Inside this database you need to create 3 tables. I have the following structure:

starosti (identification and range of activities for elders)
- number
- group
- course
- facultet
- pass

telephones (all phones in the database)
- number
- groups
- course
- facultet

on_demand (messages for sending are stored here)
- message
- group
- course
- id_this_trans
- date_log

Great, on this with all the databases.

The organization of a shell script on a netbook (server).

Now you need to make sure that the netbook checks every 10 minutes:
1) Have new messages been received from the site? If yes, then send them to the right groups.
2) Received new incoming sms? If so, are they elders? If not, then simply delete them, if so, then I determine which group the elder is responsible for and send the message to that group.

Here is the listing of the shell script:

#!/bin/bash a=1 #    test_trigger=`cat cache_test_trig.txt` #      while [ $a == 1 ] #   do if ping -c1 ya.ru &> /dev/null then test_trigger_now=$(wget http://site.ru/trigger.txt --quiet -O -) #    if [ $test_trigger_now -eq $test_trigger ]; then #     ? echo "Nothing new :(" # ,       ############################### ######### Mobile extention ############################### aa=1 #    #count=1 #       1 while [ $aa == 1 ] #      1 do sms_test=$(gnokii --getsms IN 1 1) # 1    (1 1,2 2  ..) echo $sms_test>mobile_temp.txt #     answer_test=$(grep -c Date/time: mobile_temp.txt) #    ,  (Date/time:) if [ "$answer_test" != "1" ]; then #    ,  ,   aa=2 #   echo "messages over! Stop" elif [ "$answer_test" == "1" ]; then #   , echo "Here is 1 message" #let count=$count+1 #      #   sms_test    '1. Inbox Message (Unread) Date/time: 07/02/2013 15:33:16 +0400 Sender: +7904xxxxxx Msg Center: Text: 51235  .' server_send=$(wget http://site.ru/serv_mobile.php?text="$sms_test" -O /dev/null) #     /   sleep 1 gnokii --deletesms IN 1 1 #   1  fi sleep 3 done ############################### ######### Mobile extention ############################### sleep 500 else #  ,   - echo "i need to do something!" let different=$test_trigger_now-$test_trigger #(-),   #echo $different #   get_sender=$(wget http://site.ru/trigger.php?diff=$different --quiet -O -) #         (  ) #echo $get_sender #      different  echo $get_sender #    sleep 3 #    #Start SMS-sending n=0 while [ $n -lt $different ] #  n < different do let n=$n+1 #!!!!!!!!!!!SENDING!!!!!!!!!!!!!! echo "send files from send$n folder" #   gnokii mess_for_gnok=$(wget http://site.ru/send$n/message.txt --quiet -O -) #  numb_for_gnok=$(wget http://site.ru/send$n/numbers.txt) #  cat numbers.txt | while read line do inp=$line echo "$mess_for_gnok" | gnokii --sendsms $inp echo $line echo $mess_for_gnok sleep 2 done rm numbers.txt #delete temporary files sleep 1 #!!!!!!!!!!!SENDING!!!!!!!!!!!!!! done terminator=$(wget http://site.ru/terminate.php?kill=$different --quiet -O -) # delete all temperal folders in root #echo "i kill all files" test_trigger=$test_trigger_now # ,       trigger.txt echo $test_trigger>cache_test_trig.txt sleep 10 #    fi else echo "Here is no Internet. Find it!" sleep 500 fi done 


In addition, in the folder with the script you need to create a file cache_test_trig.txt with a value of 1 (and make the first entry in the on_demand table)

Organization of php scripts on remote hosting.

The following form should be in the joomla user account:

 <script type="text/javascript"> function isNotMax(e){ e = e || window.event;var target = e.target || e.srcElement;var code=e.keyCode?e.keyCode:(e.which?e.which:e.charCode)switch(code){case13:case8:case9:case46:case37:case38:case39:case40:returntrue;}return target.value.length <= target.getAttribute('maxlength');} </script> <p><strong> -.</strong></p> <form action="http://site.ru/handler.php" method="POST">  :<br /> <select name="Group"> <option value="11"> 11 </option> </select> <select name="Course"> <option value="4"> 4 </option> </select> <select name="Facult"> <option value="1">  </option> </select> <br /> <br /> <hr style="color:#B4B4B4; border:thin groove"/> <b>!</b>      <b>70</b>  (/,  )<hr style="color:#B4B4B4; border:thin groove"/><br />  :<br /> <textarea name="message" rows="4" cols="46" maxlength="70" onkeypress="return isNotMax(event)"></textarea> <br /> <input type="submit" value=" !" /></form> 


Obviously, the data from the form is transferred to a file located in the root directory of the hosting handler.php

Here is its listing:

 <?php $prev_page = $_SERVER['HTTP_REFERER']; $who_is_you = substr_count("$prev_page", "http://site.ru/index.php/"); //       if ($who_is_you == 1) { $message=$_POST['message']; $message=htmlspecialchars(stripslashes($message)); //    $course=$_POST['Course']; $course=htmlspecialchars(stripslashes($course)); //    $groups=$_POST['Group']; $groups=htmlspecialchars(stripslashes($groups)); //    $facult=$_POST['Facult']; $facult=htmlspecialchars(stripslashes($facult)); //    //validation----------------------------- if (empty($message)) { echo " <body style='background-image: url(body_bg.gif); background-repeat: repeat-x'> <div align='center' style='margin-top: 14%'> <p style='color: #666666; font-family: Verdana,Helvetica,sans-serif; font-size: 18px; line-height: 1.8em;'><i>   !</i> <br>   . <a href = '$prev_page'>    .</a> </p> </div> </body> "; } //validation----------------------------- else { //connect_to_BD-------------------------- include(db_connect.php); //connect_to_BD-------------------------- mysql_query("SET NAMES 'utf8'"); $r = mysql_query("SELECT id_this_trans FROM on_demand ORDER BY id_this_trans DESC LIMIT 0, 1"); //     while($row = mysql_fetch_array($r)) { $last_tranzaktion = $row['id_this_trans']; } $last_tranzaktion++; //  id   $heutige_datum = date("Ymd H:i:s"); mysql_query ("INSERT INTO on_demand VALUES('$message','$groups','$course','$last_tranzaktion', '$heutige_datum')"); //     file_put_contents("trigger.txt", $last_tranzaktion); echo " <body style='background-image: url(body_bg.gif); background-repeat: repeat-x'> <div align='center' style='margin-top: 14%'> <p style='color: #666666; font-family: Verdana,Helvetica,sans-serif; font-size: 18px; line-height: 1.8em;'><i>    !</i> <br>      ,  <a href = 'http://site.ru/'>    </a> </p> </div> </body> "; } } else { echo " <html> <head><title> 404 Not Found </title></head> <body><h1> 404 Not Found </h1> The resource requested could not be found on this server!<hr /> Powered By <a href='http://www.litespeedtech.com'>LiteSpeed Web Server</a><br /> <font face='Verdana, Arial, Helvetica' size=-1>LiteSpeed Technologies is not responsible for administration and contents of this web site!</font></body></html> "; } ?> 


Further, in order for the remote hosting to respond to requests from the shell script, it is necessary that the following files be located in its root directory:

file serv_mobile.php - is responsible for processing messages received by a mobile phone

 <?php #$sms_text=mysql_real_escape_string($_GET['text']); //      +    $sms_text=$_GET['text']; //      +    file_put_contents("temp_mobile_mess.txt", $sms_text); //      $get_from_file = file_get_contents("temp_mobile_mess.txt"); //     $number = substr("$get_from_file", 72, 11); //   (   read/unread) $pass = substr("$get_from_file", 103, 5); //  $message = substr("$get_from_file", 109); //   //connect_to_BD-------------------------- include(db_connect.php); //connect_to_BD-------------------------- mysql_query("SET NAMES 'utf8'"); //   $number = mysql_real_escape_string($number); $pass = mysql_real_escape_string($pass); $message = mysql_real_escape_string($message); //   $r = mysql_query("SELECT COUNT(*) FROM `starosti` WHERE pass='$pass' AND number='$number'"); //   /   while($row = mysql_fetch_array($r)) { if($row[0] == 1) //,     { //------------   id_this_trans--------------- $q = mysql_query("SELECT id_this_trans FROM on_demand ORDER BY id_this_trans DESC LIMIT 0, 1"); //     while($rowq = mysql_fetch_array($q)) { $last_tranzaktion = $rowq['id_this_trans']; } $last_tranzaktion++; //  id   //------------   id_this_trans--------------- //------------      on_demand---- $second_r = mysql_query("SELECT * FROM `starosti` WHERE pass='$pass' AND number='$number'"); //    ,   while($second_row = mysql_fetch_array($second_r)) { $groups = $second_row['group']; $course = $second_row['course']; $heutige_datum = date("Ymd H:i:s"); mysql_query ("INSERT INTO on_demand VALUES('$message','$groups','$course','$last_tranzaktion','$heutige_datum')"); //     } //------------      on_demand---- #        file_put_contents("trigger.txt", $last_tranzaktion); } } ?> 


The script trigger.php, also in the root directory, creates folders on the hosting, containing a file with a message and a file with numbers to which this message will be sent.

His listing:
 <?php $inp_diff = $_GET['diff']; //     //connect_to_BD-------------------------- include(db_connect.php); //connect_to_BD-------------------------- mysql_query("SET NAMES 'utf8'"); //Create files AND Folders--------------- for($i=1; $i<=$inp_diff;$i++) { mkdir("send$i"); file_put_contents("send$i/message.txt",''); file_put_contents("send$i/numbers.txt",''); } //Create files AND Folders--------------- $counter=1;//     ,     //    ----- $r = mysql_query("SELECT * FROM on_demand ORDER BY id_this_trans DESC LIMIT 0, $inp_diff"); //    while($row = mysql_fetch_array($r)) { file_put_contents("send$counter/message.txt",$row['message']); //   message.txt    //   numbers.txt    $help_course = $row['course']; $help_group = $row['group']; $second_r = mysql_query("SELECT * FROM telephones WHERE course='$help_course' AND groups='$help_group'"); while($second_row = mysql_fetch_array($second_r)) { $insert_number = $second_row['number']; //     1   1  $fp=fopen("send$counter/numbers.txt","a"); fputs($fp,$insert_number. "\n"); fclose($fp); } //   numbers.txt    $counter++; } //    ----- echo "ok"; ?> 


The last file in the root directory is the terminate.php file. Removes previously created folders from the server when messages have already been sent.

His listing:
 <?php $inp_diff = $_GET['kill']; //     //Delete files AND Folders--------------- for($i=1; $i<=$inp_diff;$i++) { unlink("send$i/message.txt"); unlink("send$i/numbers.txt"); rmdir("send$i"); } //Delete files AND Folders--------------- ?> 


Summarizing.


Please forgive me if my story turned out to be unnecessarily muddled. I really tried to put my thoughts on as consistently as possible here :) Successful to you setting, I will gladly answer questions in the comments.

By the way, this is what it looks like with me:

Source: https://habr.com/ru/post/170901/


All Articles