/simple-telegram

a simple PHP class for telegram bot API.

Primary LanguagePHPMIT LicenseMIT

simple-telegram

A simple PHP class for telegram bot API. Useful for learning.

Requirements

  • PHP 5 with cURL

Usage

Copy telegram.php into your server and include it in your script.

require("telegram.php");
$tg = new Telegram("BOT_TOKEN");
Getting an array of the current message
$result = $tg->getData();
$chat_id = $result['message']['chat']['id'];
$text = $result['message']['text'];
Sending requests to API as an example of the method sendLocation

All available methods you can see here.

$params = ["chat_id" => $chat_id, "latitude" => "55.7539303", "longitude" => "37.620795"];
$tg->sendRequest("sendLocation", $params);
Sending files
$file = $tg->loadFile("test.jpg");
$params = ["chat_id" => $chat_id, "photo" => $file];
$tg->sendRequest("sendPhoto", $params);
Sending messages with keyboard
$btn1 = ["text" => "Button"];
$btn2 = ["text" => "Again button"];
$btn3 = ["text" => "Wow such button"];

$buttons = [
  [$btn1, $btn2],
  [$btn3]
];

$keyboard = $tg->buildReplyKeyboard($buttons); //supports a second parameter with array

$params = ["chat_id" => $chat_id, "text" => "Hello, it's me", "reply_markup" => $keyboard];
$tg->sendRequest("sendMessage", $params);
Removing keyboard
$rmkb = $tg->removeReplyKeyboard();

$params = ["chat_id" => $chat_id, "text" => "I was wondering if after all these years", "reply_markup" => $rmkb];
$tg->sendRequest("sendMessage", $params);
Sending messages with inline-keyboard
$btn1 = ["text" => "Button", "switch_inline_query" => "hello world"];
$btn2 = ["text" => "Again button", "switch_inline_query_current_chat" => "qwerty"];
$btn3 = ["text" => "Wow such button", "url" => "https://github.com/erorrov/simple-telegram"];

$buttons = [
  [$btn1, $btn2],
  [$btn3]
];

$keyboard = $tg->buildInlineKeyboard($buttons); //supports a second parameter with array

$params = ["chat_id" => $chat_id, "text" => "You'd like to meet", "reply_markup" => $keyboard];
$tg->sendRequest("sendMessage", $params);
Enable debug mode
$tg->debug(CHAT_ID);
Disable debug mode
$tg->debug();
Response to inline query
$lines = [
  ["type" => "contact", "id" => "1", "phone_number" => "+78005553535", "first_name" => "Tim", "last_name" => "Cook"],
  ["type" => "location", "id" => "2", "latitude" => 55.7539303, "longitude" => 37.620795, "title" => "Kremlin and Red Square, Moscow"],
  ["type" => "location", "id" => "3", "latitude" => 37.402473, "longitude" => -122.3212843, "title" => "Silicon Valley"]
];

$inline = $tg->buildInlineQueryResult($lines);
$params = ["inline_query_id" => $result['inline_query']['id'], "results" => $inline];
$res = $tg->sendRequest("answerInlineQuery", $params);

Contacts

You can contact me via Telegram but if you have an issue please open one.