Giter Club home page Giter Club logo

xkcdbot's Introduction

XKCD Bot

A bot for /r/xkcd that posts detailed comments about xkcd comics

About The Project

After seeing this suggestion on the /r/xkcd subreddit, I began working on this bot. It is intended to be a non-intrusive bot that provides a detailed and well formatted comment with information regarding the relevant comic.

It is currently running 24/7 in the cloud. You can find the bot at /u/BobbyTablesBot.

Example

There are two ways to use the bot.

  • The first way is to post a comment in /r/xkcd with the number of the xkcd comic you would like to link prefixed by an exclamation mark (!) or a pound sign (#).
    Examples:

    !327

    or

    #327

    or even

    Show me !327

  • The second way is to mention the bot from any subreddit with the number of the xkcd comic you would like to link. If the bot is mentioned, the comic number does not need to be prefixed.
    Examples:

    /u/BobbyTablesBot 327

    or

    /u/BobbyTablesBot !327

    or even

    Hey /u/BobbyTablesBot, can you link me 327?

In any of the previous cases, the bot would respond with:

327: Exploits of a Mom
Alt-text: Her daughter is named Help I'm trapped in a driver's license factory.
Image
Mobile
Explanation

xkcd.com | Feedback | Stop Replying | GitHub | Programmer

Getting Started

To host your own xkcd bot, follow these simple example steps.

Prerequisites

  • Python 3.8
  • A Reddit Account

Installation

  • Clone the repo

    git clone [email protected]:joeyvanlierop/xkcdbot.git
    
  • Create an app here

    • Create a copy of config.example.json and rename it to config.json
    • Paste the appropritate credentials into the newly created config.json
    • Make sure to also add an appropriate user agent in the config.json
  • Install the requirements

      pip install -r requirements.txt
    

Running the bot

  • From the base directory run:

      python -m bot.bot
    

Running the tests

  • From the base directory run:

      python -m unittest
    

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

xkcdbot's People

Contributors

jgreenemi avatar jhonnystene avatar jmeberlein avatar joeyvanlierop avatar lcheng49 avatar menarulalam avatar nyhilo avatar scotsguy avatar stefanosporiazis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xkcdbot's Issues

Comic #404 is not displayed properly

find_comic(self, number) currently assumes that if the server returns a 404, that the comic doesn't actually exist and thus that the bot shouldn't display anything. However, as we all know, Randall purposefully skipped comic 404 as a nerdy joke, so that 404 error should be handled special.

Input validation missing for comment body edge cases.

Just found this project from the reddit thread! I like what I see - the whole project appears well-structured and clean. This issue is to help in validating user inputs.

Example of the behaviour this issue seeks to address: https://www.reddit.com/r/xkcd/comments/erydbl/introducing_ubobbytablesbot/ff7a610/

This is probably pretty obvious but pointing out all the same - validation on the inputs looks to be necessary at either 1 where the the body of the comment is pulled out, or 2 where the find_numbers actually makes use of the input. Looks like the one case where user input has been shown to cause a problem is when the input is simply too large, in which case you could curb this from breaking by doing one or multiples of the following:

  1. Introduce a try/except block so a single user's invalid input doesn't cause the whole bot to stop for everyone, just for the one request.
  2. Try to prevent a large input (like when a user writes an essay into a comment body, which could be unlikely to ever invoke the bot anyway) by truncating the body string to a manageable size. One could introduce a simple heuristic like...
body = comment.body[:1_000]

This heuristic ensures your comments can only ever be a certain size, and doesn't fail in the case that the input is an empty string. This of course relies on the input actually being a list-like object (as strings are in Python) so if for some reason the comment object were malformed this would fail. In that case, a try/except block within handle_comment() would help here.

  1. Add further logic to valid_comment() to handle more of these cases. The truncation approach is likely a good addition here.

Return random comic

Implement the ability to comment "!random" or "#random" to return a random comic. More information can be found here.

Implement logging

There is currently no way to monitor the activity of the bot. Logging should be implemented using Python's built-in logging class.

Is a backlog/queue script needed?

Since we are setting skip_existing to False when initializing the comment stream, even if the bot was down for whatever reason, when it goes back online, it should be able to respond to all the comments chronologically and eventually catch up. Am I wrong?

Sorry if It's a dumb issue I'm new to Github

Handle range of comics

Implement the ability to comment "!585...590" and return the comics with id's 585, 586, 587, 588, 589, 560. More information can be found here.

Call bot with "Relevant xkcd: {number}"

Add the ability to call the bot by commenting:
Relevant xkcd: {number}

Example comment:
Relevant xkcd: 327

The two streams that are currently being analyzed are:

  • Comments in /r/xkcd
  • Inbox items (username mentions, private messages)

This change will require analyzing comments from outside /r/xkcd which is currently not implemented. It will also require a new matching pattern.

Response size has no upper bound.

Found in 1, there appears to be no upper bound to the size of a response. If I was to issue a comment like the following, I suspect the bot would try to post all hundred comics:

!1 !2 !3 !4 !5 !6 !7 !8 !9 !10 !11 !12 !13 !14 !15 !16 !17 !18 !19 !20 !21 !22 !23 !24 !25 !26 !27 !28 !29 !30 !31 !32 !33 !34 !35 !36 !37 !38 !39 !40 !41 !42 !43 !44 !45 !46 !47 !48 !49 !50 !51 !52 !53 !54 !55 !56 !57 !58 !59 !60 !61 !62 !63 !64 !65 !66 !67 !68 !69 !70 !71 !72 !73 !74 !75 !76 !77 !78 !79 !80 !81 !82 !83 !84 !85 !86 !87 !88 !89 !90 !91 !92 !93 !94 !95 !96 !97 !98 !99

Example of this behaviour in action: https://www.reddit.com/r/xkcd/comments/erydbl/introducing_ubobbytablesbot/ff7o6rt/

This may cause the bot to break if the bot tries to write a reply to a subreddit that has a comment max size limit, rather than failing that request. This can be wrapped in a try/except block to contain the failure from such an occurrence affecting other users' experience using the bot (though this should not be the only line of defense here), and the response size itself should be limited to prevent the bot trying to create a huge response in the first place.

In most cases users will probably ask for a single comic, but incredibly unlikely (unless they're pressure-testing the bot) to ask for more than a handful. But of course data would tell you where best that upper bound should be.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.