Skip to content

cdr60/smsapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SmsApi

simple web api to delegate sending sms on a linux server that owns a sim card

What you needs :

  • A linux web server using apache or nginx or lighttp.
  • Php from 7.x to 8.2
  • sqlite3 and php-sqlite module
  • gammu : the SMS sender binary file
  • An USB Sim card adaptater
  • An activated Sim card
  • Kownledges to call a web api with your prefered language

How it works :

  • This api is php written and uses a sqlite database.
  • It calls gammu binary executable and of course needs a SIM Card
  • At boot, your SIM card must be PIN unlocked (a service is used to send your PIN Code at boot time if needed)
  • Your application (web site or whatever) has to call this web api using curl (if your langage is php) or requests (if python) or other
  • Be carefull about execution time (sometimes it takes several seconds to send a SMS)
  • Datas are in a sqlite database
  • This api provides a test web page do test it manually.
  • You have to create clients ( = which account is sending a SMS)
  • This api provides quotat by clients (account) (sending SMS wil decrease it, and you have to increase it manually using update via sqlite studio, cli or by another method)
  • Accessing this api is secured by user/password that must exists in TBSMSCLI sqlite database (this is the account list)
  • Each account owns user/password in TBSMSCLI table
  • These api checks quotat via the VQUOTATLEFT view in SQLITE database
  • Quotats can be changed using updates on the TBQUOTAT table
  • Each acocunt must have a quotat in the TBQUOTAT table
  • Sqlite database access must to be secured my an .htaccess file
  • Sqlite database provides SMS history, account and quotat control

What you will find here :

  • 404.gif : a simple 404 gif.
  • index.html : a simple root file that show 404.
  • test.php : a web page to use this api manually, it uses the demo account (password is demopass : take a look at the beginning of this file)
  • smsapi.css : the style file for test.php
  • index.php : the root api php file (I suggest you to user another name)
  • robots.txt : a file to prevent search indexing by search robots
  • A log folder, it must be web server writable : you will find log files here.I recommend to put an htaccess file here to prevent direct downloading.
  • A tmp folder for temporary files, it must be web server writable. I recommend to put an htaccess file here to prevent direct downloading.
  • A db folder, it must be web server writable. It contains smsapi.db file (the sqlite database) that must be web server writable
  • In db folder you have to put a .htaccess file to prevent direct download of database !
    Use someting like this for Apache 2.4 :

    <Files 'smsapi.db'>Require all denied</Files>
    blockquote>
  • A service folder that contains what is needed to help to use gammu :
    • .gammurc is a file used by gammu (the sms binary file). You have to check the line "port = /dev/ttyUSB0" is your Sim card USB adaptater is in another port, and "connection = at19200" if your adaptater is using another dial speed
    • group.txt : A CLI example to add your web serveur user to dialout group : beacause the web server must access to the com port. This exemple is for apache 2.4 on Fedora. Check you web server account, it can be www-data on Debian.
    • usbmodeswitch.conf : This is my usbmodeswitch configuration file : I'm using a Huawei USB Sim Card adaptater : check what configuration you need.
    • pincode.sh : this script must be run at boot time : It will unlock Sim card's protection using your PIN Code : replace your pincode at line "pincode='0000'"
    • pincode.service : The pincode unlocking service description file. Check the pincode.sh path (I'm using /opt/gammu path tu put pincode.sh)

Installation :

Assuming you have a web service that is running
Assuming you have installed gammu
If gammu needs your pin code at each boot (you can check it with gammu getsecuritystatus), you can use my pincode.sh and pincode.service files to do it.
Assuming that gamm can send SMS by cli
Check it with gammu sendsms TEXT aphonenumber -text "Test by CLI"

If you need some gammu config file, define it in index.php like this
DEFINE("GAMMU_CONFIG_FILE","/root/.gammurc"); or DEFINE("GAMMU_CONFIG_FILE","");

Put all the content repo in a document root web server folder (or subfolder)
make ./log, ./tmp and ./db/smsdb.db writable by your web server
Open ./db/smsdb.db with sqlite and create or choose password for the demo account (see TBPARAM).
Check TBQUOTAT for this useror insert a row in TBQUOTAT to make your new user own some SMS to send.

Go to a web navigator and ask for index.php, you will see : 403 (Forbidden)

Open test.php, search at the beginning of the file for : $smspassw=GetVariableFrom($_POST,"smspassw","demopass"); and replace demopass by your demo password if you changed it in TBPARAM.
in index.php search for filterlist function, because the script only accept French phone number, you will have to change the rules if you leave in another country.
Go to a web navigator and ask for test.php you will see a test page, here chose an account (client), put the from phone number (your SIM Card phone number), give one or more recipients, and a message to send.
Chose directsend

You have received this SMS ? Yeah, next one : How to use this api programmaticaly.

How to use this api programmaticaly

See in test.php the action listbox :
You have :

  • new : create a new SMS without sending : you have to give the from number (your SIM Card phone Number) , the message body, the accound and his password. It will return a SMS Id
  • putcc : remplace or put recipients in an existing SMS (you will have to give the SMS Id, the account and his password)
  • getquotat : giving the accound and his password. It will return, how much SMS this account can send
  • directsend : create and send a new SMS : you have to give the from number (your SIM Card phone Number) , the message body, a recipient, the accound and his password. It will return the SMS Id
  • send : send a SMS previously fully created : you have to give ths SMS Id ,the accound and his password.

Calling api

Use curl, python requests library, javascript fetch or other methods. You have to call the index.php script using POST method You have to give these POST values :

  • smscli : this is the string id of the account that want to send a message (ex : demo)
  • smspassw : this is the string password of the account that want to send a message (ex : demopass)
  • from : your SIM card phone number
  • body : The message (use UTF-8 charset !)
  • action : The action to do (new, putcc, getquotat, directsend or send).
  • idsms : The smsid if you want to update or send a message that you have just created.
  • cclist : Recipients list, using semi-colon separator.

Returning values

This api will return you infomations for debugging and check what has been done.
Theses informations are in XML format and UTF-8 encoded. You will find :

  • CR : The return code : 0 If everything is OK
  • MSG : A message if something went wrong
  • DATA : Some data if neccessary, it can be idsms or quotatleft

About

simple web api to delegate sending sms

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages