PHP code example of ustadev / telegram
1. Go to this page and download the library: Download ustadev/telegram library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ustadev / telegram example snippets
$bot = new BotApi("$token");
$update = new Update($bot->getWebhookUpdate());
if ($update->isMessage()) {
$message = $update->getMessage();
$text = $message->isText() ? $message->getText() : '';
$user = $message->getUser();
$user_id = $user->getId();
if ($text == '/start') {
$bot->sendMessage($user_id, "Welcome");
}
if ($text == '/video') {
$bot->sendVideo($user_id, "vide url");
}
if ($text == '/doc') {
$bot->sendDocument($user_id, "doc url");
}
if ($text == '/photo') {
$bot->sendPhoto($user_id, "photo url");
}
if ($update->getMessage()->isAudio()) {
print_r($update->getMessage()->getAudio());
}
}
$keyboard = new Keyboard();
$keyboard->addCallBack('CallBack','callBackData');
$keyboard->addUrl('Url link','https://t.me/jamshidbekakhlidinov');
$keyboard->addRow();
$keyboard->addSwtichInlineQueryCurrentChat('Query chat','query choosen chat');
$keyboard->addSwtichInlineQuery('Query','query text');
$keyboard->addRow();
$keyboard->addWebApp('web app', 'https://ustadev.uz');
$keyboard->addRequestPoll('poll button');
if ($text == '/button') {
$bot->sendMessage(
$user_id,
"<b>Buttons</b>",
[
'reply_markup' => $keyboard->init(),
'parse_mode' => 'html',
]
);
}
$bot = new BotApi("$token");
$update = new Update($bot->getWebhookUpdate());
if ($update->isCallbackQuery()) {
$callback = $update->getCallbackQuery();
$user = $callback->getUser();
$user_id = $user->getId();
$data = $callback->getData();
$keyboard = new Keyboard();
$keyboard->addCallbackDataButton("Button title", "data_name");
if ($data == 'data_name') {
$bot->sendMessage(
$user_id,
"<b>Your text</b>",
[
'reply_markup' => $keyboard->init(),
'parse_mode' => 'html',
]
);
}
}
$bot = new BotApi("$token");
$update = new Update($bot->getWebhookUpdate());
if ($update->isInlineQuery()) {
$inlineQuery = $update->getInlineQuery();
$query = $inlineQuery->getQuery();
$query_id = $inlineQuery->getId();
$keyboard = new Keyboard();
$keyboard->addCallbackDataButton("Your CallBack button", "data_name");
$photoUrl = "https://storage.kun.uz/source/thumbnails/_medium/10/egAyZ4_AtLF8OGa4bxD4hpZTI6vHFaqj_medium.jpg";
if($query == 'photo'){
$response = new InlineQueryResult();
$response->addPhoto(
"$photoUrl",
"Rasmni yuborish",
[
'description' => 'bu rasm chatga yuboriladi',
'caption' => $query,
]
);
$bot->answerInlineQuery(
$query_id,
$response->init(),
);
}
if($query == 'text'){
$response = new InlineQueryResult();
$response->addText(
"Xabarni yuborish",
"$query",
[
'description' => 'bu xabar chtga yuboriladi'
]
);
$bot->answerInlineQuery(
$query_id,
$response->init(),
);
}
if($query == 'document'){
$response = new InlineQueryResult();
$response->addDocument(
"$photoUrl",
"Yuborish doc",
'application/pdf',
[
'caption' => $query
]
);
$bot->answerInlineQuery(
$query_id,
$response->init(),
);
}
}
if ($update->isChannelPost()) {
$post = $update->getChannelPost();
$text = $post->isText() ? $post->getText() : '';
$chat = $post->getSenderChat();
$chat_id = $chat->getId();
$title = $chat->getTitle();
if ($text == "#help") {
$bot->sendMessage(
$chat_id,
"salom <b>{$title}</b>",
);
$bot->deleteMessage(
$chat_id,
$post->getMessageId()
);
}
}
if ($update->isEditedChannelPost()) {
$post = $update->getEditedChannelPost();
$text = $post->isText() ? $post->getText() : '';
$chat = $post->getSenderChat();
$chat_id = $chat->getId();
$title = $chat->getTitle();
if ($text == "#help123") {
$bot->sendMessage(
$chat_id,
"salom <b>{$title}</b>",
);
$bot->deleteMessage(
$chat_id,
$post->getMessageId()
);
}
}
use ustadev\telegram\proxy\Proxy;
$token = 'bot token';
$url = 'http://localhost:1212';
$proxy = new Proxy($url,$token);
$proxy->loop();