Jump to content

The best way to handle global telegram sending function


alireza.stack
 Share

Recommended Posts

For my prestashop website I usually send messages to telegram on different conditions. What I don't like about what I do is that I declare telegram functions in each class I want to use telegram like below:

 

private function _sendTelegramMessage($chatID, $messaggio, $token) {
        $url = "https://api.telegram.org/" . $token . "/sendMessage?chat_id=" . $chatID;
        $url = $url . "&text=" . urlencode($messaggio);
        $ch = curl_init();
        $optArray = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
        );
        curl_setopt_array($ch, $optArray);
        $result = curl_exec($ch);
        curl_close($ch);
}
 
I have to include this function in every class I use. How to declare a global function accessible every where? Beside that I want to put Telegram URL in global config which is accessible in classes and modules. I want to follow prestashop rules, but not completely familiar.
 
How should I handle this procedure?
Edited by alireza.stack (see edit history)

Share this post


Link to post
Share on other sites

I think it would be better for you to create a Telegram class with the above as a public static function, then you can use Telegram::_sendTelegramMessage($chatID, $messaggio, $token) wherever you like. For example:



class Telegram
{
public static function sendTelegramMessage($chatID, $messaggio, $token)
{
$url = "https://api.telegram.org/".$token."/sendMessage?chat_id=".$chatID;
       $url = $url."&text=".urlencode($messaggio);
        $ch = curl_init();
        $optArray = array(
           CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
        );
        curl_setopt_array($ch, $optArray);
        $result = curl_exec($ch);
        curl_close($ch);
}
}

Share this post


Link to post
Share on other sites

  • 6 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More