-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
87 lines (65 loc) · 2.23 KB
/
bot.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
81
82
83
84
85
86
87
#!/usr/bin/env python
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
import sys
from chatterbotapi import ChatterBotFactory, ChatterBotType
factory = ChatterBotFactory()
sessions = {}
class GCIBot(irc.IRCClient):
nickname = 'rengar'
username = 'rengar'
password = 'irodriguez'
def __init__(self):
self.channels = []
def connectionMade(self):
irc.IRCClient.connectionMade(self)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
def signedOn(self):
for c in self.factory.channels:
self.join(c)
def joined(self, channel):
self.channels.append(channel)
def privmsg(self, user, channel, msg):
user_ = user.split('!', 1)[0]
isForMe = msg.startswith(
self.nickname +
":") or msg.startswith(
self.nickname +
",") or msg.startswith(
self.nickname +
" ")
if channel not in sessions:
sessions[
channel] = [
factory.create(
ChatterBotType.PANDORABOTS,
'b0dafd24ee35a477')]
sessions[channel].append(sessions[channel][0].create_session())
if not isForMe:
return
if msg[len(self.nickname) + 2:] == "the msg":
self.msg(channel, "%s, blabla" % user_)
else:
self.msg(channel, str(sessions[channel][1].think(str(msg))))
def alterCollidedNick(self, nickname):
return '_' + nickname + '_'
class BotFactory(protocol.ClientFactory):
def __init__(self, channels):
self.channels = channels
def buildProtocol(self, addr):
p = GCIBot()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
if __name__ == '__main__':
f = BotFactory(sys.argv[1:])
reactor.connectTCP("irc.freenode.net", 6667, f)
print "Connected to server. Channels:"
for channel in sys.argv[1:]:
print channel
reactor.run()