Ramblurr / rbot-alice

Alice chatbot plugin for rbot.

This URL has Read+Write access

rbot-alice / alice.rb
100644 103 lines (90 sloc) 2.872 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#-- vim:sw=2:et
#++
#
# :title: A.L.I.C.E chatbot plugin
#
# Author:: Casey Link <unnamedrambler@gmail.com>
# Copyright:: (C) 2008 Casey Link
#
# Uses www.pandorabots.com to "unleash virtual personalities" on irc.
 
class AlicePlugin < Plugin
  Config.register Config::BooleanValue.new('alice.enabled',
    :default => false,
    :desc => "Enable and disable the plugin")
  Config.register Config::StringValue.new('alice.botid',
    #:default => "a211adf15e36b67b",
    :default => "923c98f3de35606b", #God
    :desc => "The pandorabot.com botid.")
 
  def initialize
    super
    @custid = nil
    class << @registry
      def store(val)
        val
      end
      def restore(val)
        val
      end
    end
  end
 
  def help(plugin, topic="")
    "alice plugin: a chatbot that uses bots from pandorabot.com. usage: alice <statement> will send the statement to the bot. alice enable/disable will toggle the bot's responses on or off. alice status to see the status of the bot. to change which bot is being talked to edit the alice.botid config value."
  end
 
  def ask_question(m, params)
    return unless @bot.config['alice.enabled']
    return unless @bot.config['alice.botid']
    question = params[:question].to_s
    botid = @bot.config['alice.botid']
    uri = "http://www.pandorabots.com/pandora/talk-xml"
    botid = "botid=#{botid}"
    input = "input=#{CGI.escape(question)}"
    
    body = nil
    if @registry.has_key?( m.sourcenick )
      custid = "custid=#{@registry[ m.sourcenick ]}"
      body = [botid,custid,input].join("&")
    else
      body = [botid,input].join("&")
    end
 
    response = @bot.httputil.post(uri, body)
    debug response
    if response.class == Net::HTTPOK
      xmlDoc = REXML::Document.new(response.body)
      status = xmlDoc.elements["result"].attributes["status"]
      custid = xmlDoc.elements["result"].attributes["custid"]
      unless @registry.has_key?( m.sourcenick )
        @registry[ m.sourcenick ] = custid
      end
      case status
      when "0"
        m.reply xmlDoc.elements["result/that"].get_text.value.ircify_html
      else
        m.reply "Say again?"
      end
    else
      m.reply "Excuse me?" # the http request failed
    end
  end
 
  def enable(m, params)
    @bot.config['alice.enabled'] = true
    m.okay
  end
 
  def disable(m, params)
    @bot.config['alice.enabled'] = false
    m.okay
  end
 
  def status(m, params)
    msg = "Alice is "
    if @bot.config['alice.enabled']
      msg << "enabled. "
    else
      msg << "disabled. "
    end
    msg << "Currently using bot #{@bot.config['alice.botid']}."
    m.reply msg
  end
 
end
 
plugin = AlicePlugin.new
plugin.map 'alice enable', :action => "enable"
plugin.map 'alice disable', :action => "disable"
plugin.map 'alice status', :action => "status"
plugin.map 'alice *question', :action => "ask_question", :threaded => true