-
Notifications
You must be signed in to change notification settings - Fork 9
/
GrokItBot.py
executable file
·80 lines (63 loc) · 2.14 KB
/
GrokItBot.py
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
#!/usr/bin/env python
from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor
from AIMLBot import AIMLBot
import sys
import re
class GrokItBot(irc.IRCClient):
"""
An AIM bot that uses both PyAIML and Reverend Bayes
parser to learn and respond to messages.
Duncan Gough 13/03/04
- Updated to switch form TocTalk/AIM to Twisted/IRC
Duncan Gough 11/01/09
PyAIML: http://pyaiml.sourceforge.net/
Reverend Bayes: http://www.divmod.org/Home/Projects/Reverend/
Twisted: http://twistedmatrix.com/
GrokItBot: http://www.suttree.com/code/grokitbot/
"""
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
self.join(self.factory.channel, self.factory.password)
print "Signed on as %s." % (self.nickname,)
def joined(self, channel):
print "Joined %s." % (channel,)
def privmsg(self, user, channel, msg):
if msg.startswith('*** '):
print msg
return
elif not user:
return
elif self.nickname in msg:
msg = re.compile(self.nickname + "[:,]* ?", re.I).sub('', msg)
prefix = "%s: " % (user.split('!', 1)[0], )
else:
prefix = ''
sentence = self.factory.aiml.on_MSG_IN(user.split('!', 1)[0],msg)
self.msg(self.factory.channel, prefix + sentence)
class GrokItBotFactory(protocol.ClientFactory):
protocol = GrokItBot
def __init__(self, channel, nickname, password=''):
self.channel = channel
self.nickname = nickname
self.password = password
self.aiml = AIMLBot(self.nickname)
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s), reconnecting." % (reason,)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
if __name__ == "__main__":
if len(sys.argv) > 1:
nickname = sys.argv[1]
channel = sys.argv[2]
password = sys.argv[3]
else:
nickname = 'GrokItBot'
channel = 'suttree.com'
password = ''
reactor.connectTCP('irc.pmog.com', 6667, GrokItBotFactory('#' + channel, nickname, password))
reactor.run()