-2

I have been playing with PRAW to build reddit bots. While it is easy to build a bot that auto responds generic messages to triggered keywords, I want to build something that is a bit more interactive.

I am trying to build a reddit bot that invokes the username of the redditor it is replying to. Eg redditor /u/ironman666 posts "good morning", I want the bot to auto respond "good morning to you too! /u/ironman666". How can I make this work? Thanks!

Sample code: where and how do I invoke the triggering user's name?

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)
iamthewalrus
  • 77
  • 1
  • 7

1 Answers1

0

If you read the docs you'll see that the comment has an author attribute (unless it was deleted), so you should be able to do:

response_text = 'Good morning to you too, {}!'
...
c.reply(response_text.format(c.author))
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • Thank you. That is a lot easier than I thought. Didn't realize PRAW had a method for it. Still was hoping that it would come in format of /u/reddituser, so they get a notification. – iamthewalrus Sep 09 '16 at 21:39
  • Just add /u/ to the format string. But I've always had notifications when my comments were replied to even without mentions – Wayne Werner Sep 10 '16 at 02:17