我需要帮助编写一个基本的 IRC 机器人,它只连接到一个频道.. 有人能解释一下吗?我已经设法让它连接到 IRC 服务器,但我无法加入频道并登录。到目前为止我的代码是:
import sockethost = 'irc.freenode.org'
port = 6667
join_sock = socket.socket()
join_sock.connect((host, port))
<code here>
任何帮助将不胜感激。
要连接到 IRC 频道,您必须先向 IRC 服务器发送某些特定于 IRC 协议的命令,然后才能执行此操作。
当您连接到服务器时,您必须等到服务器发送完所有数据(MOTD 等),然后您必须发送 PASS 命令。
PASS <some_secret_password>
接下来是 NICK 命令。
NICK <username>
然后您必须发送 USER 命令。
USER <username> <hostname> <servername> :<realname>
两者都是强制性的。
然后你很可能会看到来自服务器的 PING 消息,每次服务器向你发送 PING 消息时,你必须用 PONG 命令回复服务器。服务器也可能会在 NICK 和 USER 命令之间请求 PONG。
PING :12345678
使用 PONG 命令在“PING”之后回复完全相同的文本:
PONG :12345678
PING 之后的内容对于我相信的每台服务器都是独一无二的,因此请确保您回复服务器发送给您的值。
现在您可以使用 JOIN 命令加入频道:
JOIN <#channel>
现在您可以使用 PRIVMSG 命令向频道和用户发送消息:
PRIVMSG <#channel>|<nick> :<message>
退出
QUIT :<optional_quit_msg>
尝试 Telnet!从...开始
telnet irc.example.com 6667
有关更多命令和选项,请参阅IRC RFC。
希望这可以帮助!
我用它作为主要的 IRC 代码:
import socket
import sys
server = "server" #settings
channel = "#channel"
botnick = "botname"
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
print "connecting to:"+server
irc.connect((server, 6667)) #connects to the server
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is a fun bot!\n") #user authentication
irc.send("NICK "+ botnick +"\n") #sets nick
irc.send("PRIVMSG nickserv :iNOOPE\r\n") #auth
irc.send("JOIN "+ channel +"\n") #join the chan
while 1: #puts it in a loop
text=irc.recv(2040) #receive the text
print text #print text to console
if text.find('PING') != -1: #check if 'PING' is found
irc.send('PONG ' + text.split() [1] + '\r\n') #returnes 'PONG' back to the server (prevents pinging out!)
然后,您可以开始设置命令,例如:!hi <nick>
if text.find(':!hi') !=-1: #you can change !hi to whatever you want
t = text.split(':!hi') #you can change t and to :)
to = t[1].strip() #this code is for getting the first word after !hi
irc.send('PRIVMSG '+channel+' :Hello '+str(to)+'! \r\n')
请注意,所有irc.send
文本必须以PRIVMSG
或NOTICE
+ channel/user
开头,并且文本应以:
!
将其基于 Twisted 的 IRC 协议实现可能是最简单的。看看: http: //github.com/brosner/bosnobot以获得灵感。
这是MichaelvdNet 的 Post的扩展,它支持一些额外的东西:
将文本文件的更改记录到频道
#!/usr/local/bin/python
import socket
import ssl
import time
## Settings
### IRC
server = "chat.freenode.net"
port = 6697
channel = "#meLon"
botnick = "meLon-Test"
password = "YOURPASSWORD"
### Tail
tail_files = [
'/tmp/file-to-tail.txt'
]
irc_C = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
irc = ssl.wrap_socket(irc_C)
print "Establishing connection to [%s]" % (server)
# Connect
irc.connect((server, port))
irc.setblocking(False)
irc.send("PASS %s\n" % (password))
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :meLon-Test\n")
irc.send("NICK "+ botnick +"\n")
irc.send("PRIVMSG nickserv :identify %s %s\r\n" % (botnick, password))
irc.send("JOIN "+ channel +"\n")
tail_line = []
for i, tail in enumerate(tail_files):
tail_line.append('')
while True:
time.sleep(2)
# Tail Files
for i, tail in enumerate(tail_files):
try:
f = open(tail, 'r')
line = f.readlines()[-1]
f.close()
if tail_line[i] != line:
tail_line[i] = line
irc.send("PRIVMSG %s :%s" % (channel, line))
except Exception as e:
print "Error with file %s" % (tail)
print e
try:
text=irc.recv(2040)
print text
# Prevent Timeout
if text.find('PING') != -1:
irc.send('PONG ' + text.split() [1] + '\r\n')
except Exception:
continue
这将打开一个套接字,但你还需要告诉 IRCd 你是谁。我很久以前在 perl 中做过类似的事情,我发现 IRC RFC 非常有帮助。
主要 RFC:http: //irchelp.org/irchelp/rfc/rfc.html
其他 RFC:http: //irchelp.org/irchelp/rfc/index.html