Tuesday, September 6, 2016

Make a Twitter Bot in Python and AWS Lambda

I followed this excellent tutorial from Joel Grus on how to make a Twitter Bot. If you have followed some of my post before, Project Mining Social Web 2nd Edition or Python with JSon, you know I have had a little bit of experience using the Twitter API.

Here is a screenshot of the Twitter Bot:



What is different this time, is that I wish to use AWS Lambda as the backend. I want to know what is required and what the workflow was. I wont repeat the steps Joel listed out in his tutorial, just some of the things I found out:

1. Seems that AWS Lambda wants the main file to be lambda_function.py and the main function to be lambda_handler(event, context).

2. The scheduling trigger is now "CloudWatch Events - Schedule" instead of just schedule.



3. I learned this from a friend that to always immediately put the credential file in .gitignore to avoid accidentally checking it in on GitHub.

echou$ cat .gitignore
*.pyc
credentials.json

I am now the proud owner of a Twitter Bot that searches for Cisco layoff news and retweet them. On this date of Aug. 19, 2016 that was the big news of the week.

https://twitter.com/Network4_Nerds

echou$

Here is what my code ended up as:

from __future__ import print_function
from twython import Twython
from twython.exceptions import TwythonError
import re
import json, pprint
with open('credentials.json') as f:
  credentials = json.loads(f.read())
client = Twython(credentials["consumer_key"],
                 credentials["consumer_secret"],
                 credentials["access_token_key"],
                 credentials["access_token_secret"])
query = "cisco layoffs -filters:retweets"
rgx = r"cisco"
def lambda_handler(event, context):
    results = client.search(q=query)
    for tweet in results["statuses"]:
        text = tweet["text"]
        # re.search matches anywhere in the string; re.I means case-insensitive
        if re.search(rgx, text, re.I):
            print(tweet["text"])
            # client.retweet will raise an error if we try to retweet a tweet
            # that we've already retweeted. to avoid having to keep track, we
            # just use a try/except block
            try:
                client.retweet(id=tweet["id"])
            except TwythonError as e:
                print(e)

results = client.search(q=query)
pprint.pprint(results)


Happy Coding!




