telegram-crawler/data/web/blogfork.telegram.org/bots/samples/hellobot.html
2022-05-13 22:37:40 +00:00

290 lines
10 KiB
HTML

<!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">
<link href="/css/telegram.css?230" rel="stylesheet" media="screen">
<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>&lt;?php
define(&#39;BOT_TOKEN&#39;, &#39;12345678:replace-me-with-real-token&#39;);
define(&#39;API_URL&#39;, &#39;https://api.telegram.org/bot&#39;.BOT_TOKEN.&#39;/&#39;);
function apiRequestWebhook($method, $parameters) {
if (!is_string($method)) {
error_log(&quot;Method name must be a string\n&quot;);
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log(&quot;Parameters must be an array\n&quot;);
return false;
}
$parameters[&quot;method&quot;] = $method;
$payload = json_encode($parameters);
header(&#39;Content-Type: application/json&#39;);
header(&#39;Content-Length: &#39;.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(&quot;Curl returned error $errno: $error\n&quot;);
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code &gt;= 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(&quot;Request has failed with error {$response[&#39;error_code&#39;]}: {$response[&#39;description&#39;]}\n&quot;);
if ($http_code == 401) {
throw new Exception(&#39;Invalid access token provided&#39;);
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response[&#39;description&#39;])) {
error_log(&quot;Request was successful: {$response[&#39;description&#39;]}\n&quot;);
}
$response = $response[&#39;result&#39;];
}
return $response;
}
function apiRequest($method, $parameters) {
if (!is_string($method)) {
error_log(&quot;Method name must be a string\n&quot;);
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log(&quot;Parameters must be an array\n&quot;);
return false;
}
foreach ($parameters as $key =&gt; &amp;$val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) &amp;&amp; !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method.&#39;?&#39;.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(&quot;Method name must be a string\n&quot;);
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log(&quot;Parameters must be an array\n&quot;);
return false;
}
$parameters[&quot;method&quot;] = $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(&quot;Content-Type: application/json&quot;));
return exec_curl_request($handle);
}
function processMessage($message) {
// process incoming message
$message_id = $message[&#39;message_id&#39;];
$chat_id = $message[&#39;chat&#39;][&#39;id&#39;];
if (isset($message[&#39;text&#39;])) {
// incoming text message
$text = $message[&#39;text&#39;];
if (strpos($text, &quot;/start&quot;) === 0) {
apiRequestJson(&quot;sendMessage&quot;, array(&#39;chat_id&#39; =&gt; $chat_id, &quot;text&quot; =&gt; &#39;Hello&#39;, &#39;reply_markup&#39; =&gt; array(
&#39;keyboard&#39; =&gt; array(array(&#39;Hello&#39;, &#39;Hi&#39;)),
&#39;one_time_keyboard&#39; =&gt; true,
&#39;resize_keyboard&#39; =&gt; true)));
} else if ($text === &quot;Hello&quot; || $text === &quot;Hi&quot;) {
apiRequest(&quot;sendMessage&quot;, array(&#39;chat_id&#39; =&gt; $chat_id, &quot;text&quot; =&gt; &#39;Nice to meet you&#39;));
} else if (strpos($text, &quot;/stop&quot;) === 0) {
// stop now
} else {
apiRequestWebhook(&quot;sendMessage&quot;, array(&#39;chat_id&#39; =&gt; $chat_id, &quot;reply_to_message_id&quot; =&gt; $message_id, &quot;text&quot; =&gt; &#39;Cool&#39;));
}
} else {
apiRequest(&quot;sendMessage&quot;, array(&#39;chat_id&#39; =&gt; $chat_id, &quot;text&quot; =&gt; &#39;I understand only text messages&#39;));
}
}
define(&#39;WEBHOOK_URL&#39;, &#39;https://my-site.example.com/secret-path-for-webhooks/&#39;);
if (php_sapi_name() == &#39;cli&#39;) {
// if run from console, set or delete webhook
apiRequest(&#39;setWebhook&#39;, array(&#39;url&#39; =&gt; isset($argv[1]) &amp;&amp; $argv[1] == &#39;delete&#39; ? &#39;&#39; : WEBHOOK_URL));
exit;
}
$content = file_get_contents(&quot;php://input&quot;);
$update = json_decode($content, true);
if (!$update) {
// receive wrong update, must not happen
exit;
}
if (isset($update[&quot;message&quot;])) {
processMessage($update[&quot;message&quot;]);
}</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>
<li><a href="//telegram.org/blog">Blog</a></li>
<li><a href="//telegram.org/jobs">Jobs</a></li>
</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>
<li><a href="//telegram.org/dl/android">Android</a></li>
<li><a href="//telegram.org/dl/wp">Windows Phone</a></li>
</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">
<h5><a href="https://twitter.com/telegram" target="_blank" data-track="Follow/Twitter" onclick="trackDlClick(this, event)">Twitter</a></h5>
</div>
</div>
</div>
</div>
<script src="/js/main.js?46"></script>
<script>backToTopInit("Go up");
removePreloadInit();
</script>
</body>
</html>