0
irc 봇을 만들려고합니다. 그것은 연결하지만 완전한 메시지를 보내지 않습니다. "hello world"를 보내려면 "hello"만 보냅니다. 첫 번째 공간까지 모든 것을 보냅니다.소켓 irc 봇이 완료 메시지를 보내지 않습니다.
이 프로그램에서 irc에 hello를 입력하면 봇은 hello world를 전송하게됩니다. 그러나 그것은 단지 안녕하세요를 보냅니다.
import socket
channel = "#bots"
server = "chat.freenode.org"
nickname = "my_bot"
class IRC:
irc = socket.socket()
def __init__(self):
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def send(self, chan, msg):
self.irc.send("PRIVMSG " + chan + " " + msg + "\n")
def connect(self, server, channel, botnick):
# defines the socket
print("connecting to: " + server)
self.irc.connect((server, 6667)) # connects to the server
self.irc.send("NICK %s\n" % botnick)
self.irc.send("USER %s %s Ibot :%s\n" % (botnick, botnick, botnick))
self.irc.send("JOIN %s\n" % channel)
self.irc.send("PRIVMSG %s :Hello Master\n" % channel)
def get_text(self):
text = self.irc.recv(2040) # receive the text
if text.find('PING') != -1:
self.irc.send('PONG ' + text.split()[1] + 'rn')
return text
irc = IRC()
irc.connect(server, channel, nickname)
while True:
text = irc.get_text().strip()
if "hello" in text.lower():
irc.send(channel, "hello world")
print text