I'm looking to write a simple Python 3 bot for reddit which will reply to all top-level comments in a thread with a particular title, in a particular subreddit.
For example:
Bot scans /r/testsubreddit for the most recent post with the title 'bot trigger'
Bot replies to all top-level comments in that thread with a random string from a predefined list
What's the best way to go around doing this?
Thanks.
I think it's a valid question. You asked "How do I go about doing this?", not "Build this for me", which is certainly a valid question for a programming Q&A website.
I see two possible routes you could go:
Using a library of your choice (I would use requests) to make some network calls to the Reddit API. There's a lot to digest on that page, but to get started with OAuth, which you need in order to make a bot that's more than just read-only, check out this guide.
Using an existing framework, such as PRAW. I love using PRAW, although again, if you've never done anything like this before, it's kind of tricky to figure out. Follow along with some of the examples under Quick Start -> Common Tasks to get your feet wet. I think you'll find it relatively straight forward after first writing some basic scripts to get submissions, get comments from submissions, and automate comment replies.
After you've gotten oriented, your basic algorithm is simple:
Get your auth headers (or Reddit instance if you're using PRAW)
Get the submission you want to target
Get all of the top-level comments on that submission
Loop through them and reply however you want
If you get stuck, you can ask PRAW questions at http://reddit.com/r/redditdev, or come talk to me at my sub, http://reddit.com/r/redditscripting.
Once you have some initial code going, your fellow programmers will respond a little more warmly to your requests for help :-)
Happy coding!
Related
I need a telegram bot that sends you inline button with some information bellow and when you press that button, it sends information (not an URL or something) I need a message with an image for example, when I search for tutorials it only shows me to put URLs, if someone can help me I would be so gratefull
A quick search reveals that you are searching for a keyboard and if you look into the python-telegram-bot library, you can find examples like inlinekeyboard2.py. Once you understand how it works, you can move on to sending a message. I suggest you take a look at the resources section in the same library, since you tagged python.
If you try this and fail at a clear definable problem, then you might ask a question here (in a new thread), so that you can get an exact solution.
I'm using instabot in python.
I want to get a list of users that requested to follow me and accept some of them according to some conditions and stuff.
I have read the documentation and didn't find anything about this.
I just want to have the list somehow.
Not just through instabot.
So if u know another way to get the list, some kind of web scraping or by using another bot or even Instagram's native API, I will be happy to hear (read!) that.
I am not familiar with Instabot in Python, but you can use this as a reference https://github.com/mgp25/Instagram-API.
That library is written in PHP and able to do get your pending follower list, even to do actions such as reject or accept the follow request. I'm currently using that library and it's well maintained.
Hope that helps. :)
Install following library:
https://github.com/instagrambot/instabot
And use get_pending_follow_requests function to retrieve intended list.
I am new to Python (somehow I started looking at python 2 days ago).
I tried to write a simple interactive webpage with CGI, and I used HTML form to get user input. However, as everyone knows, if I refresh the webpage, the form data will be resubmitted again. I just have no idea how to prevent this from happening.
The same problem, but in php/javascript, has been discussed and solutions can be found, but I would like to find the answer to the python case.
I hope there is someone here who can address my problem. Since I am not quite familiar with the language, I hope that, instead of solely giving me a descriptive solution, the kind one can also give me the pieces of codes necessary.
Thank you very much
Your question is not Python-specific, it's about the general web application design patterns.
Use GETs for requests that do not change the web app's state (viewing, queries and so on). Render the page in the response.
Use POST for whatever request that modifies the state (post submitting, editing, deleting).
Never render anything in the response, provide a 303 See Other redirection to the page where the results of the POST can be seen. E.g., if the user added a comment on the page http://example.com/blog/123, the POST handler can be http://example.com/submitcomment, but it should return nothing but a redirection to http://example.com/blog/123#comment5.
That's what #mikep referred to as Post-Redirect-Get.
On the related note: do yourself a favor, skip CGI. There is a number of nice and clean web frameworks for Python. Try Flask or Bottle (smaller frameworks) or go straight to Django if you are building a big app. Search here on Stack Overflow for pros and cons of different frameworks.
Just skip CGI. It's hard to build a good and maintainable CGI app, there are too many things to take care of, things that are already thought if in the frameworks.
I've been wasting time supporting a CGI application for some time, and switching to a framework was a relief.
You might want to view these lectures: http://www.udacity.com/overview/Course/cs253/CourseRev/apr2012
Week 2 will address your queries.
Take a look at POST-REDIRECT-GET web programming patern.
I've built an internal company website using Django. I have a view that sends an email to the users of the website using the send_mail() function.
Some users were not receiving emails from the site, and we fount that if they have configured Outlook to High junk-email protection level, the emails from the website are flagged as spam.
Are there any coding techniques for making an email sent from Python appear legitimate to Outlook and other email clients?
This is not really Django related problem but more about what and how you send emails, there are lots of great articles and blog posts about this topic, this is one of my favorite:
http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html
Of course you can always take a different direction and let someone else do the job for you, this will work almost certainly better that running your own smtp server. You are going to have nice stuff like stats (things like bounces, hard bounces, spam, blocks...) higher deliverabilty ...
This come with a price which in my experience will be less than the time your are going to spend to let the things run properly :)
Just to name one of those I had direct experience using MailJet with Django to handle quite big email sending (few millions per week) and it works great.
What worked for me was a simple thing that was missing. I added a "text_content" argument
(2nd from left) which was left blank before.
Also I added some text in the template in paragraph form .
msg = EmailMultiAlternatives(
subject, "Please find details of Candidate in this mail",
request.user.email,
[round_taker.email])
I know this sounds NOT so technical. But it might work for some people.
I'm learning Python and would like to start a small project. It seems that making IRC bots is a popular project amongst beginners so I thought I would implement one. Obviously, there are core functionalities like being able to connect to a server and join a channel but what are some good functionalities that are usually included in the bots? Thanks for your ideas.
Unless it's solely for the educational experience, you should really just use a framework for the core functionality.
That said, here's some of the things the bot in my home IRC channel does:
Choose one item from a list of options
Display a random entry from the Linux fortunes file
Display a random set of words from the Emacs spook file
Check every line from a user and display a quote from The Big Lebowski if it's sufficiently similar (this is probably a bit my-channel specific :) )
Check if a link has been mentioned before and say who/when (we all read the same RSS feeds and tend to duplicate links a lot)
Conduct a poll
Pull a given quote from our internal QDB
Check if a given link has been posted to Reddit, and give the corresponding Reddit thread link if so. If a Reddit link is posted, give the direct link instead
Track the last time a given nick was in the channel, and the last time they spoke
Queue a message for an offline nick that's automatically sent in-channel when they join
Use Google Translate to translate a given phrase
Post a given line to our channel's Twitter feed
Choose a random user and kick them (not the best idea depending on how unruly your channel is)
Pull the summary of a given term from Wikipedia and display it along with a link to the full article
Display information about any posted Youtube link (video title, length, submitter, votes, comments, etc.)
I'm also in the process of writing a bot in node.js. Here are some of my goals/functions:
map '#' command so the bot detects the last URI in message history and uses the w3 html validation service
setup a trivia game by invoking !ask, asks a question with 3 hints, have the ability to load custom questions based on category
get the weather with weather [zip/name]
hook up jseval command to evaluate javascript, same for python and perl and haskell
seen command that reports the last time the bot has "seen" a person online
translate command to translate X language string to Y language string
map dict to a dictionary service
map wik to wiki service
Again, this is an utterly personal suggestion, but I would really like to see eggdrop rewritten in Python.
Such a project could use Twisted to provide the base IRC interaction, but would then need to support add-on scripts.
This would be great for allowing easy IRC bot functionality to be built upon using python, instead of TCL, scripts.
That is very subjective and totally depends upon where the bot will be used. I'm sure others will have nice suggestions. But whatever you do, please do not query users arbitrarily. And do not spam the main chat periodically.
Make a google search to get a library that implements IRC protocol for you. That way you only need to add the features, those are already something enough to bother you.
Common functions:
Conduct a search from a wiki or google
Notify people on project/issue updates
Leave a message
Toy for spamming the channel
Pick a topic
Categorize messages
Search from channel logs