Streamlining Football Event Notifications: A Procedural Approach with Slack Integration
Embarking on my first project at Pitchero, I leveraged my foundational knowledge of PHP to develop a more efficient system for notifying data entry staff about new football events. In the early stages, the process involved manually reloading pages and parsing for new events, leading to potential human errors. Recognizing the need for automation, I implemented a procedural solution using the Slack API to create a bot that could seamlessly inform the team of new events.
Implementation
The heart of the system was a PHP-based bot triggered by a cron job every minute during live football fixtures. This marked my initiation into working with APIs, enabling me to streamline the notification process and contribute to the team's efficiency.
Key Steps in Implementation
1. Slack API Integration
Utilizing the Slack API, I developed a bot capable of posting messages to a dedicated channel whenever a new football event was created. This not only eliminated the need for manual checking but also reduced the chances of human error.
2. Cron Job Automation
To ensure real-time updates, the bot was set to run as a cron job every minute during live football fixtures. This frequent execution guaranteed timely notifications for the data entry staff, enhancing the overall responsiveness of the system.
3. Weekly Fixture Retrieval
The procedural nature of the system involved running a PHP file every week to obtain the upcoming week's fixtures. This preemptive step ensured that the bot would be equipped with the latest information, allowing it to accurately post notifications during the weekend.
Example code
These code snippets represent saved instances of this system. At the time of creation, I wasn't utilizing Git. However, upon adopting Git later, I made sure to keep a copy for future reference.
Initial import of fixtures
<?php
include('connection.php');
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
function databaseInsert($conn,$matchID,$home_team,$away_team,$matchDate){
$query = "INSERT INTO `match_notifications` (`match_id`,`event_id`,`home_team`,`away_team`,`match_date`,`status`) VALUES ('".$matchID."','', '".$home_team."', '".$away_team."','".$matchDate."','0')";
$data = mysqli_query($conn,$query);
}
function databaseUpdate($conn,$matchID,$event_idss){
$query = "UPDATE `match_notifications` SET `event_id`='".$event_idss."' WHERE `match_id`= '".$matchID."'";
$data = mysqli_query($conn,$query);
}
function databaseSelect($conn,$matchID){
$query = "SELECT * FROM `match_notifications` WHERE `match_id`='".$matchID."'";
$data = mysqli_query($conn,$query);
return $data;
}
$sXML = download_page('http://football.api.press.net/v1.5/competition/fixtures/[apikey]/167'); //replace apikey with the api key
$oXML = new SimpleXMLElement($sXML);
$arraydata = json_decode(json_encode((array)$oXML), TRUE);
foreach($arraydata['fixture'] as $matchData){
$matchID = $matchData['@attributes']['matchID'];
$matchDate = $matchData['@attributes']['date'];
$awayTeam = $matchData['awayTeam'];
$homeTeam = $matchData['homeTeam'];
$currentDate = date('d/m/Y');
if(strtotime($matchDate) == strtotime($currentDate)){
$dbSelect = databaseSelect($conn,$matchID);
if($dbSelect->num_rows == 0){
$dbInsert = databaseInsert($conn,$matchID,$awayTeam,$homeTeam,$matchDate);
}
}
}
Notfication system
<?php
include('connection.php');
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
function databaseInsert($conn,$matchID,$home_team,$away_team){
$query = "INSERT INTO `match_notifications_nl` (`match_id`,`event_id`,`home_team`,`away_team`) VALUES ('".$matchID."','', '".$home_team."', '".$away_team."')";
$data = mysqli_query($conn,$query);
}
function databaseUpdate($conn,$matchID,$event_idss){
$query = "UPDATE `match_notifications_nl` SET `event_id`='".$event_idss."' WHERE `match_id`= '".$matchID."'";
$data = mysqli_query($conn,$query);
}
function databaseSelect($conn,$matchID){
$query = "SELECT * FROM `match_notifications_nl` WHERE `match_id`='".$matchID."'";
$data = mysqli_query($conn,$query);
return $data;
}
function databaseSelectSingle($conn){
$query = "SELECT * FROM `match_notifications_nl` WHERE `status`='0'";
$data = mysqli_query($conn,$query);
return $data;
}
function curlPost($data){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'slack webhook', //set your slack chanel webhook here
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
'Content-type: application/json'
)
));
$resp = curl_exec($curl);
curl_close($curl);
}
$sindleSata = databaseSelectSingle($conn);
$dataTeam = array();
while($matchData=mysqli_fetch_assoc($sindleSata)){
$matchID = $matchData['match_id'];
$matchDate = $matchData['match_date'];
$awayTeam = $matchData['home_team'];
$homeTeam = $matchData['away_team'];
$currentDate = date('d/m/Y');
if(strtotime($matchDate) == strtotime($currentDate)){
$path3 ="http://football.api.press.net/v1.5/match/info/[apikey]/".$matchID;
$matchInfo = download_page($path3);
$matchDetailInfo = new SimpleXMLElement($matchInfo);
$matchInfoarray = json_decode(json_encode((array)$matchDetailInfo), TRUE);
$hometeamID = $matchInfoarray['match']['homeTeam']['@attributes']['teamID'];
$hometeamName = $matchInfoarray['match']['homeTeam']['teamName'];
$awayteamID = $matchInfoarray['match']['awayTeam']['@attributes']['teamID'];
$awayteamName = $matchInfoarray['match']['awayTeam']['teamName'];
$dataTeam[$hometeamID]=$hometeamName;
$dataTeam[$awayteamID]=$awayteamName;
$pathnew = "http://football.api.press.net/v1.5/match/events/[apikey]/".$matchID;
$matchDetail = download_page($pathnew);
$matchDetailData = new SimpleXMLElement($matchDetail);
$matchDetailarray = json_decode(json_encode((array)$matchDetailData), TRUE);
foreach($matchDetailarray['events']['event'] as $events){
$eventsDetail = array('substitution','goal','booking','dismissal','own goal');
if(in_array($events['eventType'], $eventsDetail)){
$event_id = $events['@attributes']['eventID'];
$teamID = $events['@attributes']['teamID'];
$matchTime = $events['matchTime'];
$player = $events['players']['player1'];
$player = $events['players']['player1'];
$dataIn = databaseSelect($conn,$matchID);
$fetchData = mysqli_fetch_assoc($dataIn);
$eventIDdb = $fetchData['event_id'];
if(empty($eventIDdb)){
$event_idss = $event_id;
}else{
$event_idss = $eventIDdb.','.$event_id;
$idsArray = explode(',',$eventIDdb);
}
if(!in_array($event_id,$idsArray)){
$dbUpdate = databaseUpdate($conn,$matchID,$event_idss);
if($events['eventType'] == 'substitution'){
$player2 = $events['players']['player2'];
$test = ":subs:*SUB*: (".$dataTeam[$teamID]."), :subon: *[".$player."]* :suboff: *[".$player2."]* @ ".$matchTime;
}elseif($events['eventType'] == 'goal'){
$test = ":soccer:*GOAL*: (".$dataTeam[$teamID].") *[".$player."]* @ ".$matchTime;
}elseif($events['eventType'] == 'booking'){
$test = ":yellowcard:*YELLOW CARD*: (".$dataTeam[$teamID].") *[".$player."]* @ ".$matchTime;
}elseif($events['eventType'] == 'dismissal'){
$test =":redcard:*RED CARD*: (".$dataTeam[$teamID].") *[".$player."]* @ ".$matchTime;
}elseif($events['eventType'] == 'own goal'){
$test =":owngoal:*OWN GOAL*: (".$dataTeam[$teamID].") *[".$player."]* @ ".$matchTime;
}else{
}
$dataNotification = array('text'=>$test);
$postData = curlPost($dataNotification);
}
}
}
}
}