PHP code example of dasturchiuz / telegram-bot
1. Go to this page and download the library: Download dasturchiuz/telegram-bot 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' );
dasturchiuz / telegram-bot example snippets
:bot_api_key';
$bot_username = ' username_bot';
$hook_url = ' https:
try {
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
$result = $telegram->setWebhook($hook_url);
if ($result->isOk()) {
echo $result->getDescription();
}
} catch (Longman\TelegramBot\Exception \TelegramException $e) {
}
:bot_api_key';
$bot_username = ' username_bot';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
// Handle telegram webhook request
$telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Silence is golden!
// log telegram errors
// echo $e->getMessage();
}
$result = $telegram->setWebhook($hook_url, ['certificate' => '/path/to/certificate' ]);
ot_api_key';
$bot_username = ' username_bot';
$mysql_credentials = [
' host' => ' localhost',
' port' => 3306, // optional
' user' => ' dbuser',
' password' => ' dbpass',
' database' => ' dbname',
];
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
// Enable MySQL
$telegram->enableMySql($mysql_credentials);
// Handle telegram getUpdates request
$telegram->handleGetUpdates();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// log telegram errors
// echo $e->getMessage();
}
$telegram->useGetUpdatesWithoutDatabase();
$result = Request::sendMessage([
'chat_id' => $chat_id,
'text' => 'Your utf8 text 😜 ...' ,
]);
$result = Request::sendPhoto([
'chat_id' => $chat_id,
'photo' => Request::encodeFile('/path/to/pic.jpg' ),
]);
$result = Request::sendPhoto([
'chat_id' => $chat_id,
'photo' => 'AAQCCBNtIhAoAAss4tLEZ3x6HzqVAAqC' ,
]);
$result = Request::sendPhoto([
'chat_id' => $chat_id,
'photo' => 'https://example.com/path/to/pic.jpg' ,
]);
Request::sendChatAction([
'chat_id' => $chat_id,
'action' => Longman\TelegramBot\ChatAction::TYPING,
]);
$results = Request::sendToActiveChats(
'sendMessage' ,
['text' => 'Hey! Check out the new features!!' ],
[
'groups' => true ,
'supergroups' => true ,
'channels' => false ,
'users' => true ,
]
);
$telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason = 'Update denied by update_filter' ) {
$user_id = $update->getMessage()->getFrom()->getId();
if ($user_id === 428 ) {
return true ;
}
$reason = "Invalid user with ID {$user_id}" ;
return false ;
});
$mysql_credentials = [
'host' => 'localhost' ,
'port' => 3306 ,
'user' => 'dbuser' ,
'password' => 'dbpass' ,
'database' => 'dbname' ,
];
$telegram->enableMySql($mysql_credentials);
$telegram->enableMySql($mysql_credentials, $bot_username . '_' );
$telegram->enableExternalMySql($external_pdo_connection)
$telegram->setCommandConfig('date' , [
'google_api_key' => 'your_google_api_key_here' ,
]);
$telegram->setCommandConfig('weather' , [
'owm_api_key' => 'your_owm_api_key_here' ,
]);
$telegram->enableAdmin(your_telegram_user_id);
$telegram->enableAdmins([
your_telegram_user_id,
other_telegram_user_id,
]);
$telegram->setCommandConfig('sendtochannel' , [
'your_channel' => [
'@type_here_your_channel' ,
]
]);
$telegram->setCommandConfig('sendtochannel' , [
'your_channel' => [
'@type_here_your_channel' ,
'@type_here_another_channel' ,
'@and_so_on' ,
]
]);
$telegram->setDownloadPath('/your/path/Download' );
$telegram->setUploadPath('/your/path/Upload' );
bash
$ chmod +x getUpdatesCLI.php
bash
$ ./getUpdatesCLI.php