As a Python developer, this code is a telegram bot based on Python-telegram-bot version…
- User
As a Python developer, this code is a telegram bot based on Python-telegram-bot version 3.17. Please find fix the issue:
error is:
No error handlers are registered, logging exception. Traceback (most recent call last): File "/usr/local/lib/python3.10/dist-packages/apscheduler/executors/base.py", line 125, in run_job retval = job.func(*job.args, **job.kwargs) File "/home/bot/sandbox/sandbox.py", line 55, in periodic_check_questions messages = chat.get_messages() AttributeError: 'Chat' object has no attribute 'get_messages'The bot code:
import json import time from datetime import datetime, timedelta, timezone from telegram import Update from telegram.ext import Updater, MessageHandler, Filters, CallbackContext TOKEN = "1935475560:AAEAOFvw10Jm_FHbqjnaLP5zuAKhsEFSDrA" SUPPORT_CHANNEL_ID = "-1001818078966" SUPPORT_TEAM_FILE = 'teammates.json' def load_support_teammates(file): with open(file, 'r') as f: teammates = json.load(f) return teammates SUPPORT_TEAM = load_support_teammates(SUPPORT_TEAM_FILE) def is_question(text): question_keywords = ['?', 'how', 'what', 'when', 'where', 'which', 'who', 'why'] return any(keyword in text.lower() for keyword in question_keywords) def handle_messages(update: Update, context: CallbackContext): if update.message.document and update.message.document.file_name.endswith(('.zip.enc', '.zip', '.enc')) or is_question(update.message.text): if update.message.from_user.id not in SUPPORT_TEAM: context.chat_data.setdefault('questions', {}) context.chat_data['questions'][update.message.message_id] = { 'created_at': update.message.date, 'is_resolved': False, } elif update.message.from_user.id in SUPPORT_TEAM: for question_id in context.chat_data.get('questions', {}): context.chat_data['questions'][question_id]['is_resolved'] = True else: pass def periodic_check_questions(context: CallbackContext): now = datetime.now(timezone.utc) for chat_id, chat_data in context.dispatcher.chat_data.items(): if 'questions' not in chat_data: continue for message_id, question_data in chat_data['questions'].items(): if question_data['is_resolved']: continue created_at = question_data['created_at'] waiting_time = now - created_at if waiting_time >= timedelta(minutes=3): # Get the chat object for the specified chat ID chat = context.bot.get_chat(chat_id) messages = chat.get_messages() for message in messages: if message.message_id == message_id: text = f'Group: {chat.title}\nWaiting Time: {waiting_time}\nOriginal Message:\n{message.text}' context.bot.send_message(SUPPORT_CHANNEL_ID, text=text) chat_data['questions'][message_id]['is_resolved'] = True def main(): updater = Updater(token=TOKEN, use_context=True) dp = updater.dispatcher # Initialize chat_data in the context dp.chat_data['questions'] = {} dp.add_handler(MessageHandler(Filters.all, handle_messages)) dp.job_queue.run_repeating(periodic_check_questions, interval=180, first=0) updater.start_polling() updater.idle() if __name__ == "__main__": main() - Assistant
The error occurs because there is no attribute 'get_messages' in the 'telegram.Chat' object. You can't fetch the messages directly using the 'telegram.Chat' object. Instead, you need to reply to the original message when sending the alert to the support channel.
Here's the corrected
periodic_check_questions()function:def periodic_check_questions(context: CallbackContext): now = datetime.now(timezone.utc) for chat_id, chat_data in context.dispatcher.chat_data.items(): if 'questions' not in chat_data: continue for message_id, question_data in chat_data['questions'].items(): if question_data['is_resolved']: continue created_at = question_data['created_at'] waiting_time = now - created_at if waiting_time >= timedelta(minutes=3): # Get the chat object for the specified chat ID chat = context.bot.get_chat(chat_id) # Prepare the text message to be sent to the support channel waiting_time_str = str(waiting_time).split('.')[0] # remove microseconds text = f'Group: {chat.title}\nWaiting Time: {waiting_time_str}\n[Original Message](https://t.me/c/{chat_id[4:]}/{message_id})' context.bot.send_message(SUPPORT_CHANNEL_ID, text=text, parse_mode="Markdown", disable_web_page_preview=True) chat_data['questions'][message_id]['is_resolved'] = True - User
The original message is empty, there is no link or an actual forwarded message from the client group.
- Assistant
In that case, you should modify the
periodic_check_questions()function to forward the original message to the support channel. The changes include calling theforward_message()method instead ofsend_message().Here's the updated
periodic_check_questions()function:def periodic_check_questions(context: CallbackContext): now = datetime.now(timezone.utc) for chat_id, chat_data in context.dispatcher.chat_data.items(): if 'questions' not in chat_data: continue for message_id, question_data in chat_data['questions'].items(): if question_data['is_resolved']: continue created_at = question_data['created_at'] waiting_time = now - created_at if waiting_time >= timedelta(minutes=3): # Forward the original message context.bot.forward_message(SUPPORT_CHANNEL_ID, chat_id, message_id) # Send additional information about the forwarded message chat = context.bot.get_chat(chat_id) waiting_time_str = str(waiting_time).split('.')[0] # remove microseconds text = f'Group: {chat.title}\nWaiting Time: {waiting_time_str}' context.bot.send_message(SUPPORT_CHANNEL_ID, text=text) chat_data['questions'][message_id]['is_resolved'] = True