Speaking Twitter


This will allow you to produce a basic program which makes a Raspberry Pi (or any computer with Python) speak the latest tweet from a particular person or search.

Prerequisites

Espeak

Espeak is a text to speech engine.

To install it you need to type sudo apt-get install espeak into the terminal on the Raspberry Pi

pyTTSx

Whilst espeak provides the text to speech engine, it doesn’t come with a python interface. For this we need to install pyTTSx

Type the following into the terminal:

  1. sudo apt-get install python-pip
    This allows you to install python libraries
  2. sudo pip install pyttsx
    Installs pyttsx using pip

Twitter API wrapper

This is required to allow python to access Twitter’s API. Type the following into the terminal:

sudo pip install python-twitter

Get access to Twitter API

These instructions assume you already have a twitter account, if not you need to create one before proceeding

  1. Navigate to https://dev.twitter.com/apps and click Create a new application
  2. Complete the form provided including the Capcha and click Create your Twitter application
  3. You will now be provided with consumer key and consumer secret which will look something like this:
    Consumer key 05d0FO30g50rIwM3zVfKQ
    Consumer secret SCEOp3iYPs0s2YjqKHbw6ggEr6kgpgMzgrtXHrMgHAM
  4. Click on Create my access token then click on Details at the top of the screen to reload the page. You will then get your access token and access secret similar to shown below:
    Access token 281981832-h0dYhnapEWZAVAyodSlX4u80r7vwDy8H6AlDA7Ee
    Access token secret bTsHKqR2EvR1UQNjbqPyYFea2Q2SRmsKShYmpw2sT2Aog

    NOTE THE TOKENS AND SECRETS PROVIDED ARE NOT REAL AND WILL NOT WORK FOR YOUR PROGRAM

The code

Type the following into your preferred IDE (e.g. IDLE):

Modify lines 3-6 to use the information you provided when setting up a Twitter application earlier. You can also modify the search and text on lines 11, 13 and 15.

Save this as twitter-speaker.py and run using python twitter-speaker.py

Leave a Reply