How to validate functions all the time using PHP. To forward messages and notifications

Asked

Viewed 29 times

-2

I would like good practice guidelines for my project, I am structuring a server that will do validations all the time, to forward notifications and also messages to various sectors of a Company.

Examples:

  • If the stock is less than 10, send a message to the responsible sector, so that they are aware.
  • A customer tried to make the payment, but the system presented an error. Send a message to the Support sector so they can get in touch with Fulano da Silva...
  • Every day at 8:00 pm, send a message to the company owner with the Billing report...

I use Pushcut for notifications and Telegram for forwarding messages to the Company’s sectors.

Pushcut

<?php
    $ch = curl_init();
    $title = "Texto dinamico ";
    $btn1 = "Botão 1";
    $btn1Url = "www.empresa.com/acao1";
    $btn2 = "Botão 2";
    $btn2Url = "www.empresa.com/acao2";
    curl_setopt($ch, CURLOPT_URL, 'https://api.pushcut.io/v1/notifications/Faturamento');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\":\"Mensal\",\"title\":\"$title\",\"input\":\"My dynamic input\",\"actions\":[{\"name\":\"$btn1\",\"url\":\"$btn1Url\"}, {\"name\":\"$btn2\",\"url\":\"$btn2Url\"}]}");
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = 'Accept: */*';
    $headers[] = 'Api-Key: TOKEN';
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
?>

Telegram

<?php  
    $boturl = "https://api.telegram.org/bot#####";
    $chat_id = "123456789";
    $c="Mensagem aqui";

    file_get_contents($boturl.'/sendmessage?chat_id='.$chat_id.'&text='.$c.'&reply_markup={"inline_keyboard":[[{"text":"Press here to open URL","url":"https://example.com"}]]}'); ?>
  • The validations I can perform is only when the page is reloaded. But if I put a script to keep reloading when there is a change the page will stay in refresh all the time

1 answer

0

To solve the problem, I found about Cron Tasks or Cron Jobs. So I put on my server to run the function every so long and not when there is a change in the database.

Using the following Curl command "https://domain.com/folder/"

After that, just validate to only send the message when there is something new using

if ($valida) { // Send a notification }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.