Search code examples
phpbotstelegramsendmessageallusersprofile

PHP telegram bot: send message all users from in id.txt


I created a php telegram boat. In this case, I will store the user id in the id.txt file.

$message = $update->message;
$text1 = $message->text;
$fadmin = $message->from->id;
$baza = file_get_contents("id.txt");

$saqla2 = "$baza\n$fadmin";
file_put_contents("id.txt", $saqla2);

Then how do I send a message to every registered user? So you need to send a message to all the minorities.

$text2 = str_replace("/xabar","",$text1);
  $response = file_get_contents("https://api.telegram.org/bot".API_KEY."/sendMessage?chat_id= //there all user id from $baza // &text=$text2");

Solution

  • you can store user_ids in a text file : for example list.txt :

    108926499
    108926497
    108926496
    

    now send a message to all users index.php:

    <?php
    $apiToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    
    $users=file_get_contents('list.txt');
    $users=explode("\n",$users);
    foreach ($users as $user)
    {
        if (empty($user)) continue;
        $data = [
            'chat_id' => $user,
            'text' => 'Hello world!'
        ];
    
        $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );
    }