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:
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?
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.
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:
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.
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.