import praw def get_reddit_instance(): reddit_config = {} with open("reddit_config") as tmp_file: for line in tmp_file: (key, val) = line.split(';') reddit_config[key] = val.replace('\n', '') return praw.Reddit( user_agent=reddit_config['user_agent'], client_id=reddit_config['client_id'], client_secret=reddit_config['client_secret'] ) def wrong_dog(bot, update): reddit = get_reddit_instance() sub = reddit.subreddit('whatswrongwithyourdog') post = sub.random() update.message.reply_text("This dog has serious issues: %s" % post.url)