کد مربوط به ربات تلگرام روی هاست خارجی:
کد:
$botToken = "36654000000000000000000000000000PDA";
$webSite = "https://api.telegram.org/bot" . $botToken;
$update = file_get_contents("php://input");
$update = json_decode($update, TRUE);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
if($message !== null){
if($chatId === null) $chatId=0;
switch ($message) {
case "/start":
sendMessage($chatId, "کاربر محترم به ربات نوتيف سايت خوش اومديد آي دي شما جهت ارسال پيام ".$chatId. " مي باشد.");
break;
case "/chat_id":
sendMessage($chatId, "Chat_ID: ".$chatId);
break;
default:
sendMessage($chatId, "دستور وارد شده نامعتبر است!");
}
}
function sendMessage($chatId, $message)
{
$url = $GLOBALS['webSite'] . "/sendMessage?chat_id=" . $chatId . "&text=" .urlencode($message);
file_get_contents($url);
}
کد مربوط به وب سرویس روی هاست خارجی:
کد:
<?php
require_once ('lib/nusoap.php');
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
if (PHP_VERSION_ID < 50207) {
define('PHP_MAJOR_VERSION', $version[0]);
define('PHP_MINOR_VERSION', $version[1]);
define('PHP_RELEASE_VERSION', $version[2]);
}
class Notifcaster_Telegram
{
protected
$api_token = '',
$chat_id = null,
$url = '',
$api_method = null;
/**
* Notifcaster API constructor
* @param string $api_token
* @param string $url
*/
public function Notifcaster($token_bot,$chat_id)
{
$this->api_token = $token_bot;
$this->chat_id = $chat_id;
$this->url = 'https://api.telegram.org/bot'.$this->api_token;
}
/**
* Telegram API constructor
*
* @param string $bot_token
*
*/
public function _telegram($bot_token)
{
$this->url = 'https://api.telegram.org/bot'.$bot_token;
}
/**
* Send Notification to user
*
* @param string $msg
*
* @return string
*/
public function notify($msg = 'NULL')
{
$params = array(
'chat_id' => $this->chat_id,
'text' => $msg
);
$this->api_method = "/sendMessage";
$this->url = 'https://api.telegram.org/bot'.$this->api_token.$this->api_method;
$response = $this->make_request($params);
return $response;
}
/**
* Request Function
*
* @param array $params
* @param string $file_upload
*
* @return string "success" || error message
*/
protected function make_request(array $params = array())
{
if (function_exists('curl_init')) {
$curl = curl_init($this->url);
$params = http_build_query($params);
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $params
));
$response = curl_exec($curl);
curl_close($curl);
} else {
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $post,
'timeout' => 10,
),
));
$response = file_get_contents($this->url.$this->api_method, false, $context);
}
return $this->response = json_decode($response, true);
}
}
class telegram{
private $token_user;
private $token_bot;
private $sendnotify_users;
public function __construct()
{
$this->token_users = '0000000';
$this->token_bot = '3665000000000000000000000000PDA';
$this->sendnotify_users = true;
}
public function send_notif_to_user($msg){
if($this->sendnotify_users) {
$token_user = explode(',', $this->token_users);
foreach ($token_user as $value) {
$nt = new Notifcaster_Telegram();
$nt->Notifcaster($this->token_bot,$value);
$nt->notify($msg.PHP_EOL);
}
return 'OK';
}
return 'NO';
}
}
function verify_key($key){
return ($key === '#keystring#');
}
function send_notif($msg,$key){
if(verify_key($key)){
$notif_telegram = new telegram();
return json_encode($notif_telegram->send_notif_to_user($msg));
}
}
try {
$server = new nusoap_server();
$server->configureWSDL('web_service', 'urn:web_service');
$server->register('send_notif',
array('msg' => 'xsd:string','key' => 'xsd:string'), //parameter
array('data' => 'xsd:string'), //output
'urn:web_service', //namespace
'urn:web_service#send_notif' //soapaction
);
$server->service(file_get_contents("php://input"));
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
کد مربوط به ارسال پیام به ربات تلگرام در هاست های ایرانی (استفاده از وب سرویس ایجاد شده بر روی هاست خارجی):
کد:
$result = array();
$wsdl = $this->settings['telegram_proxy'];
//require_once ('../lib/nusoap.php');
$client = new nusoap_client($wsdl, true);
$err = $client->getError();
if ($err) {
//echo '<h2>Constructor error</h2>' . $err;
//exit();
} else{
$result = $client->call('send_notif', array($msg.PHP_EOL.moduls::get_home_url(),$this->settings['telegram_key']));
//$result = json_decode($result);
}
نکته: دقت کنید که حتما پوشه lib مربوط به فایل های وب سرویس در کنار پروژه باشد(lib/nusoap.php).