Version 2, last updated by Jonathan Ferguson at May 31, 2010 UTC

Next consider a poll where the options are either unknown or to numerous to list. For example, "What is your favourite colour?"

The set up instructions are the same as the previous example. However now a single text-in word is created and the webhook URL has changed to http://taykt-demo.appspot.com/open?name=colour

class OpenPollServlet extends PollTrait {

    override def doPost(request:HttpServletRequest , response:HttpServletResponse ) {
        val pollName = request.getParameter("name")
        response.setContentType("text/plain")
        response.setCharacterEncoding("utf-8")
        if (null == pollName) 
            response.getWriter().println("No poll name provided, contact Poll organiser")
        else {
             pollOptionFromString(request.getParameter("text")) match {
            case None             => response.getWriter().println("You didn't say what you wanted to vote for!")
            case Some(pollOption) => 
                polls.get(pollName) match {
                    case None => polls += (pollName -> Map(pollOption -> 1)) // new poll
                    case Some(poll) =>  //existing poll
                    poll.get(pollOption) match {
                        case Some(value) => poll += (pollOption -> (value + 1))  // existing poll option 
                        case None      => poll += (pollOption -> 1)            // new poll option  
                }
            }
           response.getWriter().println(sMSResponse(pollName, pollOption))
            }
        }
    } 

    override def pollOptionFromString(raw:String):Option[String] = raw match {
      case null   => None
      case option => Some(option.split(" ").slice(0,2).reverse(0))
    }

}

To view how a poll is going, point your browser at http://taykt-demo.appspot.com/poll?name=pollname, so for the example above it would be http://taykt-demo.appspot.com/open?name=colour

You can try this particular service out with the web hook URL of: http://taykt-demo.appspot.com/open?name=colour

Example call:

$ curl http://taykt-demo.appspot.com/open?name=election -X POST -d text="colour orange" -d pid="abc123"