NgenTwit – Tweet like no one follows

Well, the “description” part of the title is not a feature at all of a complete application. That’s merely my lack of knowledge in Python, and definitely I don’t have enough time to learn Python. As an old ode says “there isn’t `too late to learn`” and I do believe in my personal inner intelligent and spirit for the sake of the harmony of this universe … I started to learn Python.

As my usual learning scheme, I can’t start if there is no challenge. So, I did challenge myself to write a new twitter desktop application. That was the birth of NgenTwit (credit to for the name). The first thing came into my mind is, this application must have a nice GUI. Then, it has the ability to do automatic tweet. For the former spec, choice is PyGTK.

I don’t want to have a big confusion in the beginning, so I look for Python twitter framework, and I choose Tweepy. From it’s documentation, it is clear, clean, and easy to use. Then I start to write.

The first thing to do is to make sure that Tweepy is correctly installed, please refer to Tweepy documentation. You will need Tweepy-example package too, to test Tweepy installation.

Next is twitter related administration. Twitter provide two types of authentication; Basic auth and OAuth. Tweepy provides a very cool API to use respectively. For my case, I will use OAuth. It allows me to tell me follower from where I send my tweet.

Assume that I have an application registered to twitter, I will have consumer key and consumer secret as follow:

consumer_key = "xxxxxxxx"

consumer_secret = "yyyyyyyy"

Each user need access token and access token key. This is persistent once you are authenticated. We can get them by using one of tweepy example: tweepy-examples/oauth/

So now I have four variables needed to access Twitter API; consumer_key, consumer_secret, access_token_key, access_token_secret.

First, I will need Twitter to authenticate me.

 auth = tweepy.OAuthHandler("xxxxxxxx", "yyyyyyyy")

as we have access_token_key and access_token_secret, these variables are set to Tweepy as follow:
assumes their values are “aaaaaaaa” and “bbbbbbb” respectively.

auth.set_access_token("aaaaaaaa", "bbbbbbb")

then initialize Tweepy API with this authenticated access token.

api = tweepy.API(auth)

Now I can do anything I want thanks to tweepy.API.

Now, done with twitter stuffs, I am ready to write the GUI. I am using PyGTK. This is not a rich GUI, it is merely intended to catch my tweet and send it to Twitter once I press return. No way to see the timeline, replies, friends, etc. So, basically only two things needed.
1. User input
2. update_status of Tweepy API

The result is as simply as follow:

Here is the code:

#!/usr/bin/env python

import pygtk
import gtk
import tweepy

class Ngentwit:
    def enter_callback(self, widget, entry):
        entry_text = entry.get_text()
        entry.set_text("Sending to twitter...")
        auth = tweepy.OAuthHandler("xxxxxxxx", "yyyyyyyy")
        auth.set_access_token("aaaaaaaa", "bbbbbbb")
        api = tweepy.API(auth)

    def __init__(self):  # create a new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(300, 50)
        window.connect("delete_event", lambda w,e: gtk.main_quit())
        vbox = gtk.VBox(False, 0)  window.add(vbox)
        frame = gtk.Frame("Tweet? No one follows")  

        entry = gtk.Entry()
        entry.connect("activate", self.enter_callback, entry)
        entry.select_region(0, len(entry.get_text()))  

        vbox.pack_start(frame, False, False, 0)

def main():
        return 0

if __name__ == "__main__":

PECL and twitter oauth

Currently, I am trying to make batbet to be able to communicate with twitter API. Twitter uses oauth as authentication method. The first thing comes in mind is to use liboauth for C (available here). In fact I am not so familiar and I am studying how twitter’s oauth works. I got clue from PECL sample (available here).

So, the first thing to do is how to make PECL works then port it to C. Why C? because this is the only language I know 😛

How to install PECL oauth extension?

$ pecl search oauth

Retrieving data…0%Matched packages, channel
Package Stable/(Latest) Local
oauth   0.99.9 (beta)   0.99.9 oauth consumer extension


$ sudo pecl install oauth-0.99.9

Now PECL oauth should be installed to your machine.

or you can also build manually the extension using phpize.

download oauth-0.99.9, extract then go inside oauth directory, invoke phpize

oauth-0.99.9 $ phpize

oauth-0.99.9 $ ./configure; make

oauth-0.99.9 $ sudo make install

To make your new extension works you should adjust your php.ini. On ubuntu there is /etc/php5/cli/php.ini then add following:

Once you’re done, go to twitter example of oauth PECL package.

$ php updateStatus.php

you’ll see …

I was wondering how does the twitter authentication method go, from the beginning to the end. I might be wrong but it works for me, following is the workflow:

1. After you get consumer key and consumer secret, you need to request_token(). For instance, using PECL liboauth:

$request_token_info = $oauth->getRequestToken(;

then you will get “oauth_token” reply.

2. You need to ask authorization from user, by giving a link to: -> you get this on the step #1

User will have to allow the access.

3. Right after authorization is granted by user, you need to request a new oauth token by accessing:


You will get oauth_token and oauth_token_secret to be used to call twitter REST API. This oauth_token and oauth_token_secret is persistent. I save these variables to a plaintext to be used as future request, so user does not need to do step #2 anymore.

4. Use Twitter REST API to do anything you want.

NOTE: I am open to any suggestion.