Wednesday, December 10, 2008

How to make a Twitter bot

Twitter bot is an application that automatically responds to users requests. It could be done in Twitter either via @reply postings or via direct messages. Here we will show how to use Twitter 411 service for your own bots creation. E.g. you can create there your own information system for the business or private purposes.

Twitter 411 uses direct messages transport. A potential user can follow to t411 and send direct messages to t411. The replies (responses) will come back as direct messages too. And users of Twitter 411 service define how to proceed requests. They can reserve some keywords as well as associated rules described how to proceed messages starting with that keyword.

As a method of message processing service users can choose an URL for some CGI script. This script will be responsible for the processing the incoming messages and producing the responses. Twitter 411 service works in this case as a daemon: gets a message, passes it to the appropriate CGI script, reads the response and sends it back. Let us see how does it looks like practically. Service Twitter 411 includes build-in bot lets you request stock quotes. Let us see its internals:

1. We've registered a keyword t (just a letter t). So all the direct messages starting with t (t will be processed by this bot.

2. Our bot expects messages in the following format t stock_symbol for getting the quote. For example:
t ORCL - quote for Oracle
or
t JAVA - quote for Sun

3. As a processing ngine for this bot we've set an URL for JSP file. It is our CGI script for the processing: http://linkstore.ru/t411/quote.jsp. For this bot our processing will be placed on the same server, but of course you can provide your own URL for any available server.

4. We can instruct Twitter 411 (our daemon) pass the whole incoming message to our CGI script. So in service's GUI we've set actually the following URL :
http://linkstore.ru/t411/quote.jsp?t=text
Service (daemon) will replace the variable text with the original request. For example, for the following incoming message t JAVA the final request to CGI script will looks so: http://linkstore.ru/t411/quote.jsp?t=t%20JAVA.

Now let us see the script itself. In our case it is JSP file expecting requests with the parameter t. And parameter's value will be in the following form t stock_symbol. So, our script (JSP file) will read parameter's value, extract stock symbol (part of the string after the space), request quote and print the result. It is all. For example:

<%@ page contentType="text/plain; charset=utf-8" %>
<%@ taglib uri="taglib27.tld" prefix="get" %>

<%
String t = request.getParameter("t");
if (t==null)
{ out.println("unknown");
return; }

// the pattern is: t <space> stock_symbol

int i = t.indexOf(" ");

if (i<=0)
{ out.println(t+"?? could not get ticket");
return; }

t = t.substring(i+1).trim();
%>

<get:Quote symbol="<%=t.toUpperCase()%>" id="A" />

<%=A.get(0)+": "+A.get(1)+" "+A.get(9)%>


Here our JSP file prints the quote and its chart (we've used custom tags from Coldtags suite)

In other words - all what do you need for your own Twitter bot is just an ordinary CGI script with your own processing. You can program your own script, reuse something existing component etc. And your Twitter bot could be placed on any web server with CGI scripts support.

6 comments:

Anonymous said...

This is neat. I was thinking of creating a Twitter Daemon that sits on a computer and automatically sends out updates.

Abava said...

>This is neat. I was thinking of
>creating a Twitter Daemon that sits
it is exactly what this programmable bot Twitter 411 is for.

Jonas Lejon said...

Thanks! I've just made a Twitter bot. More info http://download11.com/twitter

Wolf said...

Here is a YouTube friend Adder Bot for you.

http://www.ccvid.com/buy/yt-friend-adder/

Cheers
Wolf
http://icfun.blogspot.com

Konfig├╝rasyon M├╝hendisi said...

That's a good share.

But actually I am more interested in a mention bot.

For example, there is a Fight Club bot which mentions you a quote whenever you write "fight club" in your tweet. No matter you follow or mention it.

So I want to make its Dostoevsky version but really couldn't find a helpful tutorial. Any ideas about this?

How to make a Twitter bot said...

Just Repost - Thanks