我有一个基本的机器人设置,将文本发送回用户,现在我也想向用户发送一条音频消息。但我不能做,这是代码 我也使用这个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);
}
调用函数之外的代码是有效的,但是每次用户输入内容时都会获取文件。我试验这么一点解释会很棒。
答案 0 :(得分:2)
您有一些错误: