import praw import requests def get_post_type(url): header = requests.head(url).headers if 'Content-Type' in header: return header['Content-Type'] else: return '' 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_instance = get_reddit_instance() sub = reddit_instance.subreddit('whatswrongwithyourdog') post = sub.random() update.message.reply_text("This dog has serious issues: %s" % post.url) def hamster(bot, update): reddit_instance = get_reddit_instance() sub = reddit_instance.subreddit('hamsters') post = sub.random() while "image" not in get_post_type(post.url): post = sub.random() bot.send_photo(chat_id=update.message.chat_id, photo=post.url) def reddit(bot, update, args): if not args: update.message.reply_text("Geile, sag sub") return sub = args[0] reddit_instance = get_reddit_instance() post = reddit_instance.subreddit(sub).random() update.message.reply_text(post.url)