import logging import requests import urllib.parse from community_qa_bot.settings import ( FORUM_API_KEY, FORUM_API_USERNAME, FORUM_API_BASE_URL ) logger = logging.getLogger(__name__) def reply_to_topic(topic_id: int, message: str) -> bool: api_url = urllib.parse.urljoin(FORUM_API_BASE_URL, f'/posts') try: resp = requests.post( api_url, headers={ 'Api-Key': FORUM_API_KEY, 'Api-Username': FORUM_API_USERNAME, 'Content-Type': 'application/json' }, json={ 'raw': message, 'topic_id': topic_id, }, timeout=5 ) if resp.status_code != 200: logger.error( f'Failed to reply to topic {topic_id}, ' f'status_code: {resp.status_code}, content: {resp.content}' ) return False except Exception as e: logger.error( f'Failed to reply to topic {topic_id}, error: {e}' ) return False return True def get_posts_of_topic(topic_id: int, post_id: int) -> dict: api_url = urllib.parse.urljoin(FORUM_API_BASE_URL, f'/t/{topic_id}/{post_id}.json') try: resp = requests.get( api_url, headers={ 'Api-Key': FORUM_API_KEY, 'Api-Username': FORUM_API_USERNAME, 'Content-Type': 'application/json' }, timeout=5 ) if resp.status_code != 200: logger.error( f'Failed to get posts of topic {topic_id}, ' f'status_code: {resp.status_code}, content: {resp.content}' ) return {} except Exception as e: logger.error( f'Failed to get posts of topic {topic_id}, error: {e}' ) return {} return resp.json()