I’m writing this for future use. 4 steps.
But before that… need SSL certificates to run this thing. If you already have a web server, carry on. Otherwise, fix that problem.
Step 1
- Create new bot with TheBotFather (via Telegram)
- Invoke /newbot
- Give the bot a name
- Give the bot an ID (only _ is the allowed special character)
- When all is done, a message containing the token will be given
Step 2
- Create a directory for the bot (this is your bot’s root)
- Create index.php in root. Everything will be here. If you create separate files, you can <?php include(); ?> or <?php require_once(); ?> like a normal PHP file. For the sake of this post assume everything is in a single file to avoid headaches.
- Now your bot’s URL might be https://your.bot/index.php
Step 3
- Activate Webhook so that you can use Telegram API. Edit the token in the sample URL below and run it in a web browser.
https://api.telegram.org/bot<token_here>/setwebhook?url=https://your.bot/index.php
- Setup the bot in index.php like so:
$auth = "https://api.telegram.org/bot<token_here>";
$chatId = $update["message"]["chat"]["id"];
$msgId = $update["message"]["message_id"];
$sender = $update["message"]["from"]["first_name"];
$username = $update["message"]["from"]["username"];
$senderId = $update["message"]["from"]["id"];
$getmsg = $update["message"]["text"];
$sendmsg = "/sendMessage?parse_mode=HTML&chat_id=";
- $auth = “https://api.telegram.org/bot<token_here>”; // need this to authorize json requests
- $chatId = $update[“message”][“chat”][“id”]; // the active chat window
- $msgId = $update[“message”][“message_id”]; // ID of a specific message within an active chat
- $sender = $update[“message”][“from”][“first_name”]; // first name of a sender
- $username = $update[“message”][“from”][“username”]; // username of a sender (if any) – some users don’t have it
- $senderId = $update[“message”][“from”][“id”]; // ID of a sender (fixed and hidden)
- $getmsg = $update[“message”][“text”]; // the text being sent over
- $sendmsg = “/sendMessage?parse_mode=HTML&chat_id=”; // send message process itself
Step 4
- Create a sample greeting from the bot when a command is invoked using /start (the default command upon first launch of a bot
- You can /create /your /own /commands too
- Be sure to wrap everything you’re doing with…
<?php
...the block
?>
- You can also build a webpage like structure with <title> and all; won’t affect the bot.
- Begin the block with
if (
$getmsg == "/start") {
- Then the workings of this block…
$message = "wassup";
$url = $auth . "/sendMessage?parse_mode=HTML&chat_id=" . $chatId . "&text=" . urlencode($message);
file_get_contents($url);
- $message = “wassup”; // the message
- $url = $auth . “/sendMessage?parse_mode=HTML&chat_id=” . $chatId . “&text=” . urlencode($message); // the sending code; you can put /sendMessage?parse_mode=HTML in a variable too – $message’s content can be added directly in this line too like this: text=<the message> or . urlencode(“<the message>”) .
- file_get_contents($url); // this reads the string (the message and all) via https
- A message appears saying “wassup” 🙂
- Now close this block…
} // yes, this
That is it. Sending a photo is slightly different than sending a text.
file_get_contents("https://api.telegram.org/$ttoken/sendPhoto?chat_id=$chatId&photo=" . urlencode($photoUrl) . "&caption=" . urlencode($caption));
Set $photoURL beforehand of course with the actual photo’s URL. In this example $url is not used, you can also inline everything. The only difference is you can add the caption parameter.
Happy Boting
PS: Did you know in Windows 11 you can 🪟 + . to launch the emoji picker?
Leave a Reply