Search code examples
pythonbotsredditpraw

How to make a reddit bot invoke a username using PRAW


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)

Solution

  • 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))