PHP code example of leealex / telegram-sdk

1. Go to this page and download the library: Download leealex/telegram-sdk 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/ */

    

leealex / telegram-sdk example snippets


// Pass your bot's token to the Bot's constructor
$bot = new leealex\telegram\Bot(BOT_TOKEN);
// Optional. Directory path to store DB files at. Default value: sys_get_temp_dir()
$bot->setDb(DB_DIR_PATH);
// Optional. Array of admins IDs
$bot->setAdmins([123456789]);
// Required. Directory path to store all bot's commands 
$bot->setCommandsPath(COMMANDS_DIR_PATH);
// Optional. Aliases are primarily used for reply keyboards, which, unlike inline keyboards,
// cannot pass callback queries. Reply keyboard passes the text of the button itself,
// which may contain emoji.
$bot->setCommandsAliases([
    'Button 1️⃣ 🙂' => 'SomeCommand argument1 argument2',
    'Button 2️⃣ 👍' => 'AnotherCommand argument1',   
]);
$bot->run();

// Get DB
$db = $bot->getDb();
// Fetch data
$user = $db->findBy(['user_id', '=', 123]);
// Fetch data with query builder
$users = $db->createQueryBuilder()
    ->where(['type', '=', 'user'])
    ->orderBy(['age' => 'desc'])
    ->limit(10)
    ->getQuery()
    ->fetch();
// Insert data
$db->insert([
    'type' => 'user', 
    'user_id' => 123, 
    'username' => 'John', 
    'age' => 18
]);
// Update data
$db->createQueryBuilder()
    ->where(['user_id', '=', 123])
    ->getQuery()
    ->update(['age' => 20]);