Tweet on Raspberry Pi via Twython! (Part 1)

Tweet on Raspberry Pi via Twython! (Part 1)

How to Tweet via Twython by linking Raspberry Pi and Twitter.
Tweet on Raspberry Pi via Twython

This article was translated to English, and was originally published for deviceplus.jp.

Today, I’ll talk about linking up Raspberry Pi and Twitter through “Twython.” I’ve tried linking up Twitter in the past, but it’s harder than it seems! This time, we’ll be following the Raspberry Pi Official Site Document. We’ll be using the 2017-11-29 release of “Raspbian Stretch with desktop” OS version with a “Raspberry Pi2 Model B” for this article.

What Is Twython?

Twython Official Site

Twython – Twython 3.6.0 documentation

Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs

「Twython」is a library for Python for using Twitter API.

  Twitter libraries — Twitter Developers

twython by @ryanmcgrath — Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs. Supports all v1.1 endpoints, including dynamic functions so users can make use of endpoints not yet in the library.

The official Twitter also introduces this.

Installing Twython

Steps to install Twython
Fig 1.

Getting Started with the Twitter API

Let’s proceed. Click on the left menu when you’d like to switch the page, as the URL doesn’t change.

The second from the top, “What you will need,” describes how to install “Twython for Python 3.”

As always, start by updating the system.

Next, implement the “Twython” install icon.

Doing that will bring up this message:

Install Twython icon with Raspberry Pi
Fig. 2

Checking on the “pip3 list” command shows that “twython (3.4.0)” was confirmed, so it looks like it was already installed.

Finally, execute a confirmation command to see if you have everything you need.

Install Twython icon with Raspberry Pi
Fig. 3

If no errors come up, that means preparations are complete!

At the end of the page, there’s a warning stating that a verifiable phone number is necessary for using Twitter API. If you don’t register a phone number, you won’t be able to complete the Twitter registration on the application, so make sure to have this prepared beforehand.

Preparing a Twitter Account

The third item, “Create a Twitter account,” details how to make a Twitter account. For new accounts, follow the Twitter Official Site to set up an account. When using an existing account, go on to the fourth page.

The “Create a Twitter application” details how to make a Twitter application.

Sign into a Twitter account on this page Twitter Application Managementand click the “Create New App” button.

How to add Twitter application details

Next, input a “Name,” “Description,” and “Website.” Filling in something for “Website” is necessary, but since I won’t be taking this account public, I put in my own Twitter URL instead. After agreeing to the terms and conditions, click the “Create your Twitter application” button.

 Twitter application details error
Fig. 5

If you don’t have a phone number registered to your Twitter account, the following error message is going to appear.

When that happens, refer to the page below to begin registering a phone number.

How to register a phone number for your account

How to create my access token
Fig. 6

After you’re finished registering, click the “Keys and Access Tokens” button.

“Consumer Key (API Key)” and “Consumer Secret (API Secret)” are values that Twython needs to verify.

First, let’s confirm the access level.

If you have it as “Read and write,” that’s OK. I had that setting on by default when I was setting up, but you can change it anytime you want by clicking on the “modify app permissions” link.

Next is making an access token. Click the “Create my access token” button at the bottom of the screen.

Where to find my access token
Fig. 7

Under “Your Access Token,” “Access Token” and “Access Token Secret” will be displayed. Same as “Consumer Key (API Key)” and “Consumer Secret (API Secret)” from before, these values are needed when using Twython.

Setting up Date/Time

“Set the system date/time” on the fifth page is the date and time setting, so let’s adjust the date and time on the Raspberry Pi’s setting.

Posting on Twitter!

“Send a tweet from Python” on the sixth page will finally give us the chance to write a program! Select “Programming”-“Python 3(IDEL)” from the menu and open editor.

Posting on twitter with Twyton
Fig. 8

The initial display screen will look like this, but if it stays like this, each line will be executed line by line, so let’s create a new one starting with  [File] – [New File]. Line up 「Consumer Key (API Key)」「Consumer Secret (API Secret)」「Access Token」「Access Token Secret」 in the order below and save in a file called “auth.py.”

Python needs to be aware that the indentation by leading spaces and tab characters affects the program.

(Lines indented with the same number are considered as blocks)

I saved this file on the “/home/pi/” directory. I think when you log in as a pi user, this directory will be selected as default. Next, we’ll be typing out the program for the Twitter post. Again, create a new file on [File]-[New File] and write down the program, as seen below. Then, save the file as “twitter.py” in the same directory as “auth.py.”

With the “twitter.py” window active, press the “F5” key or run “Run -> Run Module” from the top menu.

Your first tweet on Raspberry pi
Fig. 9

The execution screen should then display 「Tweeted:Hello world!」

Hello world tweet
Fig. 10

Let’s try confirming this by accessing Twitter. If “Hello world!” is posted, then that means we’ve succeeded!

Successfully tweeted with Twython in Raspberry Pi
Fig. 11

If you try to continue posting the same Tweet, this error message is going to come up. When that happens, try changing the content of the “message” and word order to get it to work.

In Conclusion

For part 1 of “Twython,” we’ll stop here at the character word order for the Tweets.

When I worked on Twitter related project before, I had direct access to Twitter’s API, but I thought the difficulty level got much lower by using the library.

I think that “Twython” with Python as a base is easy to introduce to any electronics that uses parts. Posting images, searching tweets… We’ll be talking about those in the next article!

Next time, we’ll be going over pages 7-9 of the document and trying out a variety of different tweets!