Join 3,433 readers in helping fund MetaFilter (Hide)

How do I code a website to use Twitter as an interface?
May 8, 2012 12:39 PM   Subscribe

How do I code a site to parse direct msg tweets, act on them, and then reply?

I'm trying to code a website, in PHP and MySQL, that users can interact with via SMS and Twitter. (Twitter is the priority because I'm not about to get a dedicated number for users to send SMS to.) Basically, the interaction would be similar to how users of RememberTheMilk interact with that service via Twitter; they can send a direct message through Twitter to input information and to retrieve information.

I've managed to get some groundwork laid, but I'm struggling as a real novice. I can't quite figure out how to get retrieved DMs to be parsed, individually "executed," and then replied to. Where can I turn for help? I need example code.
posted by dvrcthewrld to Computers & Internet (8 answers total) 5 users marked this as a favorite
Why not get an SMS number? Give Twilio a try
You can get DMs from the twitter REST api
which has a plethora of PHP wrappers here:
posted by gunslingingbird at 1:10 PM on May 8, 2012

What I really need is example code that I can modify, even if heavily, for my needs.

Some recent searching has yielded this:
It's close, but I'm looking for a little bit more than that.
posted by dvrcthewrld at 1:25 PM on May 8, 2012

This could well be a great task for If This Then That. Here's a random recipe that processes Twitter DMs.

If you can't build your whole app in ifttt, you could use it to handle the initial DM parsing and call out to your PHP to handle the details.
posted by zachlipton at 1:44 PM on May 8, 2012

If what you mean by "parsed" is to read natural language text coming to you and perform interesting actions based on it, then what you want to do is natural language processing. It's an *extremely* difficult problem. The sort of thing that Google hires PHDs to work on.

If what you are parsing is a particular language of your own design, say a minimalistic programming language, then you might want to read up on things like context-free grammars, and tools like bison and flex. These are the sort of tools that people use to build compilers for programming languages, and that's essentially what you want to do here.

Exactly how deep you want to go depends on what it is that you're parsing, but you're really starting down the road of a compiler design class here.
posted by tylerkaraszewski at 7:14 PM on May 8, 2012

The input that I would be parsing would be very simple predefined terms.

For example: "met Andrew" would add "Andrew" to a db table of acquaintances, then some background processing would go on that would select a "random" name from elsewhere in the db and reply with "go find Mark"

That's not what I'm actually after, but it's the same idea. Users who sent DMs that didn't conform to the proper input would be replied to with a message along the lines of "I didn't understand that. Please use proper format."
posted by dvrcthewrld at 7:44 AM on May 9, 2012

Presuming you want to stick with straight PHP to parse your input, you'll probably wind up using regular expressions. Setting aside the old, "Now you have two problems" joke, it's probably not going to be that big of a deal.

If your input is of the form "<command> <target>", something simple like '(\w+) (\w+)' might work. If your commands get more complex than two words seperated by a space, you'll need to have a more complex regex, in which case using a regex tester will make your life much easier.

For the simple case, it might wind up looking something like
$input = 'met Andrew'; // Obviously, this would actually come from reading the tweet.
$input_correct = preg_match('(\w+) (\w+)', $input, $matches);

if ($input_correct) {
    $command = $matches[0];
    $target = $matches[1];

    switch ($command) {
        case 'met':
            // Do your pick a random response thing.

        case 'somethingelse':
            // Do some other thing.

            // Invalid command. Return an error.
else {
    // Didn't match the pattern. Return an error.
In any case, you'll want to make sure you sanitize the input to protect yourself if somebody tries to sneak a malicious target like 'Andrew;drop table acquaintances;' past you.
posted by ob1quixote at 8:55 AM on May 9, 2012

ob1quixote, your answer is a little less than I was hoping for, but perhaps you can clarify for me.

I could have my application hooked to the Streaming API to receive only DMs.
I would parse that feed and store each tweet in the db.
Separate code would constantly process each tweet from the db, marking each tweet as processed as it went 1 by 1, using a more robust version of your example.

I guess that leads me to wonder how to use database entries as input, how I'd get the code to run virtually constantly, etc. Does this sound right?
posted by dvrcthewrld at 12:14 PM on May 10, 2012

Yes, what you want is doable. Reading from and writing to a database is more or less what PHP is for. You'll probably want to use a queuing system such as Gearman or RabbitMQ.

If it were me, I think I'd have one queue reading the tweet stream and writing them into the database; a second queue reading them from the database, doing the processing, and then writing the results to the database; then a third queue sending out the resulting messages.
posted by ob1quixote at 2:58 PM on May 10, 2012

« Older Are there differences in the e...   |  It's that time of year again .... Newer »
This thread is closed to new comments.