141 comments:

  1. Updating ourselves to the latest technology is the good idea to live in this modern IT world. Reading articles like your blog this will make an encouraging power within me. Thanks for sharing such an informative content. AWS Training in Bangalore | Big Data Hadoop Training in Bangalore

    ReplyDelete
    Replies
    1. Amazing Article, thank you!. I just wish to give you a big thumbs up for the excellent post. Kindly keep updating your blog. Java Developer is a dream career for IT students.To start wonderful Career to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India .

      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  2. you provide a very good information that is very useful for modern IT world. I gather lot of articles but this very nice one to read,They gave lot of new ideas for me.Thanks for sharing this valuable information.
    Python Online Training

    ReplyDelete
  3. Thanks for sharing this unique and informative post. This blog which provided me the required information. Software Testing Training in Chennai | Java Training in Chennai

    ReplyDelete
  4. It’s too informative blog and I am getting conglomerations of info’s about Python certification. Thanks for sharing,
    Python Training in Marathahalli
    Java Training in Marathahalli

    ReplyDelete
  5. At that point I scanned for website architecture, site improvement, and pay per click Buy twitter followers

    ReplyDelete
  6. Great article. May be create a short video/GIF about the working of the application. It will be very useful to see the working of the app.

    Ashwathi,
    Facilitator,
    Kamal Technologies - Best React JS, Angular JS, Node JS training institute in Chennai

    ReplyDelete
  7. This blog is very helpful for beginners and experts also, thanks for sharing it. Keep share content on MSBI Online Training Bangalore

    ReplyDelete
  8. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    AWS Training in Bangalore|

    ReplyDelete
  9. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    amazon-web-services-training-institute-in-chennai

    Best Hadoop Training Institute In chennai

    ReplyDelete
  10. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Selenium Training in chennai


    ReplyDelete
  11. Hi Eric,

    Nice to be visiting your blog again, it has been months for me. Well this article that i’ve been waited for so long.

    I am a beginner in programming,started learning python a few days ago.When I was doing exercise,found a problem that I couldn't solve by myself.Could anyone help me about it?Thank you very much.
    My python version is 3.6.3
    the codes:
    Python Code: (Double-click to select all)
    1
    2
    3
    4 name = ["aa", "bb", "cc", "dd"]
    for zzz in (name):
    name.remove(zzz)
    print(name)
    I wanted to delete or remove all values in the list,but the results always contain the values on the even places (like "bb" and "dd").
    However,when I execute codes below:
    Python Code: (Double-click to select all)
    1
    2
    3 name = ["aa","bb","cc","dd"]
    for zzz in (name):
    print("hello, " + zzz)


    I can get the results I want, printing every string with all the names in the list.
    Is it because this function cannot use with remove function?


    PEP 8 provides coding conventions for the Python code. It describes rules to adhere while coding in Python. This helps in better readability of code and thereby better understanding and easy maintainability. It covers from code indentation, amount of space to use for indentation, spaces v/s tabs for indentation, commenting, blank lines, maximum line length, way of importing files, etc.

    It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
    Keep up the good work!

    Ciao,
    Hima

    ReplyDelete
  12. Hello There,


    A really interesting, clear and easily readable Network Automation Nerds article of interesting and different perspectives' will clap. So much is so well covered here.

    I need some help on how to build this project and what c++ program to us, I have TC in stalled and when I build it comes up with and error or some think like that, all I want is to learn how to build projects and I really want to see how this project works.
    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.


    Thanks and Regards,
    Meera

    ReplyDelete
  13. This is a much needed information thank you for sharing and it's very helpful to know about this information. Thanks for sharing it MSBI Online Training

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This is an excellent blog thanks for sharing valuable information with us please check it once at MSBI Online Training Hyderabad

    ReplyDelete
  16. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    embedded rtos training in chennai | embedded testing training in chennai .

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. thanks for conveying this information in a simpler manner..
    https://www.besanttechnologies.com/training-courses/python-training-institute-in-chennai

    ReplyDelete
  19. Very Good and Useful Information about python-and-aws Thanks For Sharing Nice Article anyone want to learn advance devops tools training or devops Online training
    DevOps Online Training

    ReplyDelete
  20. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
    Click here:
    angularjs training in velarchery
    Click here:
    angularjs training in sholinganallur

    ReplyDelete
  21. I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.
    Click here:
    Microsoft azure training in bangalore
    Click here:
    Microsoft azure training in pune

    ReplyDelete
  22. Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
    Blueprism training in marathahalli


    AWS Training in chennai

    AWS Training in bangalore



    ReplyDelete
  23. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    Devops training in velachery
    Devops training in annanagar

    ReplyDelete
  24. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.

    java training in annanagar | java training in chennai

    java training in chennai | java training in electronic city

    ReplyDelete
  25. very nice blog.it contains a lot of information for amazon web services(aws).Thanks a lot.aws online training

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. Really awesome work. keep on blogging, thanks for sharing!
    DevOps Online Training

    ReplyDelete

  30. Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

    DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    Good to learn about DevOps at this time.


    devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

    ReplyDelete
  31. For this web site, you will see our account, remember to go through this info.
    mason soiza

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete
  33. Much obliged to you for exceptionally usefull data.. Buy Twitter Retweets

    ReplyDelete
  34. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts
    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  35. This comment has been removed by the author.

    ReplyDelete

  36. The information which you have provided is very good. It is very useful who is looking for Python Training

    ReplyDelete
  37. What a useful article, This is a wonderful sharing, Given so much info in it, keep sharing more..

    ExcelR Data Science in Bangalore

    ReplyDelete
  38. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
    date analytics certification training courses

    ReplyDelete
  39. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  40. I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!




    DATA SCIENCE COURSE MALAYSIA

    ReplyDelete
  41. I see the greatest contents on your blog and I extremely love reading them.
    machine learning course in bangalore

    ReplyDelete

  42. I went through your blog its really interesting and holds an informative content. Thanks for uploading such a wonderful blog.
    python classes near Bellandur|python classes in Marathahalli
    selenium testing classes in Bangalore|selenium testing classes near Bellandur

    ReplyDelete
  43. I love your article so much. Good job
    Participants who complete the assignments and projects will get the eligibility to take the online exam. Thorough preparation is required by the participants to crack the exam. ExcelR's faculty will do the necessary handholding. Mock papers and practice tests will be provided to the eligible participants which help them to successfully clear the examination.

    Excelr Solutions

    ReplyDelete
  44. It's really nice and meanful. it's really cool blog.you have really helped lots of people who visit blog and provide them useful information.
    data science course

    ReplyDelete
  45. I Got Job in my dream company with decent 12 Lacks Per Annum salary, I have learned this world most demanding course out there in the current IT Market from the Hkbk group of institutions experts who helped me a lot to achieve my dreams comes true. Really worth trying

    ReplyDelete
  46. Your blog has very useful information about this topic which i am looking now, i am eagerly waiting to see your next post as soon
    salesforce Training in Bangalore
    uipath Training in Bangalore
    blueprism Training in Bangalore

    ReplyDelete
  47. Thank you so much for sharing this excellent information. Your article is amazing. Good to discover your post. We are the Best

    salesforce Training in Bangalore
    uipath Training in Bangalore
    blueprism Training in Bangalore

    ReplyDelete
  48. https://www.socifan.com/free-facebook-followers Did you ever envy the people and influencers on the web just because they had a lot of followers backing them up? If you said yes, then we would like to present our newest tool that lets you get free Facebook followers!

    ReplyDelete
  49. Very interesting, good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up

    aws Training in Bangalore
    python Training in Bangalore
    hadoop Training in Bangalore
    angular js Training in Bangalore
    bigdata analytics Training in Bangalore

    ReplyDelete


  50. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    Best Angularjs Training in Chennai
    Best Java Training in Chennai
    Best Bigdata Hadoop Training in Chennai
    Best SAS Training in Chennai
    Best Python Training in Chennai
    Best Software Testing Training in Chennai

    ReplyDelete
  51. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    Digital Marketing Training in Chennai

    ReplyDelete
  52. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging...
    AWS Course in Bangalore

    ReplyDelete
  53. Very informative post.If anyone wants to learn python can check this Python Training institute.

    ReplyDelete
  54. Nice blog.Check this Python training institute.

    ReplyDelete
  55. dish tv recharge online Greetings! This is my first comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. keep sharing

    ReplyDelete
  56. Such computing helps in providing centralized data processing, authentic access, dedicated storage and centralized processing of information through an encrypted form. Cloud means internet that can be accessed from any geographical region. machine learning courses in hyderabad

    ReplyDelete
  57. Although data science does involve machine learning, this field has a set of a lot of different tools. machine learning courses in hyderabad

    ReplyDelete
  58. This is the reason many industries hire the services of machine learning professionals for developing ML based apps. best machine learning course in hyderabad

    ReplyDelete
  59. So, if you are interested in data science or machine learning, we suggest that you take a best machine learning course in hyderabad

    ReplyDelete
  60. I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.
    Cyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course

    ReplyDelete
  61. Attend online training from one of the best training institute Data Science Training in Hyderabad

    ReplyDelete
  62. Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing.
    data science courses

    ReplyDelete
  63. Very informative content.
    Join 360digitmg for the best data scientist course at Hyderabad.
    Data scientist course

    ReplyDelete
  64. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.

    Manal testing Training in Gurgaon
    Selenium Training in Gurgaon
    Cucumber Training in Gurgaon
    Microservices Testing Training in Gurgaon

    ReplyDelete
  65. Your posts are very good, through this I have gained a lot of information, keep posting.First Copy Ladies Watches Online

    ReplyDelete
  66. Excellent post. Gained a lot of knowledge from it. Looking ahead for more of such interesting postings
    https://www.kellytechno.com/Hyderabad/Course/amazon-web-services-training

    ReplyDelete
  67. Really Good blog post.provided a helpful information.I hope that you will post more updates like this. https://www.cognextech.com/aws-training-and-certification-course-3

    ReplyDelete
  68. it’s really nice and meanful. it’s really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information.
    Data Science Training in Hyderabad

    I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

    Devops Training in Hyderabad

    Hadoop Training in Hyderabad

    Python Training in Hyderabad

    Tableau Training in Hyderabad

    Selenium Training in Hyderabad

    ReplyDelete
  69. Great with detailed information. It is really very helpful for us.
    Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
    As a best video production company in Bangalore, we produce quality and creative videos to our clients.

    ReplyDelete
  70. Excellent post. Very informative.
    Python is one of the most popular language where one can easily learn coding. That's why Cisco has added automation and programming language in their certification exams. You may also visit PYTHON FOR NETWORK ENGINEERS course training.

    ReplyDelete
  71. I’m glad you like my post! I learned many new things myself
    by cognex is the best AWS Training in Chennai

    ReplyDelete
  72. Very nice blog i really enjoyed to read this blog.

    IntelliBuddies

    ReplyDelete
  73. CCNP ENCOR training will prepare candidates for ENCOR exam that will be helpful in CCNP enterprise, CCIE enterprise infrastructure, and CCIE enterprise wireless certifications.

    ReplyDelete
  74. Python for network engineers is very demanding course among CCNA aspirants. Through Python network engineers learn automation and programmability and this is mandatory skills to pass CCNA exam.
    Thanks for this informative post.
    CCNA training institute in Pune

    ReplyDelete
  75. I really enjoyed this blog. It's an informative topic. It helps me very much to solve some problems. Its opportunities are so fantastic and the working style so speedy.
    data scientist training and placement in hyderabad

    ReplyDelete
  76. I have read your blog and information on your blog is helpful for me.

    ReplyDelete
  77. Really Good blog post.provided a helpful information.I hope that you will post more updates like this.Visit here : https://www.cognextech.com/aws-training-and-certification-course-3

    ReplyDelete
  78. Great information and Thank you so mush for the publishing such type of information with us

    Python training institute in Hyderabad with real-time projects

    ReplyDelete
  79. Good Information Thank you..

    ELearn Infotech offers Python Training in Hyderabad Madhapur. Our Python course includes from Basic to Advanced Level Python Course. We have designed our Python course content based on students Requirement to Achieve Goal. We offer both class room Python training in Hyderabad Madhapur and Python Course Online Training with real time project.

    ReplyDelete
  80. Good information about the twitter bots and how you can build it, 101desires.com computer thank you very much for sharing this valuable knowledge. I glad to read Thank you

    ReplyDelete