1. Go to this page and download the library: Download httd1/telegramphp 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/ */
httd1 / telegramphp example snippets
TelegramPhp\TelegramPhp;
use \TelegramPhp\Methods;
use \TelegramPhp\Buttons;
// set bot token
\TelegramPhp\Config\Token::setToken ('110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw');
$tlg = new TelegramPhp;
$tlg->command ('/start', function ($bot){
// send message
Methods::sendMessage ([
'chat_id' => $bot->getChatId (),
'text' => 'Hello 👋'
]);
});
// Passing parameters to a command with {{info}}
$tlg->command ('/get {{info}}', function ($bot, $data){
switch ($data ['info']){
case 'id':
$user_info = $bot->getUserId ();
break;
case 'username':
$user_info = $bot->getUsername ();
break;
case 'name':
$user_info = $bot->getFullName ();
break;
default:
$user_info = "Use <code>/get id or username or name</code>";
}
Methods::sendMessage ([
'chat_id' => $bot->getChatId (),
'text' => "User Info: <b>{$user_info}</b>",
'parse_mode' => 'html',
'reply_markup' => Buttons::inlineKeyBoard ([
[Buttons::inlineKeyBoardUrl ("Link My Profile", "tg://user?id=".$bot->getUserId ())],
[Buttons::inlineKeyBoardCallbackData ("Ok, Thanks 👍", "/ok")]
])
]);
});
// match pattern
$tlg->commandMatch ('/^\/ok$/', function ($bot){
Methods::answerCallbackQuery ([
'callback_query_id' => $bot->getCallbackQueryId (),
'text' => '💪 Bro'
]);
});
// commandDefault aways in the end of code!
$tlg->commandDefault (function ($bot){
Methods::sendMessage ([
'chat_id' => $bot->getChatId (),
'text' => 'Chose a command /start, /info with id, name or username'
]);
});
$secret_token = 'wubbalubbadub_dub';
// set secret_token in webhook
// Methods::setWebhook ([
// 'url' => 'https://url.com/mybot/',
// 'secret_token' => $secret_token
// ]);
// my secret token
$tlg->setSecretToken ($secret_token);
if ($tlg->checkSecretToken () == false){
http_response_code (401);
}
$tlg->command ('👍', function ($bot){
// process command...
});
$tlg->command ('/colors {{color_1}} {{color_2}} {{color_3}}', function ($bot, $data){
// $data ['color_1']...
// process command...
});
// run the colors method of ClassBot class
// $tlg->command ('/colors {{color_1}} {{color_2}} {{color_3}}', 'ClassBot:methodColors');
// for namespace use '\MyNamespace\ClassBot:colors'
// $tlg->command ('/colors {{color_1}} {{color_2}} {{color_3}}', '\MyNamespace\ClassBot:colors');
// telegram urls https://t.me/botfather, https://t.me/TelegramBR
$tlg->commandMatch ('/^https?:\/\/t\.me\/\w{5,}$/', function ($bot, $data){
// $data [0]
// process command...
});
// run the executeLinks method of TelegramBot class
// $tlg->commandMatch ('/^https?:\/\/t\.me\/\w{5,}$/', 'TelegramBot:executeLinks');
// for namespace use '\MyNamespace\ClassBot:colors'
// $tlg->commandMatch ('/^https?:\/\/t\.me\/\w{5,}$/', '\MyNamespace\TelegramBot:executeLinks');
// ...command
// ...commandMatch
// in the end of code!
$tlg->commandDefault (function ($bot){
// send default message
});
// $tlg->commandDefault ('ControllerBot:default');