This bot script can select a random top of the day post from reddit and send it to a telegram channel via a bot, while saving the url of post into a file, so as to not post the same image twice.
praw
python-dotenv
Returns an array of top posts based on time and limit entered (from PRAW)
get_top_posts(time, limit)
Parameters:
time
: Can be one of: all, day, hour, month, week, year (default: day)limit
: Number of posts to fetch (default: 10)
Returns a single post. It first calls get_top_posts
, then calls get_list_of_posted_items
and chooses a random element from difference of the two.
select_a_post(time, limit)
Parameters:
Same as above.
Sends the given post to the channel.
get_top_posts(post, caption)
Parameters:
post
: URL of the image.caption
: Caption for thepost
(Optional)
- Git clone this repo
- Rename .env.example to .env
- Replace all data in .env with your own
- Create a virtual environment (optional)
- Run
pip install -r requirements.txt
- Create a new file named
bot.py
with the following contents:
from reddit_to_telegram import Reddit_to_telegram
x = Reddit_To_Telegram(user_agent, subreddit, channel_name)
selected_post = x.select_a_post()
x.post_to_telegram(selected_post)
-
Enclose into a function for easier callingConverted to class - Generalize this for any subreddit
- Allow calling multiple subreddits
- Generate a dockerfile (update: testing this.)