Skip to content

Commit ab8a9fc

Browse files
authored
Updated readme, updated the main script
Now the readme is more detailed about commands and how the images are fetched. The scripts now handles errors while sending images without crashing the whole system, replies in a more unique way to private messages and the log is appended at the end of the file (previously, it erased everything when it started up - access mode to file is now "a" while it used to be "w+")
1 parent c33f206 commit ab8a9fc

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Corgos Telgram BOT
22
## Free delivery of cute corgis images
3+
`Corgo: just a corgi but man, internet slang is weird` ~ *Oxford dictionary, probably*
34

45
### Background
56
I like corgis. Like, a lot. Sadly I cannot adopt one right now on in the near future, so I made the most reasonable thing a person could do:
@@ -10,36 +11,46 @@ The bot is coded in Python by using the [python-telegram-bot](https://github.com
1011
### Usage
1112
The bot it's pretty straightforward to use. Just start a conversation with \@corgos_bot or navigate to t.me/corgos_bot to start using it.
1213
Usage of the commands:
13-
* */start* will give you a brief description of the bot.
14+
* */start* will give you a brief description of what the bot can do.
1415
* */corgo* will send you a corgi picture.
1516
* */goldencorgo* will tell you the tale of the Golden Corgo.
1617
* */stats* will tell you some (mostly useless) information about the bot.
1718

18-
It will also reply to private messages, although not in a really smart way. I mean, it's a corgi, not a human.
19+
Furthermore, there are 3 more *hidden* commands (as they are not listed):
20+
* */ping* will reply **PONG**, I coded this to make sure that the bot was currently running and any user can use this.
21+
* */stop* will stop the bot. This command can only be used by users whose id is in the *admins* settings in the config file.
22+
* */reset* will reload the script. This command can only be used by users whose id is in the *admins* settings in the config file.
23+
24+
The bot will also reply to private messages, although not in a really smart way. I mean, it's a corgi, not a human.
1925

2026
**Video example:**
2127

22-
![demo - i cannot center this image :()](https://media.giphy.com/media/STxn673gNkyXwQXr1w/giphy.gif)
28+
![demo - i cannot center this image :( )](https://media.giphy.com/media/STxn673gNkyXwQXr1w/giphy.gif)
2329

2430
*(thanks to Reddit user u/GleamTheCube for his unwitting help. What a lovely corgi!)*
2531

2632
### Image sourcing
27-
All the images are sourced from Reddit, namely from */r/corgi* and */r/babycorgis* subreddits. I do not own nor I choose any of this pictures. <br>
28-
In order to be chosen, a post must have a minimum score, fixed in the settings file. I trust each moderator and their ability to remove any unsuitable image. <br>
29-
The subreddits are scraped three times a week, at 2.20 AM (GMT), and every time about 250 photos are loaded (this might not be a fixed value).
33+
All the images are sourced from Reddit, namely from */r/corgi* and */r/babycorgis* subreddits. I do not own nor I choose any of this pictures.
34+
35+
In order to be chosen, a post must have a minimum score, fixed in the settings file. I trust each moderator and their ability to remove any unsuitable image.
36+
37+
The subreddits are scraped three times a week, at 2.20 AM (GMT), and every time 300 (as set in *settings.json* file) *weekly hottest* posts are loaded.
38+
Every post is then analyzed and any non pictures posts or posts with a low score are discarded. Lastly, the list of URL is randomized.
39+
40+
Every time a picture is sent the list is rotated, so it's impossible that the same picture is sent twice (or more!) in a row.
3041

3142
### Next features
3243
As I write this readme, the bot has been going for almost 9 month and about 20000 pictures have been sent.
3344
I feel that, during the multiple iterations of this script (none of those are on GitHub, I'm sorry) I implemented every aspect I liked (and needed).
34-
However, I plan to add one command to get photos of my corgi as soon as I manage to adopt one. Did I already mention that I love corgis?
3545

46+
However, I plan to add one command to get photos of my corgi as soon as I manage to adopt one. Did I already mention that I love corgis?
3647

3748
## Installation
3849
I provided a requirements.txt file in order to automatically install all the needed requirements. <br>
3950
If you want to run this bot yourself on your machine, you have to follow a few steps:
4051
1. Register to Reddit Api
4152
2. Create a Telegram bot using \@botfather on Telegram and set the command list as it's provided in the code (line 145)
42-
3. Create your own Golden Corgo image (yeah, I'm not going to provide it. That's top secret)
53+
3. Create your own Golden Corgo image (yeah, I'm not going to provide it. That's top secret!)
4354
4. Fill the *settings.json* with the data you have gathered so far (Reddit access details, Telegram token)
4455
5. Install all the requirements via the command `pip3 install -r requirements.txt`
4556
6. Finally, run the script with `python3 corgos_bot.py`. The script will log everything in a file (named *logging.log*) in the same folder.

corgos_bot.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,19 @@ def corgo(update, context):
317317
bot_username = t.updater.bot.get_me()["username"]
318318
caption = f"@{bot_username}"
319319

320-
# Not the best way of catching errors
321-
# I might find a better way of checking urls but I fear it might
320+
# not the best way of catching errors
321+
# i might find a better way of checking urls but I fear it might
322322
# be too slow. There's no real way to know if an image is still available
323-
# unless trying to download it. So what this loop does is trying again
324-
# until it works
325-
while True:
326-
try:
327-
url = r.getUrl()
328-
context.bot.send_photo(chat_id=chat_id, photo=url, caption=caption)
329-
break
330-
except:
331-
pass
323+
# unless trying to send it. It shouldn't happen often, but the method I
324+
# used previoulsy managed to crash both the script and the RaspberryPi
325+
try:
326+
url = r.getUrl()
327+
context.bot.send_photo(chat_id=chat_id, photo=url, caption=caption)
328+
except Exception as e:
329+
logging.error("Error while sending photo. Url %s Error %s", url, str(e))
330+
raise Exception(f"Url {url} is not valid")
331+
# at this point, an exception is raised and the error function is
332+
# called. The user gets notified and prompted to try again.
332333

333334
t.updateCorgosSent()
334335
message = "_Press /corgo for another corgo!_"
@@ -424,8 +425,13 @@ def text_message(update, context):
424425

425426
context.bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
426427

428+
# we want to generate some gibberish answer to every message
427429
# the dog noise list was sourced on Wikipedia. Yes, Wikipedia.
428-
message = f"_{random.choice(['ARF', 'WOFF', 'BORK', 'RUFF'])}!_"
430+
bark = random.choice(['ARF ', 'WOFF ', 'BORK ', 'RUFF '])
431+
bark *= random.randint(1, 2) # get some repetition
432+
bark = bark.rstrip() # remove the last space if present
433+
mark = random.choice(['!', '?', '!?', '?!'])
434+
message = f"_{bark}{mark}_"
429435
context.bot.send_message(chat_id=chat_id, text=message,
430436
reply_to_message_id=message_id,
431437
parse_mode=ParseMode.MARKDOWN)
@@ -481,7 +487,7 @@ def error(update, context):
481487
# we log everything into the "corgos_bot.log" file
482488
logging.basicConfig(filename="corgos_bot.log", level=logging.INFO,
483489
format='%(asctime)s %(levelname)s %(message)s',
484-
filemode="w+")
490+
filemode="a")
485491

486492
# Reddit section
487493
r = Reddit()

0 commit comments

Comments
 (0)