I’m trying to create a basic Telegram bot that responds to messages using PHP webhooks. The bot should send a reply when users type specific commands. Here’s my current implementation:
I’m running into several problems. First, when I try using file_get_contents() to call the API, I get HTTP 400 bad request errors. Second, switching to cURL doesn’t seem to help either. The weird thing is that when I copy the exact URL from my debug file and paste it into a browser, it works perfectly and sends the message. My server uses Cloudflare for SSL and I have the webhook properly configured. What could be causing this issue?
I had the same issue with my Telegram bot. The problem was how I handled the chat_id parameter. You’re using $user_id from the ‘from’ field, but you need $update['message']['chat']['id'] instead. Chat ID and user ID aren’t always the same, especially in group chats. Also, GET parameters can mess up special characters. Switch to POST with JSON payload - it’s way more reliable and what Telegram recommends. Add error handling by checking the API response and logging it so you can debug what’s actually happening.
maybe try logging the whole $update var to see if it’s getting the expected data. also, check that your api call structure aligns with telegams api docs; POST is key here, and don’t forget to sanitize user input!
Had this exact problem last month with my first Telegram bot. Your issue is probably URL encoding and how you’re passing parameters in the GET request. The browser works because it handles encoding automatically - your PHP script doesn’t. You’ve got unencoded characters breaking the API call. Use http_build_query() to encode your parameters properly, or just switch to POST with json payload like Telegram wants. Add some error checking too - log the HTTP response code and response body from your curl request. You’ll see exactly what Telegram’s complaining about instead of guessing.