Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/config.php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ $bot_api_key = 'your_key';
$bot_username = 'OnlineBot';
$doWithoutCron = true;
$base_url = '';
$admins_id = [
0123456789,
];

$mysql_credentials = [
'host' => 'localhost',
Expand Down
57 changes: 57 additions & 0 deletions src/Commands/BroadcastCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;

class BroadcastCommand extends UserCommand
{
protected $name = 'broadcast';
protected $description = 'Broadcast message to all users';
protected $usage = '/broadcast <message>';
protected $version = '1.0.0';

public function execute(): ServerResponse
{
require __DIR__ . '/../../config/config.php';

$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$user_id = $message->getFrom()->getId();
$text = trim($message->getText(true));

if (!isset($admins_id) || !in_array($user_id, $admins_id)) {
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => '*Error:* You do not have permission to execute this command.',
'parse_mode' => 'Markdown'
]);
}

if ($text === '') {
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => '*Error*: Message body is empty. Usage: `/broadcast <message>`',
'parse_mode' => 'Markdown'
]);
}

$results = DB::getPdo()->query('SELECT id FROM user')->fetchAll(\PDO::FETCH_COLUMN);

foreach ($results as $target_id) {
Request::sendMessage([
'chat_id' => $target_id,
'text' => "*Important message*\n\n" . $text,
'parse_mode' => 'Markdown'
]);
}

return Request::sendMessage([
'chat_id' => $chat_id,
'text' => 'Broadcast sent to ' . count($results) . ' users.'
]);
}
}
2 changes: 1 addition & 1 deletion src/Commands/UnregisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function execute() : ServerResponse
{
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => "*error:* register host '$uid' failed",
'text' => "*error:* unregister host '$uid' failed",
'parse_mode' => 'Markdown'
]);
}
Expand Down
Loading