Giter Site home page Giter Site logo

twitterbot's Introduction

Twitter Bot

This solution/tutorial is meant to produce a very simple twitter bot that generates random sentences based on a list of sentences and tweets them out on a schedule.

I <3 constructive criticism, so if you think there's something that could have been done better don't hesitate to contact me or submit a pull request- I'd love to discuss it.

*This is a work in progress.

Prerequisites

  • A twitter account with a verified email. This will be the account that tweets.
  • A Heroku account. Heroku is a cloud platform. The free account is strong enough to host this bot. See the 'Setting Up Heroku' section for more details.
  • It's helpful to have a computer that can execute .py (Python) scripts.

Setting Up Your Twitter Account

Generating Random Sentences

The bot.py script that was provided is what will run on the cloud. It generates a random sentence, tweets it, and then sleeps for a given amount of time.

The code generates the random sentence by randomly choosing words that appear next to one another from the sentences provided in sentences.txt. If you haven't made any changes to the solution, sentences.txt will contain the poem There is Another Sky by Emily Dickinson. Change this as desired. Sentences need to be in the format that this poem is in. Each one needs a new line, and to end with either a period (.), question mark (?), or exclamation point (!).

Optional Testing/Understanding

Let's modify bot.py so that we can generate a random sentence and print it out rather than tweet it. Try replacing your main function in bot.py with this new one:

def main():
    # TODO
    CONSUMER_KEY = 'Your Consumer Key'
    CONSUMER_SECRET = 'Your Consumer Secret'
    ACCESS_KEY = 'Your Access Key'
    ACCESS_SECRET = 'Your Access Secret'
    # auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    # auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    # api = tweepy.API(auth)

    n = [1, 2, 2]
    s = random.choices(n)
    oldSentence = ""
    sentence = ""
    # while (True):
    try:
        for i in range(0, s[0]):
            if i == 0:
                sentence = getRandomSentence()
            sentence += " " + getRandomSentence()
        if sentence[-1:] != '?' or sentence[-1:] != '!':
            sentence = sentence[:-1]
        allCaps = IsAllCaps()
        if allCaps:
            sentence = sentence.upper()
        print(sentence)
        # api.update_status(sentence)
    except Exception as Error:
        print(Error)
        # api.update_status("Although there's a chance this may error as well, 
        # + "this is where you can have your bot tweet when something goes wrong. 
        # + "I recommend including your '@' so that you receive a notification.")
    # time.sleep(7200)

I've commented out everything related to twitter, and it no longer runs on a loop.

Now you can run the script:

And you'll see that it used the poem to generate a random sentence.

If you get an error related to tweepy, use pip to install it.

Setting Up Heroku

twitterbot's People

Watchers

James Cloos avatar fabes avatar

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.