我一直在玩PRAW来制作reddit机器人。虽然很容易构建一个自动响应通用消息到触发关键字的机器人,但我想构建一些更具交互性的东西。
我正在尝试构建一个reddit bot,它会调用它回复的redditor的用户名。例如redditor / u / ironman666发布“早上好”,我希望机器人自动回复“早上好!”/ u / ironman666。我怎样才能做到这一点?谢谢!
示例代码:我在哪里以及如何调用触发用户的名字?
import praw
import time
from praw.helpers import comment_stream
r = praw.Reddit("response agent")
r.login()
target_text = "Good Morning!"
response_text = "Good Morning to you too! #redditor name go here "
processed = []
while True:
for c in comment_stream(r, 'all'):
if target_text == c.body.lower() and c.id not in processed:
print('Wiseau bot activated! :@')
c.reply(response_text)
processed.append(c.id) #then push the response
time.sleep(2)
答案 0 :(得分:0)
如果您read the docs,您会发现评论中包含author
属性(除非已将其删除),您应该可以这样做:
response_text = 'Good morning to you too, {}!'
...
c.reply(response_text.format(c.author))