In this tutorial, you’ll learn to use Python 3 for creating an IRC bot. IRC is an acronym for Internet Relay Chat which is a popular form of communication to send text messages over the network.
How to Create an IRC Bot in Python
We’ll use Python socket programming to create the IRC program. However, it’s first nice to learn what is an IRC bot and what does it do? So, let’s catch up on some of its technical details. It will help us write the IRC bot program code.
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. Many programming languages like Python and Java aid in developing IRC bots.
Learn more from here about IRC Bot
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:
How to Implement IRC Bot 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 the 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
Find the Python code of the IRC bot in the below section.
Class File
First, you need to create an IRC bot class. Copy the below code paste it into 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, chn, msg):
# Transfer data
self.irc.send(bytes("PRIVMSG " + chn + " " + msg + "\n", "UTF-8"))
def connect(self, server, port, chn, 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 + " :python\n", "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 " + chn + "\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") [1] + '\r\n', "UTF-8"))
return resp
After creating the network communication class, we’ll import it into 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.
Also Read: A Simple IRC Bot Built in Java
Client Script
Below is the Python IRC Bot program to start the client communication. Create a new file, copy the code, paste it, and save it 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:
python irc_bot.py
We hope the above tutorial was simple enough to teach you how to build an IRC bot with multiple features and different usage.
To learn more, go through our step-by-step tutorials from the below link and take your Python programming skills to a notch further.