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
$botApi = new BotApi("$token");
$update = new Update($botApi->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, "https://t.me/hukumdor_usmon_uzbek_kinolar_u/12204");
}
if ($text == '/doc') {
$bot->sendDocument($user_id, "https://t.me/Nod32bazaa/3733");
}
if ($text == '/photo') {
$bot->sendPhoto($user_id, "https://t.me/Nod32bazaa/3735");
}
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',
]
);
}
$botApi = new BotApi("$token");
$update = new Update($botApi->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') {
$botApi->sendMessage(
$user_id,
"<b>Your text</b>",
[
'reply_markup' => $keyboard->init(),
'parse_mode' => 'html',
]
);
}
}
$botApi = new BotApi("$token");
$update = new Update($botApi->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,
]
);
$botApi->answerInlineQuery(
$query_id,
$response->init(),
);
}
if($query == 'text'){
$response = new InlineQueryResult();
$response->addText(
"Xabarni yuborish",
"$query",
[
'description' => 'bu xabar chtga yuboriladi'
]
);
$botApi->answerInlineQuery(
$query_id,
$response->init(),
);
}
if($query == 'document'){
$response = new InlineQueryResult();
$response->addDocument(
"$photoUrl",
"Yuborish doc",
'application/pdf',
[
'caption' => $query
]
);
$botApi->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") {
$this->botApi->sendMessage(
$chat_id,
"salom <b>{$title}</b>",
);
$this->botApi->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") {
$this->botApi->sendMessage(
$chat_id,
"salom <b>{$title}</b>",
);
$this->botApi->deleteMessage(
$chat_id,
$post->getMessageId()
);
}
}
use ustadev\telegram\proxy\Proxy;
$token = 'bot token';
$url = 'http://localhost:1212';
$proxy = new Proxy($token, $url);
$proxy->loop();