简体   繁体   中英

Simple Python IRC bot for reporting to a channel

I'm trying to find out the most simple way to report some data in an IRC channel. My bot does not need to receive any commands or handle any input. It's simply a part of a different program that gathers data and simply outputs it to an IRC channel. I've looked at the code at O'Reilly and was wondering if the while part could be skipped. However, then there is nothing keeping the bot alive and always logged in. How would I keep it logged on and basically strip away the receive handling code?

Edit: I also took a look at Willie but it seems monstrous for the use case I want. Is there any stripped down version that can be used just for the IRC communication?

How often do you need to send data? If it is not that often you could do it like this:

import sys
import socket
import string

HOST="irc.freenode.net"
PORT=6667
NICK="MauBot"
IDENT="maubot"
REALNAME="MauritsBot"

def sendData(dataString):
    s=socket.socket( )
    s.connect((HOST, PORT))
    s.send("NICK %s\r\n" % NICK)
    s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
    s.send("%s\r\n" % dataString)
    s.close()

Whenever you have new Data from another skrip you can call the sendData Function and pass the Data as a string. Die function opens up the socket send the data and closes the socket.

This is exactly why I wrote simple_irc . It'll handle the necessary reading to get you into the channel and keep the connection alive, but can send messages asynchronously. Try it out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM