2022-05-14 00:37:40 +02:00
<!DOCTYPE html>
< html class = "" >
< head >
< meta charset = "utf-8" >
< title > Hellobot< / title >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< meta property = "description" content = "This sample PHP bot demonstrates the basics of the Telegram Bot API .
If you have questions, try our FAQ or check out this…">
< meta property = "og:title" content = "Hellobot" >
< meta property = "og:image" content = "" >
< meta property = "og:description" content = "This sample PHP bot demonstrates the basics of the Telegram Bot API .
If you have questions, try our FAQ or check out this…">
< link rel = "icon" type = "image/svg+xml" href = "/img/website_icon.svg?4" >
< link rel = "apple-touch-icon" sizes = "180x180" href = "/img/apple-touch-icon.png" >
< link rel = "icon" type = "image/png" sizes = "32x32" href = "/img/favicon-32x32.png" >
< link rel = "icon" type = "image/png" sizes = "16x16" href = "/img/favicon-16x16.png" >
< link rel = "alternate icon" href = "/img/favicon.ico" type = "image/x-icon" / >
< link href = "/css/bootstrap.min.css?3" rel = "stylesheet" >
2023-03-20 12:08:32 +01:00
< link href = "/css/telegram.css?236" rel = "stylesheet" media = "screen" >
2022-05-14 00:37:40 +02:00
< style >
< / style >
< / head >
< body class = "preload" >
< div class = "dev_page_wrap" >
< div class = "dev_page_head navbar navbar-static-top navbar-tg" >
< div class = "navbar-inner" >
< div class = "container clearfix" >
< ul class = "nav navbar-nav navbar-right hidden-xs" > < li class = "navbar-twitter" > < a href = "https://twitter.com/telegram" target = "_blank" data-track = "Follow/Twitter" onclick = "trackDlClick(this, event)" > < i class = "icon icon-twitter" > < / i > < span > Twitter< / span > < / a > < / li > < / ul >
< ul class = "nav navbar-nav" >
< li > < a href = "//telegram.org/" > Home< / a > < / li >
< li class = "hidden-xs" > < a href = "//telegram.org/faq" > FAQ< / a > < / li >
< li class = "hidden-xs" > < a href = "//telegram.org/apps" > Apps< / a > < / li >
< li class = "" > < a href = "/api" > API< / a > < / li >
< li class = "" > < a href = "/mtproto" > Protocol< / a > < / li >
< li class = "" > < a href = "/schema" > Schema< / a > < / li >
< / ul >
< / div >
< / div >
< / div >
< div class = "container clearfix" >
< div class = "dev_page" >
< div id = "dev_page_content_wrap" class = " " >
< div class = "dev_page_bread_crumbs" > < ul class = "breadcrumb clearfix" > < li > < a href = "/bots" > Telegram Bots< / a > < / li > < i class = "icon icon-breadcrumb-divider" > < / i > < li > < a href = "/bots/samples" > Code Examples< / a > < / li > < i class = "icon icon-breadcrumb-divider" > < / i > < li > < a href = "/bots/samples%2Fhellobot" > Hellobot< / a > < / li > < / ul > < / div >
< h1 id = "dev_page_title" > Hellobot< / h1 >
< div id = "dev_page_content" > < blockquote >
< p > This sample PHP bot demonstrates the basics of the < a href = "/bots/api" > Telegram Bot API< / a > .< br > If you have questions, try our < a href = "/bots/faq" > FAQ< / a > or check out < a href = "/bots/samples" > this page< / a > for more examples.< / p >
< / blockquote >
< div > < br > < / div >
< pre > < code > < ?php
define(' BOT_TOKEN' , ' 12345678:replace-me-with-real-token' );
define(' API_URL' , ' https://api.telegram.org/bot' .BOT_TOKEN.' /' );
function apiRequestWebhook($method, $parameters) {
if (!is_string($method)) {
error_log(" Method name must be a string\n" );
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log(" Parameters must be an array\n" );
return false;
}
$parameters[" method" ] = $method;
$payload = json_encode($parameters);
header(' Content-Type: application/json' );
header(' Content-Length: ' .strlen($payload));
echo $payload;
return true;
}
function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log(" Curl returned error $errno: $error\n" );
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code > = 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log(" Request has failed with error {$response[' error_code' ]}: {$response[' description' ]}\n" );
if ($http_code == 401) {
throw new Exception(' Invalid access token provided' );
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response[' description' ])) {
error_log(" Request was successful: {$response[' description' ]}\n" );
}
$response = $response[' result' ];
}
return $response;
}
function apiRequest($method, $parameters) {
if (!is_string($method)) {
error_log(" Method name must be a string\n" );
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log(" Parameters must be an array\n" );
return false;
}
foreach ($parameters as $key => & $val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) & & !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method.' ?' .http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
return exec_curl_request($handle);
}
function apiRequestJson($method, $parameters) {
if (!is_string($method)) {
error_log(" Method name must be a string\n" );
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log(" Parameters must be an array\n" );
return false;
}
$parameters[" method" ] = $method;
$handle = curl_init(API_URL);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($handle, CURLOPT_HTTPHEADER, array(" Content-Type: application/json" ));
return exec_curl_request($handle);
}
function processMessage($message) {
// process incoming message
$message_id = $message[' message_id' ];
$chat_id = $message[' chat' ][' id' ];
if (isset($message[' text' ])) {
// incoming text message
$text = $message[' text' ];
if (strpos($text, " /start" ) === 0) {
apiRequestJson(" sendMessage" , array(' chat_id' => $chat_id, " text" => ' Hello' , ' reply_markup' => array(
' keyboard' => array(array(' Hello' , ' Hi' )),
' one_time_keyboard' => true,
' resize_keyboard' => true)));
} else if ($text === " Hello" || $text === " Hi" ) {
apiRequest(" sendMessage" , array(' chat_id' => $chat_id, " text" => ' Nice to meet you' ));
} else if (strpos($text, " /stop" ) === 0) {
// stop now
} else {
apiRequestWebhook(" sendMessage" , array(' chat_id' => $chat_id, " reply_to_message_id" => $message_id, " text" => ' Cool' ));
}
} else {
apiRequest(" sendMessage" , array(' chat_id' => $chat_id, " text" => ' I understand only text messages' ));
}
}
define(' WEBHOOK_URL' , ' https://my-site.example.com/secret-path-for-webhooks/' );
if (php_sapi_name() == ' cli' ) {
// if run from console, set or delete webhook
apiRequest(' setWebhook' , array(' url' => isset($argv[1]) & & $argv[1] == ' delete' ? ' ' : WEBHOOK_URL));
exit;
}
$content = file_get_contents(" php://input" );
$update = json_decode($content, true);
if (!$update) {
// receive wrong update, must not happen
exit;
}
if (isset($update[" message" ])) {
processMessage($update[" message" ]);
}< / code > < / pre >
< / div >
< / div >
< / div >
< / div >
< div class = "footer_wrap" >
< div class = "footer_columns_wrap footer_desktop" >
< div class = "footer_column footer_column_telegram" >
< h5 > Telegram< / h5 >
< div class = "footer_telegram_description" > < / div >
Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed.
< / div >
< div class = "footer_column" >
< h5 > < a href = "//telegram.org/faq" > About< / a > < / h5 >
< ul >
< li > < a href = "//telegram.org/faq" > FAQ< / a > < / li >
2022-09-09 12:10:24 +02:00
< li > < a href = "//telegram.org/privacy" > Privacy< / a > < / li >
2022-09-09 23:58:59 +02:00
< li > < a href = "//telegram.org/press" > Press< / a > < / li >
2022-05-14 00:37:40 +02:00
< / ul >
< / div >
< div class = "footer_column" >
< h5 > < a href = "//telegram.org/apps#mobile-apps" > Mobile Apps< / a > < / h5 >
< ul >
< li > < a href = "//telegram.org/dl/ios" > iPhone/iPad< / a > < / li >
2022-09-09 23:58:59 +02:00
< li > < a href = "//telegram.org/android" > Android< / a > < / li >
< li > < a href = "//telegram.org/dl/web" > Mobile Web< / a > < / li >
2022-05-14 00:37:40 +02:00
< / ul >
< / div >
< div class = "footer_column" >
< h5 > < a href = "//telegram.org/apps#desktop-apps" > Desktop Apps< / a > < / h5 >
< ul >
< li > < a href = "//desktop.telegram.org/" > PC/Mac/Linux< / a > < / li >
< li > < a href = "//macos.telegram.org/" > macOS< / a > < / li >
< li > < a href = "//telegram.org/dl/web" > Web-browser< / a > < / li >
< / ul >
< / div >
< div class = "footer_column footer_column_platform" >
< h5 > < a href = "/" > Platform< / a > < / h5 >
< ul >
< li > < a href = "/api" > API< / a > < / li >
< li > < a href = "//translations.telegram.org/" > Translations< / a > < / li >
< li > < a href = "//instantview.telegram.org/" > Instant View< / a > < / li >
< / ul >
< / div >
< / div >
< div class = "footer_columns_wrap footer_mobile" >
< div class = "footer_column" >
< h5 > < a href = "//telegram.org/faq" > About< / a > < / h5 >
< / div >
< div class = "footer_column" >
< h5 > < a href = "//telegram.org/blog" > Blog< / a > < / h5 >
< / div >
< div class = "footer_column" >
< h5 > < a href = "//telegram.org/apps" > Apps< / a > < / h5 >
< / div >
< div class = "footer_column" >
< h5 > < a href = "/" > Platform< / a > < / h5 >
< / div >
< div class = "footer_column" >
2024-02-14 19:35:18 +01:00
< h5 > < a href = "//telegram.org/press" > Press< / a > < / h5 >
2022-05-14 00:37:40 +02:00
< / div >
< / div >
< / div >
< / div >
2022-12-10 23:50:15 +01:00
< script src = "/js/main.js?47" > < / script >
2022-05-14 00:37:40 +02:00
< script > b a c k T o T o p I n i t ( " G o u p " ) ;
removePreloadInit();
< / script >
< / body >
< / html >