Telegram 机器人发送文件和文本 PHP

Telegram bot send file and text PHP

我已经设置了一个基本的机器人,可以将文本发送回用户,现在我还想向用户发送一条音频消息。但我做不到,这是代码。
我也在使用这个 https://github.com/Eleirbag89/TelegramBotPHP 发送音频

include("Telegram.php);

    define('BOT_TOKEN', 'tokentoken');
    define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');

   $telegram = new Telegram(BOT_TOKEN);

    $content = file_get_contents("php://input");
    $update = json_decode($content, true);
    $chatID = $update["message"]["chat"]["id"];
    $message = $update["message"]["text"];

    $reply =  sendMessage($message);

    $sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;

    file_get_contents($sendto);

    function sendMessage(&$string) {

    switch ($string) {

    case "Hi":
     $message = "Hi Back";
     sendAudio();
     break;

    case "Bye":
    $message = "Bye Bye";
    break;
    default:
       $message = "Default";

    }
    return $message

    }
func sendAudio() {

$sound = curl_file_create('sampleAudio.mp3', 'audio/mp3');
$newContent = array('chat_id' => $chatID, 'audio' => $sound);
$telegram->sendAudio($newContent);

}

在函数外部调用代码是可行的,但用户每次键入内容时都会获取文件。我正在试验,所以稍微解释一下就更好了。

您有一些错误:

  • 第一行"未关闭
  • 最后一个函数,您输入的是 "func" 而不是 "function"
  • 在 sendAudio() 中您使用了 $chatID 但您必须将其作为 sendAudio() 的参数传递或设置为全局。