Need help with Telegram bot: sending images and tracking user stats

Hey everyone! I’m trying to build a Telegram bot and I’ve got some basic code working. But now I’m stuck on two things:

  1. How do I make my bot send pictures? I want it to grab an image from a URL and send it as a reply. Anyone know how to do this in PHP?

  2. I also want to keep track of how many users are interacting with my bot. Is there a way to save their IDs in a database? I’m thinking of adding a /stats command that shows the total number of users. Maybe it could add +1 to the count whenever someone uses /start.

Here’s a simple version of what I’ve got so far:

$input = json_decode(file_get_contents('php://input'));

function handleMessage($message) {
    $reply = '';
    if ($message == 'Hello') {
        $reply = 'Hi there!';
    } elseif ($message == 'Who are you?') {
        $reply = 'I'm ChatBot';
    } elseif ($message == 'How's it going?') {
        $reply = 'Pretty good, thanks for asking!';
    }
    return $reply;
}

$botToken = 'your_token_here';
$chatId = $input->message->chat->id;
$response = handleMessage($input->message->text);

$apiUrl = "https://api.telegram.org/bot{$botToken}/sendMessage?chat_id={$chatId}&text={$response}";
file_get_contents($apiUrl);

Any help would be awesome. Thanks!

hey jack, for sending pics try using sendPhoto method instead of sendMessage. pass the image URL as photo parameter. for user tracking, u could use a simple MySQL db to store user IDs. increment count on /start command. good luck with ur bot!

As someone who’s built a few Telegram bots, I can share some insights. For sending images, you’ll want to use cURL instead of file_get_contents(). It’s more reliable and gives you more control. Here’s a quick snippet:

$ch = curl_init("https://api.telegram.org/bot{$botToken}/sendPhoto");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'chat_id' => $chatId,
    'photo' => 'https://example.com/your-image.jpg'
]);
curl_exec($ch);
curl_close($ch);

For user tracking, I’d recommend using SQLite. It’s lightweight and doesn’t require a separate server. You can create a simple table to store user IDs and interaction counts. Then, in your handleMessage function, you can update the stats whenever a user interacts with the bot.

Remember to handle errors gracefully - network issues, API limits, etc. It’ll make your bot much more robust in the long run.

Regarding image sending, you’ll want to use the sendPhoto method in the Telegram Bot API. Here’s a quick example:

$photoUrl = ‘https://example.com/image.jpg’;
$apiUrl = “https://api.telegram.org/bot{$botToken}/sendPhoto?chat_id={$chatId}&photo={$photoUrl}”;
file_get_contents($apiUrl);

For user tracking, consider using a SQL database. Create a table to store user IDs and interaction counts. On each /start command, you can either insert a new record or update an existing one:

$sql = “INSERT INTO users (user_id, interaction_count) VALUES (?, 1) ON DUPLICATE KEY UPDATE interaction_count = interaction_count + 1”;

Implement the /stats command to query and return the total user count. This approach should give you a solid foundation for expanding your bot’s functionality.