Skip to main content

Channel Methods

The channels namespace provides methods for creating and managing Telegram channels and supergroups.

Accessing Channel Methods

// In EventHandler
$this->channels->createChannel([...]);

// In API instance
$MadelineProto->channels->createChannel([...]);

Creating Channels

createChannel

Create a new channel or supergroup.
title
string
required
Channel title
about
string
default:""
Channel description
megagroup
bool
default:"false"
Create as supergroup instead of channel
broadcast
bool
default:"true"
Create as broadcast channel (true) or discussion group (false)
// Create channel
$channel = $this->channels->createChannel([
    'title' => 'My Channel',
    'about' => 'This is my awesome channel',
    'megagroup' => false,
    'broadcast' => true,
]);

// Create supergroup
$supergroup = $this->channels->createChannel([
    'title' => 'My Supergroup',
    'about' => 'Public discussion group',
    'megagroup' => true,
]);

Getting Channel Information

getFullChannel

Get full channel information.
channel
string|int
required
Channel username or ID
$channelInfo = $this->channels->getFullChannel([
    'channel' => '@mychannel',
]);

// Access channel data
$about = $channelInfo['full_chat']['about'];
$participantsCount = $channelInfo['full_chat']['participants_count'];
$onlineCount = $channelInfo['full_chat']['online_count'];

getChannels

Get channel information by IDs.
id
array
required
Array of channel IDs or usernames
$channels = $this->channels->getChannels([
    'id' => ['@channel1', '@channel2'],
]);

Editing Channels

editTitle

Edit channel title.
channel
string|int
required
Channel
title
string
required
New title
$this->channels->editTitle([
    'channel' => $channelId,
    'title' => 'New Channel Title',
]);

editAbout

Edit channel description.
channel
string|int
required
Channel
about
string
required
New description
$this->channels->editAbout([
    'channel' => $channelId,
    'about' => 'Updated channel description',
]);

editPhoto

Edit channel photo.
channel
string|int
required
Channel
photo
array
required
Photo file
$this->channels->editPhoto([
    'channel' => $channelId,
    'photo' => [
        '_' => 'inputChatUploadedPhoto',
        'file' => '/path/to/photo.jpg',
    ],
]);

editUsername

Set or change channel username.
channel
string|int
required
Channel
username
string
required
New username (without @)
$this->channels->editUsername([
    'channel' => $channelId,
    'username' => 'mynewchannel',
]);

Joining and Leaving

joinChannel

Join a channel.
channel
string|int
required
Channel username or ID
$this->channels->joinChannel([
    'channel' => '@publicchannel',
]);

leaveChannel

Leave a channel.
channel
string|int
required
Channel
$this->channels->leaveChannel([
    'channel' => $channelId,
]);

Managing Participants

getParticipants

Get channel participants.
channel
string|int
required
Channel
filter
array
required
Participant filter
offset
int
default:"0"
Offset for pagination
limit
int
default:"200"
Number of participants (max 200)
// Get all participants
$participants = $this->channels->getParticipants([
    'channel' => $channelId,
    'filter' => ['_' => 'channelParticipantsRecent'],
    'offset' => 0,
    'limit' => 200,
]);

// Get administrators
$admins = $this->channels->getParticipants([
    'channel' => $channelId,
    'filter' => ['_' => 'channelParticipantsAdmins'],
    'limit' => 200,
]);

// Get kicked users
$kicked = $this->channels->getParticipants([
    'channel' => $channelId,
    'filter' => ['_' => 'channelParticipantsKicked'],
    'limit' => 200,
]);

// Get bots
$bots = $this->channels->getParticipants([
    'channel' => $channelId,
    'filter' => ['_' => 'channelParticipantsBots'],
    'limit' => 200,
]);

inviteToChannel

Invite users to channel.
channel
string|int
required
Channel
users
array
required
User IDs to invite
$this->channels->inviteToChannel([
    'channel' => $channelId,
    'users' => [$userId1, $userId2],
]);

editBanned

Ban or restrict a user.
channel
string|int
required
Channel
participant
string|int
required
User to ban/restrict
banned_rights
array
required
Rights to restrict
// Ban user permanently
$this->channels->editBanned([
    'channel' => $channelId,
    'participant' => $userId,
    'banned_rights' => [
        '_' => 'chatBannedRights',
        'until_date' => 0, // 0 = forever
        'view_messages' => true,
    ],
]);

