Last active
August 11, 2016 06:28
-
-
Save kas-cor/1ee5d1a37857ef8f9a3ddf584a095cb5 to your computer and use it in GitHub Desktop.
Telegram sendMessage API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author kas-cor <[email protected]> | |
* @link http://github.com/kas-cor repositories | |
*/ | |
namespace cls; | |
class Telegram { | |
/** | |
* Access token API | |
* @var string | |
*/ | |
private $token; | |
/** | |
* @param string $token Access token | |
*/ | |
public function __construct($token) { | |
$this->token = $token; | |
} | |
/** | |
* Send message to chat id | |
* @param string $chat_id | |
* @param string $text | |
* @return string response json | |
*/ | |
public function sendMessage($chat_id, $text) { | |
$message = urlencode(strip_tags(trim($this->convertToUtf8($text)))); | |
$url = "https://api.telegram.org/bot" . $this->token . "/sendMessage?chat_id=" . $chat_id . "&text=" . $message; | |
$curl_handle = curl_init(); | |
curl_setopt($curl_handle, CURLOPT_URL, $url); | |
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); | |
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl_handle, CURLOPT_USERAGENT, "TelegramBot"); | |
$json = curl_exec($curl_handle); | |
curl_close($curl_handle); | |
return $json; | |
} | |
/** | |
* Convert to utf-8, if win-1251 | |
* @param type $text | |
* @return type | |
*/ | |
private function convertToUtf8($text) { | |
if (!preg_match("//u", $text)) { | |
return iconv("windows-1251", "utf-8", $text); | |
} else { | |
return $text; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useing