Creating a telegram bot for multiple users with Telegram.Bot library

I’m new to making telegram bots and I’m stuck. I made a basic bot that saves messages to a database. But it only works for one person at a time. When more people use it, all the answers get mixed up in one database row. How can I fix my code so the bot handles multiple users correctly?

Here’s a simplified version of what I tried:

public static async Task HandleMessage(ITelegramBotClient bot, Message msg)
{
    var userId = msg.From.Id;
    var text = msg.Text;

    using (var db = new MyDatabase())
    {
        if (text == "/start")
        {
            db.AddNewUser(userId);
        }
        else
        {
            db.SaveUserMessage(userId, text);
        }
    }

    await bot.SendTextMessageAsync(msg.Chat.Id, "Got your message!");
}

I tried using the user ID but it’s still not working right. Any ideas on how to make this work for lots of people at once? Thanks for any help!

I’ve dealt with a similar issue when building my first Telegram bot. The key is to properly structure your database to handle multiple users. Instead of using a single row, create a table with columns for user_id, message, and timestamp.

Here’s a rough idea of how I modified my code:

public static async Task HandleMessage(ITelegramBotClient bot, Message msg)
{
    var userId = msg.From.Id;
    var text = msg.Text;

    using (var db = new MyDatabase())
    {
        if (text == "/start")
        {
            db.AddNewUser(userId);
        }
        else
        {
            db.SaveUserMessage(userId, text, DateTime.Now);
        }
    }

    await bot.SendTextMessageAsync(msg.Chat.Id, "Message saved successfully!");
}

This approach ensures each user’s messages are stored separately. You might also want to implement some kind of session management to keep track of the conversation state for each user. Hope this helps you get on the right track!

hey there! i had similar probs. instead of a single variable, try a dictionary using each user’s id as key to manage individual states. that way, state is kept separate. hope it helps!

Your approach is on the right track, but you need to refine your database structure. Consider creating separate tables for users and messages. The users table should have columns for user_id and other relevant info, while the messages table should include user_id, message_text, and timestamp.

Here’s a suggestion for your SaveUserMessage method:

public void SaveUserMessage(long userId, string text)
{
    using (var command = new SqlCommand("INSERT INTO Messages (UserId, MessageText, Timestamp) VALUES (@UserId, @MessageText, @Timestamp)", connection))
    {
        command.Parameters.AddWithValue("@UserId", userId);
        command.Parameters.AddWithValue("@MessageText", text);
        command.Parameters.AddWithValue("@Timestamp", DateTime.UtcNow);
        command.ExecuteNonQuery();
    }
}

This structure allows for efficient querying and scalability as your user base grows. Remember to implement proper error handling and consider using an ORM like Entity Framework for more complex database operations.