r/csshelp•
anton_bot

Setting up reddit flair bot

This is mainly directed at u/gavin19 but anyone else that can help is welcome with open arms.

In order to use the reddit flair bot, I have to first set it up, and I have no idea how.

This is the bot account I'm going to be using it with.

To my understanding (which is very limited), I need to get OAuth2 to work with PRAW, and to get PRAW to work I need to install it via python.

What I've done so far is created a script type app on reddit and setting the redirect uri to http://127.0.0.1:65010/authorize_callback but I have no idea where to go from here.

I then edited the oauth.ini file to contain the lines listed in this section in the praw-OAuth2Util-master folder as well as the oauth.txt. listed here in the reddit-flair-bot-master folder (I have no idea if I actually need both folders).

As for the server mode, I've got it disabled because I plan on running the bot locally as I don't have access to a remote server (I believe this means the bot will only function as long as my PC is on?).

I've got python 3.5 installed and also installed PRAW and OAuth2 via pip.


Now the next part is where I'm stuck.

When I try to use the code in python terminal:

import praw
import OAuth2Util

r = praw.Reddit("Useragent")
o = OAuth2Util.OAuth2Util(r)

it works all the way up to the last line where I get an error.

For the time being, this is where I get stuck, but also looking ahead I have no clue what I would be doing or how to actually setup the bot if I had it working.

Posted Oct 20, 2015, 6:03 AM UTC · edited

030
gavin19•

I just updated the script since the OAuth2Util was just updated a few days back and didn't notice, and they switched to an ini instead of a text file. Make sure you grab the updated files (just download them and replace all your old files with them).

Inside your script folder you should have

.flair_bot.py
.flair_list.py
oauth.ini
OAuth2Util.py

The ini should look something like

[app]
scope=identity,account,modconfig,modflair,privatemessages
refreshable=True
app_key=XGApcJUZ3nRRuD
app_secret=NQPVQR35-d_Ff2neK4NcSdjDRvv

[server]
server_mode=False
url=127.0.0.1
port=65010
redirect_path=authorize_callback
link_path=oauth

# Will be filled automatically
[token]
token=None
refresh_token=None
valid_until=0

with your own app key/secret in place of those random ones.

When you run

python flair_bot.py

in that folder for the first time, your default browser should open with an authorisation page for you to allow/decline. Make sure to have your default browser open and logged into the account you want to authorise.

Once that's done, the script will run from now on without asking for any authorisation.

Note, this

r = praw.Reddit("Useragent")

in flair_bot.py, should be changed to something more appropriate, e.g

r = praw.Reddit("Script for [r/sub](/r/sub) by [u/someone](/u/someone)")
1
antonlabz•

Owner of the bot - thank you, it worked!

I've got a couple more questions:

  1. Do I have to do anything about the code to refresh the token? What is the purpose of refreshing it?
  2. My goal is for the bot to run 24/7 but if I'm running it locally then it can only run when my PC is on?
  3. After running python flair_bot.py, did that start the bot?
  4. What do I do from here?

In the flair_list.py file I've only set 1 flair:

flairs = {
    'goku': ''
}

to test if it works first.

In your readme it mentions that the subject of the prefilled PM can be changed in the script. Does this mean I generate the PM's with a script or create them myself?

1
gavin19•
  1. No, you don't need to worry about that from now on.

  2. If your PC isn't on then the script can't run. This is the main reason that people will pay for a cheap VPS to run scripts remotely. You can use something very basic (roughly $15 a year).

  3. The bot doesn't run continuously. It runs once and then ends. You can use the Task Scheduler (Windows key + R, type in taskschd.msc, then Enter). Create a task that runs the script every X minutes. The main reason for this is that there is no logic to account for reddit being down/script crashing. If it's left to run continuously then it might crash and you may not notice for some time. By running it in intervals, it (mostly) ensures that a failed run won't then upset the next one.

  4. The PMs are hardcoded into your subreddit/wiki page. For example, the links here are just a bunch of PMs autofilled to point to the bot account with the class name included in the message field.

If you take the first PM - https://www.reddit.com/message/compose?to=RFootballBot&subject=crest&message=eng-accr - then the script reads that PM when sent by the user.

The bot reads the subject as crest and the message as eng-accr. The subject is only checked to weed out regular PMs directed at the bot as opposed to those for flairs.

It then checks the flair_list.py for any one that matches eng-accr. In this case, I have

flairs = {
    'example1': 'Example Team 1',
    'eng-accr': 'Accrington Stanley',
    'example2': 'Example Team 2'
}

If found, it applies the text to the flair that correspnds to that class name, so 'Accrington Stanley'.

1
antonlabz•

I just tested it with one flair on my test sub and it worked perfectly!

  1. Now I'm assuming I have to manually type out the other 350+ pre-filled PM's? There isn't a script for it anywhere that will generate these PM's based on the flair_bot.py and flair_list.py file (reads subject and flair class)?
  2. Can you recommend a cheap VPS that I can use from Australia?
  3. The method you proposed for creating a task scheduler to run the script every X minutes I assume is only if you're running the script locally. How would I go about setting it up on a VPS?
  4. As you may have noticed in my previous comment, I left the flair text blank in the template 'goku': ''. How do I set it up so that users can assign their own flair text? Similar to what r/pokemon has for their current flair bot.
1
gavin19•
  1. Yep. If you reply with the class names (or PM me), then I'll send you back all the links with the names injected. If you also want to send a list of the corresponding text that accompanies each class then I can add those too to save typing them all out.

  2. I use Digital Ocean in the UK (cheapest is $5 a month though), but one of the Ramnode ones would suffice.

  3. Assuming a Linux-based VPS, like Ubuntu, you'd need to use cron to set up the equivalent.

  4. The bot doesn't have any logic for allowing custom text at the point of application so custom text isn't a thing. The text will always be hardcoded into the script. That said, it could be modified so that users could be instructed how to add custom text by editing the script.

1
Emitrist•

This is something I've been looking into for a while now too, and cannot seem to find a way to get it working. If you seem to, please let me know!

0
anton_botOP•

Keep an eye on this thread, I'm certain u/gavin19 or another user will eventually help out.

1
anton_botOP•

He replied and I got it working!

Have a read through it.

1