I’m trying to build a simple C application that sends notifications through Telegram’s bot API using libcurl, but my messages aren’t getting delivered at all. Here’s my current implementation:
The bot has proper administrator permissions in my channel and I’ve triple checked that the chat ID is absolutely correct. No compilation errors appear and the program runs without crashing, but no messages show up in the channel. What could be preventing the message delivery?
I’ve dealt with Telegram bots for years and hit this exact problem before. You’re not checking what Telegram’s API sends back - that’s your main issue. Even when your request goes through, Telegram might return error codes you’re completely missing. Set up a write callback to grab the response body. You’ll see exactly what’s wrong - auth errors, bad requests, whatever. Also don’t forget curl_global_init(CURL_GLOBAL_DEFAULT) before any curl calls and curl_global_cleanup() when you’re done. Skip the initialization and libcurl gets weird depending on your system.
dude you’re also printing the result wrong at the end - printf("%s", result) won’t work since result is a CURLcode not a string. plus you need to set the content-type header for POST requests or telegram might reject it. try adding curl_easy_setopt(http_handle, CURLOPT_HTTPHEADER, headers) with “Content-Type: application/x-www-form-urlencoded”
Your code has a memory allocation bug. You declared api_url as a pointer but never allocated memory for it. When you call sprintf(api_url, ...), you’re writing to random memory - that’s undefined behavior and probably why your requests fail.
Fix it by allocating proper memory: use char api_url[256]; or malloc().
Also, turn on verbose output with curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L); to see what’s actually happening. Right now you’re not checking Telegram’s API response, so you don’t even know if your requests are reaching their servers.