Skip to content

Instantly share code, notes, and snippets.

@tadeha
Created September 6, 2017 10:51
Show Gist options
  • Save tadeha/584cd62b70ae41e5c11b1541aaac754e to your computer and use it in GitHub Desktop.
Save tadeha/584cd62b70ae41e5c11b1541aaac754e to your computer and use it in GitHub Desktop.
This is a sample Telegram bot written in PHP.
<?php
/*
* Sample Telegram Bot Source Code
*
* @author Tadeh Alexani
* @version 1.00, 2017-09-06
* PHP
*/
/*
* Follow these steps to set-up your bot:
*
* 1- Setup your bot with @BotFather (You can follow the link below) :
* http://telegra.ph/Set-up-your-bot-with-BotFather-09-06
*
* 2- After you received your bot authorization token JUST SAVE IT SOMEWHERE!! :D
*
* 3- Prepare a SSL server for your .PHP file (It should be like https://example.com
*
* 4- Set Webhook for your bot by following this link:
* https://api.telegram.org/bot<token>/setWebhook?url=https://mywebsite.com/path/to/giveawaysbot.php
*
* 5- Happy Coding! You can easilly test this file on your own server :)
* Let me know if you have any question! @tadeh & hi@TadehAlexani.com
*
*/
$apiLink = "https://api.telegram.org/bot[YOUR_API_KEY]/";
$update = file_get_contents("php://input");
$updateArray = json_decode($update, TRUE);
$username = $updateArray["message"]["chat"]["username"];
$userID = $updateArray["message"]["chat"]["id"];
$chatID = $updateArray["message"]["chat"]["id"];
$messageText = $updateArray["message"]["text"];
$keyboard = array(
["ONE"], ["TWO"], ["THREE"]
);
$resp = array("keyboard" => $keyboard, "resize_keyboard" => true, "one_time_keyboard" => true);
$reply = json_encode($resp);
if ($messageText == "/start") {
$welcomeText = "This is a Simple Bot!";
file_get_contents($apiLink . "sendmessage?chat_id=$chatID&text=" . $welcomeText . "&reply_markup=" . $reply);
} else if ($messageText == "ONE") {
$output = "ONE";
file_get_contents($apiLink . "sendmessage?chat_id=$chatID&text=" . $output . "&disable_web_page_preview=true&parse_mode=HTML&reply_markup=" . $reply);
} else if ($messageText == "TWO") {
$output = "TWO";
file_get_contents($apiLink . "sendmessage?chat_id=$chatID&text=" . $output . "&disable_web_page_preview=true&parse_mode=HTML&reply_markup=" . $reply);
} else if ($messageText == "THREE") {
$output = "THREE";
file_get_contents($apiLink . "sendmessage?chat_id=$chatID&text=" . $output . "&reply_markup=" . $reply);
} else {
$errorMsg = "You entered a wrong command!"
file_get_contents($apiLink . "sendmessage?chat_id=$chatID&text=" . $errorMsg);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment