How to Use Python to Build an IRC Bot?
What is an IRC Bot?
A bot is a virtual assistant that emulates a real user to provide instant responses. IRC bot is a type of network client that could be a script or a program that can relay messages using the IRC protocol.
When any active user receives a text from the IRC bot, it appears to him as another real user.
What can a Bot do?
The bots mimic a real user and communicate with other active clients. However, they can perform a variety of tasks:
- Archive chat messages
- Can parse Twitter feeds
- Crawl the web for a keyword
- Run any command if needed.
How to Implement IRC in Python?
For this, we’ll need a Python program that creates a client socket to connect to the IRC server. The IRC server performs a simple verification and connects without much hassle.
The script uses Python socket library to allow network communication. Check the below sample code.
import socket ircbot = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The IRC protocol is just a layer above the IP protocol and works over the TCP/IP stack.
We’ll need our program to exchange the following set of commands.
** Authetication **** USER botname botname botname: text NICK botname NICKSERV IDENTIFY botnickpass botpass ** Join Channel **** JOIN
Bot Source Code
First, you need to create an IRC bot class. Copy the below code and paste in a file and save it as the irc_class.py.
import socket import sys import time class IRC: irc = socket.socket() def __init__(self): # Define the socket self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def send(self, channel, msg): # Transfer data self.irc.send(bytes("PRIVMSG " + channel + " " + msg + "n", "UTF-8")) def connect(self, server, port, channel, botnick, botpass, botnickpass): # Connect to the server print("Connecting to: " + server) self.irc.connect((server, port)) # Perform user authentication self.irc.send(bytes("USER " + botnick + " " + botnick +" " + botnick + " :pythonn", "UTF-8")) self.irc.send(bytes("NICK " + botnick + "n", "UTF-8")) self.irc.send(bytes("NICKSERV IDENTIFY " + botnickpass + " " + botpass + "n", "UTF-8")) time.sleep(5) # join the channel self.irc.send(bytes("JOIN " + channel + "n", "UTF-8")) def get_response(self): time.sleep(1) # Get the response resp = self.irc.recv(2040).decode("UTF-8") if resp.find('PING') != -1: self.irc.send(bytes('PONG ' + resp.split().decode("UTF-8")  + 'rn', "UTF-8")) return resp
After creating the network communication class, we’ll import t in our client and use its instance. We are designing a demo client so that you can understand it easily.
Our bot will send the “Hello!” message while responding to a “Hello” message on the channel.
Below the Python IRC Bot program to start the client communication. Create a new file, copy the code, paste it, and save as irc_bot.py.
IRC server usually runs on ports like 6667 or 6697 (IRC with SSL). So, we’ll be using “6667” in our sample. Also, you will need to provide a valid IRC Server IP address or hostname to make this program run correctly.
from irc_class import * import os import random ## IRC Config server = "10.x.x.10" # Provide a valid server IP/Hostname port = 6697 channel = "#python" botnick = "techbeamers" botnickpass = "guido" botpass = "<%= @guido_password %>" irc = IRC() irc.connect(server, port, channel, botnick, botpass, botnickpass) while True: text = irc.get_response() print(text) if "PRIVMSG" in text and channel in text and "hello" in text: irc.send(channel, "Hello!")
Please note that you can run the above program using the following command:
Hope, the above tutorial could help you build a more complex IRC bot with more features and usage.
If you wish to learn Python programming from scratch, then read this Python Tutorial.