Meraj
Meraj

Reputation: 35

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,
    ]);
}

Upvotes: 1

Views: 452

Answers (1)

Emma
Emma

Reputation: 27743

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,
        ]);
    }
    

Upvotes: 1

Related Questions