alireza.stack Posted February 24, 2017 Share Posted February 24, 2017 (edited) 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 February 24, 2017 by alireza.stack (see edit history) Link to comment Share on other sites More sharing options...
rocky Posted February 26, 2017 Share Posted February 26, 2017 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); } } Link to comment Share on other sites More sharing options...
alireza.stack Posted February 26, 2017 Author Share Posted February 26, 2017 (edited) Where should I put this class? In classes folder? How should I include it in my modules and my classes? Edited February 26, 2017 by alireza.stack (see edit history) Link to comment Share on other sites More sharing options...
rocky Posted February 27, 2017 Share Posted February 27, 2017 You can save it to classes/Telegram.php and then add the following to any files that need to use the class: require_once(_PS_CLASS_DIR_.'Telegram.php'); Link to comment Share on other sites More sharing options...
pevoct Posted September 26, 2017 Share Posted September 26, 2017 Hello, i add this but where i can add for example to received orders notification Thanks Regards Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now