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

感谢您发布 fluent-discourse 的链接!

我目前正在努力学习 Python,因此可能存在比我在此处概述的更好的方法。

mention = client.notifications.json.get()

这将返回您在创建 client 时设置的 username 的提及的字典。我假设您在创建 client 时将 username 设置为 UserA。

要确认正在返回字典,您可以运行:

print(type(mention))

这应该会返回 <class 'dict'>

您可以通过运行以下命令查看字典的键:

print(list(mention))

这将返回 ['notifications', 'total_rows_notifications', 'seen_notification_id', 'load_more_notifications']

在您的情况下,您需要 notifications 键下的值:

notifications = mention['notifications']

如果您随后运行 type(notifications),您会看到它是一个列表。要获取由提及 UserA 触发的通知,您需要遍历列表以查找 notification_type 设置为 1 的条目。查看 Discourse 通知类型的最简单方法是从您的 Discourse 站点的 rails 控制台运行 Notification.types:

 pry(main)> Notification.types
=> {:mentioned=>1,
 :replied=>2,
 :quoted=>3,
 :edited=>4,
 :liked=>5,
 ...

因此,要获取您为 client 设置的用户的“提及”通知,请尝试执行类似以下操作(我正在键入时在 Python 解释器中进行测试):

data = client.notifications.json.get()
notifications = data['notifications']
# 创建一个空的列表对象来存储“提及”通知
mentions = []
# 遍历 `notifications` 列表以查找 `notification_type` 设置为 `1` 的所有条目
for notification in notifications:
    if notification['notification_type'] == 1:
        mentions.append(notification)

这将创建一个由提及触发的通知列表。列表中的每个项目都将是一个字典。您可以使用类似以下内容进行检查:

for mention in mentions:
    print(mention['topic_id'], mention['post_number'])