Search code examples
phptelegramtelegram-botphp-telegram-bot

How to send a random photo in TelegramBot using PHP?


I want to send a random photo in TelegramBot, I wrote this code, but it doesn't work. How do I solve this problem?

Code:

$pictures = [
  [
    "file"=>"data/pictures/pic1.jpg",
  ],
  [
    "file"=>"data/pictures/pic2.jpg",
  ]
];

$random_image = $pictures[rand(0, count($pictures) - 1)];
if ($text == "pictest"){
    Bot('SendPhoto',[
        'chat_id' => $chat_id,
        'photo' => $random_image,
    ]);
}

Solution

  • Here, we might be missing:

    • a base domain before images, such as: domain.org/data/pictures/pic1.jpg

    • or the file index in $random_image

      $pictures = [
          [
              "file" => "data/pictures/pic1.jpg",
          ],
          [
              "file" => "data/pictures/pic2.jpg",
          ],
      ];
      
      $random_image = $pictures[rand(0, count($pictures) - 1)]["file"];
      
      if ($text == "pictest"){
          Bot('SendPhoto',[
              'chat_id' => $chat_id,
              'photo' => $random_image,
          ]);
      }