【问题标题】:How to send a random photo in TelegramBot using PHP?如何使用 PHP 在 TelegramBot 中发送随机照片?
【发布时间】:2020-02-22 15:48:11
【问题描述】:

我想在 TelegramBot 中发送一张随机照片,我写了这段代码,但它不起作用。我该如何解决这个问题?

代码:

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读asking questions。 $pictures 是一个数组数组,但 $random_image 是从数组中选择一个项目,所以它获取的是一个数组而不是与文件关联的 url。以后请避免说“它不起作用”,而是说什么不起作用,提供错误消息以及实际结果和期望的结果。

标签: php telegram telegram-bot php-telegram-bot


【解决方案1】:

在这里,我们可能会丢失:

  • 图像前的基域,如:domain.org/data/pictures/pic1.jpg

  • $random_image 中的file 索引

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多