我如何在python中编写一个简单的IRC机器人?

Jak*_*ake 22 python sockets irc bots connect

我需要帮助编写一个只连接到频道的基本IRC机器人..是否有人能够解释我这个?我设法让它连接到IRC服务器但我无法加入频道并登录.我到目前为止的代码是:

import sockethost = 'irc.freenode.org'
port = 6667
join_sock = socket.socket()
join_sock.connect((host, port))
<code here> 
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

The*_*ian 49

要连接到IRC通道,必须先将某些特定于IRC协议的命令发送到IRC服务器,然后才能执行此操作.

当您连接到服务器时,您必须等到服务器发送了所有数据(MOTD和诸如此类),然后您必须发送PASS命令.

PASS <some_secret_password>
Run Code Online (Sandbox Code Playgroud)

接下来是NICK命令.

NICK <username>
Run Code Online (Sandbox Code Playgroud)

然后,您必须发送USER命令.

USER <username> <hostname> <servername> :<realname>
Run Code Online (Sandbox Code Playgroud)

两者都是强制性的

然后您可能会看到来自服务器的PING消息,每次服务器向您发送PING消息时,您都必须使用PONG命令回复服务器.服务器也可能在NICK和USER命令之间请求PONG.

PING :12345678
Run Code Online (Sandbox Code Playgroud)

使用PONG命令在"PING"后回复完全相同的文本:

PONG :12345678
Run Code Online (Sandbox Code Playgroud)

PING之后的内容对于我认为的每个服务器都是唯一的,因此请确保使用服务器发送给您的值进行回复.

现在您可以使用JOIN命令加入通道:

JOIN <#channel>
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用PRIVMSG命令向频道和用户发送消息:

PRIVMSG <#channel>|<nick> :<message>
Run Code Online (Sandbox Code Playgroud)

退出

QUIT :<optional_quit_msg>
Run Code Online (Sandbox Code Playgroud)

试用Telnet!从...开始

telnet irc.example.com 6667
Run Code Online (Sandbox Code Playgroud)

有关更多命令和选项,请参阅IRC RFC.

希望这可以帮助!

  • 嘿@Jake,如果这个答案帮助你那么多,为什么不选择他的答案是正确的? (4认同)
  • 谢谢,这太棒了!特别是有关telnet的提示..甚至没有想到它:)谢谢..我可能还有一些问题..让我尝试一下telnet的东西然后我会回来的! (2认同)

Mic*_*Net 17

我用它作为MAIN 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!)
Run Code Online (Sandbox Code Playgroud)

然后,您可以开始设置如下命令: !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')
Run Code Online (Sandbox Code Playgroud)

请注意,所有irc.send文本必须以PRIVMSGNOTICE + channel/user和文本应以开始:!


Ale*_*nor 12

将它基于twisted的IRC协议实现可能是最容易的.请查看:http://github.com/brosner/bosnobot获取灵感.

  • http://www.habnabit.org/twistedex.html是一个使用twp的基础IRC机器人教程 (2认同)