Script to send SMS via Goip4 gateway for ABillS

I will give an example of a PHP script to which I send SMS messages through the GoIP4 gateway.

First, we choose tariffs with only monthly charges (I chose everything, excluding with daily charges, since there are fewer of them):

SELECT name, tp_id FROM tarif_plans;
SELECT name, tp_id FROM tarif_plans WHERE tp_id NOT IN(1,2,4,8,9,10,39,63,64,65,106,107,109,110,114,117,118,119)


Actually, this is the script itself, I run it from cron on a separate server (where I installed the GoIP4 SMS server):

#!/usr/bin/php
<?php

$mysql_host="192.168.1.5";
$mysql_user="TEXT";
$mysql_password="TEXT";
$mysql_base="abills";

$db=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_base);
mysqli_query($db, "SET NAMES 'utf8'");

$Result = mysqli_query($db,
"
SELECT im.uid, uc.value, b.deposit, im.activate, tp.month_fee
FROM internet_main im
LEFT JOIN users u ON (im.uid = u.uid)
LEFT JOIN users_pi u_pi ON (im.uid = u_pi.uid)
LEFT JOIN bills b ON (u.bill_id= b.id)
LEFT JOIN tarif_plans tp ON (tp.tp_id = im.tp_id)
LEFT JOIN users_contacts uc ON (im.uid = uc.uid)
WHERE im.tp_id NOT IN(1,2,4,8,9,10,39,63,64,65,106,107,109,110,114,117,118,119)
AND uc.type_id=1
AND DATEDIFF(CURDATE(), im.activate) = 27
AND b.deposit+u.credit< tp.month_fee
AND u.reduction=0
AND u.company_id=0
AND u_pi._actual_mob=1;
"
);

while($Row = mysqli_fetch_array($Result))
{
$uid = $Row['uid'];
$phone =$Row['value'];
$deposit = intval($Row['deposit']);
$activate = $Row['activate'];
$date_end = date("d-m", strtotime("$activate +30 days"));
$fee = intval($Row['month_fee']);

$phone_length=strlen($phone);

if($phone_length==10)
{
$mess_ = "Інтернет відключиться $date_end. На рах. $deposit грн, тариф $fee. КОМПАНІЯ";
$mess =  rawurlencode($mess_);

print("$phone           $mess_\n");

$r = file_get_contents("http://192.168.1.55/goip/en/dosend.php?USERNAME=text&PASSWORD=text&smsprovider=3&goipname=lifecell&smsnum=$phone&method=2&Memo=$mess");
mysqli_query($db, "INSERT INTO sms VALUES(NULL, '$uid', '$phone', NOW(), '$mess_', '$activate')");
}
}
?>

There is no “_actual_mob” table in the standard abills database, I have created an additional field with a checkbox that can be used to indicate the relevance of a mobile phone in a user account, 1 is relevant, 0 is not by default.
You can also add another SQL query that removes old records, for example older than 1 year.

For the logs sent by SMS, I created the following table in the ABillS database:

CREATE TABLE IF NOT EXISTS `sms` (
  `id` mediumint(254) NOT NULL AUTO_INCREMENT,
  `uid` mediumint(254) NOT NULL,
  `phone` varchar(32) NOT NULL,
  `datetime` datetime NOT NULL,
  `text` varchar(254) NOT NULL,
  `activate` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=384 DEFAULT CHARSET=utf8 COMMENT='Sms log';

In the future, you can view the contents of the table through the rwizard module or the Settings – SQL Commander menu:

SELECT * FROM sms LIMIT 500

I use Zabbix for monitoring, so I’ve also opened the configuration of Zabbix agent:

/etc/zabbix/zabbix_agentd.conf

И добавил параметр:

UserParameter=sms_sent_today,mysql -u root -e "SELECT count(*) FROM abills.sms WHERE datetime LIKE '`date +%Y-%m-%d`%';" -s -N

And added a data item with an interval of 12 hours, a trigger warning of the excess of the sent SMS, and a schedule on the Zabbix server to see on the chart how many SMS are sent daily.

See also my articles:
SMS sending script via Goip4 gateway
Installing and configuring GoIP SMS server
SQL queries for ABillS

Did my article help you? How about buying me a cup of coffee as an encouragement? Buy me a coffe.

Leave a comment

Leave a Reply