// Restrict user (mute)
$this->channels->editBanned([
    'channel' => $channelId,
    'participant' => $userId,
    'banned_rights' => [
        '_' => 'chatBannedRights',
        'until_date' => time() + 3600, // 1 hour
        'send_messages' => true,
    ],
]);

// Unban user
$this->channels->editBanned([
    'channel' => $channelId,
    'participant' => $userId,
    'banned_rights' => [
        '_' => 'chatBannedRights',
        'until_date' => 0,
    ],
]);

editAdmin

Promote or demote admin.
channel
string|int
required
Channel
user_id
string|int
required
User to promote/demote
admin_rights
array
required
Admin rights
rank
string
default:""
Admin title
// Promote to admin
$this->channels->editAdmin([
    'channel' => $channelId,
    'user_id' => $userId,
    'admin_rights' => [
        '_' => 'chatAdminRights',
        'change_info' => true,
        'post_messages' => true,
        'edit_messages' => true,
        'delete_messages' => true,
        'ban_users' => true,
        'invite_users' => true,
        'pin_messages' => true,
        'add_admins' => false,
        'manage_call' => true,
    ],
    'rank' => 'Moderator',
]);

// Demote admin
$this->channels->editAdmin([
    'channel' => $channelId,
    'user_id' => $userId,
    'admin_rights' => [
        '_' => 'chatAdminRights',
    ],
    'rank' => '',
]);

Deletion

deleteChannel

Delete a channel.
channel
string|int
required
Channel to delete
$this->channels->deleteChannel([
    'channel' => $channelId,
]);

Complete Example

use danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Filter\FilterRegex;

class ChannelManager extends EventHandler
{
    #[FilterRegex('/^\/createchannel (.+)/')]
    public function handleCreateChannel(Message $message): void
    {
        $title = $message->matches[1];
        
        // Create channel
        $result = $this->channels->createChannel([
            'title' => $title,
            'about' => 'Created via bot',
            'megagroup' => false,
        ]);
        
        $channelId = $result['chats'][0]['id'];
        $message->reply("Channel created! ID: $channelId");
    }
    
    #[FilterRegex('/^\/channelinfo @?(\w+)/')]
    public function handleChannelInfo(Message $message): void
    {
        $username = $message->matches[1];
        
        try {
            $info = $this->channels->getFullChannel([
                'channel' => '@' . $username,
            ]);
            
            $title = $info['chats'][0]['title'];
            $about = $info['full_chat']['about'];
            $participants = $info['full_chat']['participants_count'];
            $online = $info['full_chat']['online_count'] ?? 0;
            
            $message->reply(
                "**Channel Information**\n\n" .
                "Title: $title\n" .
                "About: $about\n" .
                "Participants: $participants\n" .
                "Online: $online",
                parseMode: 'Markdown'
            );
        } catch (\Exception $e) {
            $message->reply("Error: " . $e->getMessage());
        }
    }
    
    #[FilterRegex('/^\/promote (\d+)/')]
    public function handlePromote(Message $message): void
    {
        if (!$message->chatId) {
            $message->reply("This command must be used in a channel");
            return;
        }
        
        $userId = (int)$message->matches[1];
        
        // Promote to admin
        $this->channels->editAdmin([
            'channel' => $message->chatId,
            'user_id' => $userId,
            'admin_rights' => [
                '_' => 'chatAdminRights',
                'delete_messages' => true,
                'ban_users' => true,
                'invite_users' => true,
                'pin_messages' => true,
            ],
            'rank' => 'Admin',
        ]);
        
        $message->reply("User promoted to admin");
    }
    
    #[FilterRegex('/^\/ban (\d+)/')]
    public function handleBan(Message $message): void
    {
        if (!$message->chatId) {
            $message->reply("This command must be used in a channel");
            return;
        }
        
        $userId = (int)$message->matches[1];
        
        // Ban user
        $this->channels->editBanned([
            'channel' => $message->chatId,
            'participant' => $userId,
            'banned_rights' => [
                '_' => 'chatBannedRights',
                'until_date' => 0,
                'view_messages' => true,
            ],
        ]);
        
        $message->reply("User banned");
    }
}

See Also

Build docs developers (and LLMs) love