{"id":62,"date":"2025-06-21T17:40:21","date_gmt":"2025-06-21T09:40:21","guid":{"rendered":"https:\/\/singaubulit.com\/blog\/?p=62"},"modified":"2025-06-21T17:41:12","modified_gmt":"2025-06-21T09:41:12","slug":"telegram-bot-with-php","status":"publish","type":"post","link":"https:\/\/singaubulit.com\/blog\/2025\/06\/21\/telegram-bot-with-php\/","title":{"rendered":"Telegram Bot with PHP"},"content":{"rendered":"\n<p>I&#8217;m writing this for future use. 4 steps.<\/p>\n\n\n\n<p>But before that&#8230; need SSL certificates to run this thing. If you already have a web server, carry on. Otherwise, fix that problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create new bot with <a href=\"https:\/\/telegram.me\/BotFather\">TheBotFather<\/a> (via Telegram)<\/li>\n\n\n\n<li>Invoke <strong>\/newbot<\/strong><\/li>\n\n\n\n<li>Give the bot a <strong>name<\/strong><\/li>\n\n\n\n<li>Give the bot an <strong>ID<\/strong> (only _ is the allowed special character)<\/li>\n\n\n\n<li>When all is done, a message containing the <strong>token<\/strong> will be given<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a directory for the bot (this is your bot&#8217;s root)<\/li>\n\n\n\n<li>Create <strong>index.php<\/strong> in root. Everything will be here. If you create separate files, you can <em>&lt;?php include(); ?><\/em> or <em>&lt;?php require_once(); ?><\/em> like a normal PHP file. For the sake of this post assume everything is in a single file to avoid headaches.<\/li>\n\n\n\n<li>Now your bot&#8217;s URL might be <em>https:\/\/your.bot\/index.php<\/em><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Activate <strong>Webhook<\/strong> so that you can use Telegram API. Edit the token in the sample URL below and run it in a web browser.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><a href=\"https:\/\/api.telegram.org\/bot<token_here&gt;\/setwebhook?url=https:\/\/your.bot\/index.php\">https:\/\/api.telegram.org\/bot&lt;token_here>\/setwebhook?url=https:\/\/your.bot\/index.php<\/a><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Setup the bot in <strong>index.php<\/strong> like so:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$auth = \"https:\/\/api.telegram.org\/bot&lt;token_here>\";\n$chatId = $update&#91;\"message\"]&#91;\"chat\"]&#91;\"id\"];\n$msgId = $update&#91;\"message\"]&#91;\"message_id\"];\n$sender = $update&#91;\"message\"]&#91;\"from\"]&#91;\"first_name\"];\n$username = $update&#91;\"message\"]&#91;\"from\"]&#91;\"username\"];\n$senderId = $update&#91;\"message\"]&#91;\"from\"]&#91;\"id\"];\n$getmsg = $update&#91;\"message\"]&#91;\"text\"];\n$sendmsg = \"\/sendMessage?parse_mode=HTML&amp;chat_id=\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$auth = &#8220;https:\/\/api.telegram.org\/bot&lt;token_here>&#8221;; \/\/ need this to authorize json requests<\/li>\n\n\n\n<li>$chatId = $update[&#8220;message&#8221;][&#8220;chat&#8221;][&#8220;id&#8221;]; \/\/ the active chat window<\/li>\n\n\n\n<li>$msgId = $update[&#8220;message&#8221;][&#8220;message_id&#8221;]; \/\/ ID of a specific message within an active chat<\/li>\n\n\n\n<li>$sender = $update[&#8220;message&#8221;][&#8220;from&#8221;][&#8220;first_name&#8221;]; \/\/ first name of a sender<\/li>\n\n\n\n<li>$username = $update[&#8220;message&#8221;][&#8220;from&#8221;][&#8220;username&#8221;]; \/\/ username of a sender (if any) &#8211; some users don&#8217;t have it<\/li>\n\n\n\n<li>$senderId = $update[&#8220;message&#8221;][&#8220;from&#8221;][&#8220;id&#8221;]; \/\/ ID of a sender (fixed and hidden)<\/li>\n\n\n\n<li>$getmsg = $update[&#8220;message&#8221;][&#8220;text&#8221;]; \/\/ the text being sent over<\/li>\n\n\n\n<li>$sendmsg = &#8220;\/sendMessage?parse_mode=HTML&amp;chat_id=&#8221;; \/\/ send message process itself<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a sample greeting from the bot when a command is invoked using <strong>\/start<\/strong> (the default command upon first launch of a bot<\/li>\n\n\n\n<li>You can \/create \/your \/own \/commands too<\/li>\n\n\n\n<li>Be sure to wrap everything you&#8217;re doing with&#8230;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n...the block\n?><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can also build a webpage like structure with &lt;title> and all; won&#8217;t affect the bot.<\/li>\n\n\n\n<li>Begin the block with<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if (\n$getmsg == \"\/start\") {<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Then the workings of this block&#8230;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$message = \"wassup\";\n$url = $auth . \"\/sendMessage?parse_mode=HTML&amp;chat_id=\" . $chatId . \"&amp;text=\" . urlencode($message);\nfile_get_contents($url);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$message = &#8220;wassup&#8221;; \/\/ the message<\/li>\n\n\n\n<li>$url = $auth . &#8220;\/sendMessage?parse_mode=HTML&amp;chat_id=&#8221; . $chatId . &#8220;&amp;text=&#8221; . urlencode($message); \/\/ the sending code; you can put \/sendMessage?parse_mode=HTML in a variable too &#8211; $message&#8217;s content can be added directly in this line too like this: text=&lt;the message> or . urlencode(&#8220;&lt;the message>&#8221;) .<\/li>\n\n\n\n<li>file_get_contents($url); \/\/ this reads the string (the message and all) via https<\/li>\n\n\n\n<li><strong>A message appears saying &#8220;wassup&#8221; \ud83d\ude42<\/strong><\/li>\n\n\n\n<li>Now close this block&#8230;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>} \/\/ yes, this\n\nThat is it. Sending a photo is slightly different than sending a text.<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>file_get_contents(\"https:\/\/api.telegram.org\/$ttoken\/sendPhoto?chat_id=$chatId&amp;photo=\" . urlencode($photoUrl) . \"&amp;caption=\" . urlencode($caption));<\/code><\/pre>\n\n\n\n<p>Set <strong>$photoURL<\/strong> beforehand of course with the actual photo&#8217;s URL. In this example <strong>$url<\/strong> is not used, you can also inline everything. The only difference is you can add the <strong>caption<\/strong> parameter.<\/p>\n\n\n\n<p>Happy Boting<\/p>\n\n\n\n<p><strong>PS:<\/strong> Did you know in Windows 11 you can \ud83e\ude9f + . to launch the emoji picker?<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing this for future use. 4 steps. But before that&#8230; need SSL certificates to run this thing. If you already have a web server, carry on. Otherwise, fix that problem. Step 1 Step 2 Step 3 Step 4 Set $photoURL beforehand of course with the actual photo&#8217;s URL. In this example $url is not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[17,18,16],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-code","tag-bot","tag-php","tag-telegram"],"_links":{"self":[{"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":5,"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/singaubulit.com\/blog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}