Wednesday, June 15, 2011

How to make a bot for Twitter

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.

411 for Twitter uses Twitter as transport layer. A potential user can follow to @t411 and send direct messages to @t411 or simply post a reply. The responses will come back as replies or 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 LNKD - quote for Linkedin

3. As a processing engine 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 ORCL the final request to CGI script will looks so: http://linkstore.ru/t411/quote.jsp?t=t%20ORCL.

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.

No comments: