Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

write a python script that getting lats 10 messages from a telegram chat using telebot…

  1. User

    write a python script that getting lats 10 messages from a telegram chat using telebot library

  2. Assistant

    Sure! Below is an example of a Python script using the telebot library to fetch the last 10 messages from a Telegram chat:

    import telebot
    
    # Replace 'YOUR_API_TOKEN' with your actual bot token obtained from BotFather
    bot = telebot.TeleBot('YOUR_API_TOKEN')
    
    
    @bot.message_handler(func=lambda message: True)
    def get_last_10_messages(message):
        chat_id = message.chat.id
    
        # Get the last 10 messages in the chat
        messages = bot.get_chat_history(chat_id, limit=10)
    
        # Print the last 10 messages
        for msg in messages:
            print(f'[{msg.date}] {msg.from_user.username}: {msg.text}\n')
    
    
    # Start listening for messages
    bot.polling()
    

    Make sure to replace 'YOUR_API_TOKEN' with the actual token obtained from BotFather. After running the script, it will listen for new messages in the chat and fetch the last 10 messages whenever a new message is received. The script will then print the date, username of the sender, and the message text for each of the last 10 messages.

    Note: The telebot library requires the pyTelegramBotAPI package to be installed. You can install it using the command pip install pyTelegramBotAPI.

  3. User

    AttributeError: 'TeleBot' object has no attribute 'get_chat_history'

  4. Assistant

    Apologies for the mistake. The telebot library does not have a built-in method called get_chat_history. However, you can achieve the same functionality using the telebot library by utilizing the get_updates method. Here's an updated version of the script:

    import telebot
    
    # Replace 'YOUR_API_TOKEN' with your actual bot token obtained from BotFather
    bot = telebot.TeleBot('YOUR_API_TOKEN')
    
    
    @bot.message_handler(func=lambda message: True)
    def get_last_10_messages(message):
        chat_id = message.chat.id
        message_id = message.message_id
    
        # Get the last 10 message updates for the chat
        updates = bot.get_updates(offset=message_id, limit=10)
    
        # Print the last 10 messages
        for update in updates:
            msg = update.message
            print(f'[{msg.date}] {msg.from_user.username}: {msg.text}\n')
    
    
    # Start listening for messages
    bot.polling()
    

    In this updated script, we are using the get_updates method to get the list of updates since the last processed message. We specify the offset as the ID of the last received message, and the limit as 10 to retrieve the last 10 messages. After receiving the updates, the script will print the date, username of the sender, and the message text for each of the last 10 messages.

    Again, remember to replace 'YOUR_API_TOKEN' with your actual Telegram bot token obtained from BotFather.