Python In the context of a forum post title, the phrase

这很棒。感谢您的信息。关于 Python x Discourse 的信息不多,很高兴能学到,也许这对其他人也有帮助。我刚试了您的代码,它似乎按预期工作。当然,我没有停止玩,所以找到了一种方法来完成第 5 步,以及另一种方法来获取所有内容直到原始帖子内容的粘贴。

我想要的就是这个。

  1. 获取通知
  2. 获取帖子编号
  3. 获取帖子内容
  4. 使用 OpenAI 生成回复
  5. 发布回复
  6. 将同一通知标记为已读?
# 设置一个无限运行机器人的循环
while True:
    # 获取用户操作并获取帖子 ID    
    data = {
    'username': 'theuser',
    # 对于提及和回复,我需要获取过滤器 6 + 7,但不知何故只返回 1 个 JSON 响应。
    'filter': 6 | 7,
    'limit': 10,
    }
    
    unread_mentions = client.user_actions.json.get(data)
    print(unread_mentions)

    user_actions = unread_mentions['user_actions']

    # 循环遍历每个未读提及
    for user_action in unread_mentions['user_actions']:
        topic_id = user_action['topic_id']
        post_number = user_action['post_number']
        post_id = user_action['post_id']
        username = user_action['username']

        print(f'Topic ID: {topic_id}, Post Number: {post_number}, Post ID: {post_id}, Reply to: {username}')
    
        # 获取帖子内容   
        post = client.posts[post_id].json.get()

        # 打印帖子内容
        post_content = post['raw']

        # 生成对帖子的回复
        response = generate_response(post_content)
    
        # 在论坛上发布回复
        content = {
            'topic_id': topic_id,
            'raw': response + ' @' + str(username)
        }
        make_post = client.posts.json.post(content)

目前我遇到了两个问题:

  • 只获取了 1 条提及。我可能忘记了什么,但还没弄清楚原因。
  • 当然,我们希望只回复一个帖子一次,并且由于我们正在循环,我们需要在这里做些什么来防止这种情况。
    • 我们可以将 post_id 存储在 .txt 文件中(不确定它能存储多少数据,我认为您可以处理很多提及)。
    • 我理想的解决方案是标记通知为已读。但我意识到,在我们现有的代码中,我们并没有获取未读提及(嗯,我一开始尝试过,获取未读通知,但我无法提取/存储我需要的 post_id 变量来使用 /posts/get.json 并获取“raw”内容)。

我的最终目标是用 OpenAI API 提供响应。但首先我需要找到一种方法来解决上述问题。