0

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.

4
  • You got a syntax error, top of your script Commented Apr 22, 2016 at 9:15
  • Just rewrote some of it here manually the code is running fine, it just doesn't send the audio file, while the text gets sent fine. Commented Apr 22, 2016 at 9:18
  • Tried it now, still same result. Commented Apr 22, 2016 at 10:25
  • You should check for errors occuring in your bot or in the post response of telegram. Commented Apr 22, 2016 at 12:58

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

After checking again i found the issue, as for the syntax i made mistakes while posting the question. The last point was the issue. Thank you for taking your time to answer since it was a trivial mistake and yet none did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.