0

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");

1 Answers1

3

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) );
}
Alihossein shahabi
  • 4,034
  • 2
  • 33
  • 53
  • In this case, the message is repeated many times. Can you just take a one trip? `if($text1 == "/send_all" && in_array($fadmin, $admin)){ $baza=explode("\n",$baza); foreach ($baza as $fadmin){ if (empty($fadmin)) continue; $data = [ 'chat_id' => $fadmin, 'text' => 'hello world!' ]; $response = file_get_contents("https://api.telegram.org/bot".API_KEY."/sendMessage?" . http_build_query($data) ); } }` – Mirmuhsin Hamroyev Jun 14 '18 at 08:35
  • I don't understand what you want exactly? please explain more – Alihossein shahabi Jun 14 '18 at 08:40
  • I do not speak English and use Google Translate. Errors are an excuse. If i use the code you write, the "Hello World" will be returned to the user. I only need to send a message once! – Mirmuhsin Hamroyev Jun 14 '18 at 08:45
  • My code, first reads all the users from the file and then sends them a message in the telegram. – Alihossein shahabi Jun 14 '18 at 08:59
  • Do you want to send a message to the user when the user sends a message to ‌BOT? – Alihossein shahabi Jun 14 '18 at 09:12
  • For example, get 4 id in list.txt. I used your code. The bot sends a message to the first id number four times, twice to the id number 3 times, to the third id number twice, to the fourth id once. This is a mistake! Only one message should be sent to all id numbers! – Mirmuhsin Hamroyev Jun 15 '18 at 18:30
  • I once again tested the code. For each user, only a message was sent. Are you sure you copy the codes? – Alihossein shahabi Jun 15 '18 at 19:20
  • I saved the code inside `index.php` and run `php index.php ` . – Alihossein shahabi Jun 15 '18 at 19:26
  • forgive me. Thank you very much. I'm in error. I've just added an id in list.txt several times. – Mirmuhsin Hamroyev Jun 16 '18 at 18:28
  • you're welcome So please accept my answer and close the topic. – Alihossein shahabi Jun 24 '18 at 18:11