Php – Telegram bot send file and text PHP

PHPtelegram-bot

I've got a basic bot set up that will send text back to the user, now I also want to send the user an audio message. But that I cannot do, here is the code.
I'm also using this https://github.com/Eleirbag89/TelegramBotPHP to send the audio

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);

}

Calling the code outside of the functions works, but than the user gets the file each time they type something. I experimenting so a bit of explanation would be great.

Best Answer

You have some errors:

  • First line " is unclosed
  • Last function, you've typed "func" instead of "function"
  • In sendAudio() you've used $chatID but you've to pass it as a parameter of sendAudio() or to set is as global.
Related Topic