<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python &#187; Networking</title>
	<atom:link href="/python/category/networking/feed/" rel="self" type="application/rss+xml" />
	<link>https://talkera.org/python</link>
	<description>Programming Blog</description>
	<lastBuildDate>Sun, 01 Feb 2015 17:15:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1</generator>
	<item>
		<title>Building an IRC (ro)bot</title>
		<link>https://talkera.org/python/building-an-irc-bot/</link>
		<comments>https://talkera.org/python/building-an-irc-bot/#comments</comments>
		<pubDate>Tue, 13 Jan 2015 15:53:53 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">https://talkera.org/python/?p=39</guid>
		<description><![CDATA[There are tons of (ro)bots out there for IRC (Internet Relay Chat). So how do you start and build one in Python, just for fun? You will need a program that connects with an IRC server and acts like a traditional IRC client.  IRC servers never ask for any type of complicated human verification such [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>There are tons of (ro)bots out there for <a href="https://en.wikipedia.org/wiki/Internet_Relay_Chat" target="_blank">IRC (Internet Relay Chat)</a>. So how do you start and build one in Python, just for fun?</p>
<p>You will need a program that connects with an IRC server and acts like a traditional IRC client.  IRC servers never ask for any type of complicated human verification such as solving captchas, which is why we can simply connect with a script. The script itself will use network<em> sockets,  </em>a library that is often used to provide network interactions in many programming languages including Python and C/C++.</p>
<p>To communicate with an IRC server, you need to use the IRC protocol.  The IRC protocol has distinct messages such as PRIVMSG, USER, NICK and JOIN. If you are curious, you could read<a href="http://www.irc.org/tech_docs/rfc1459.html" target="_blank"> the entire protocol</a>. But following this tutorial may be a lot simpler <img src="/python/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /> Authentication is achieved using only a few steps:</p>
<p>The IRC protocol is a layer on top of the IP protocol.   To create a socket we use the command:</p><pre class="crayon-plain-tag">irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</pre><p><code></code>socket.AF_INET tells the library to use the network protocol <a title="IPv4" href="https://en.wikipedia.org/wiki/IPv4">IPv4.</a>   The second argument tells the library to use stream sockets, which are traditionally implemented on the TCP protocol. (IRC works over TCP/IP). We then must use the commands to authenticate with the server:</p><pre class="crayon-plain-tag">USER botname botname botname: phrase
NICK botname
JOIN #channel</pre><p>Sometimes the IDENT command is neccesary too. Summing up, we get this class (save it as irc.py):</p><pre class="crayon-plain-tag">import socket
import sys


class IRC:

    irc = socket.socket()
  
    def __init__(self):  
        self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def send(self, chan, msg):
        self.irc.send("PRIVMSG " + chan + " " + msg + "\n")

    def connect(self, server, channel, botnick):
        #defines the socket
        print "connecting to:"+server
        self.irc.connect((server, 6667))                                                         #connects to the server
        self.irc.send("USER " + botnick + " " + botnick +" " + botnick + " :This is a fun bot!\n") #user authentication
        self.irc.send("NICK " + botnick + "\n")               
        self.irc.send("JOIN " + channel + "\n")        #join the chan

    def get_text(self):
        text=self.irc.recv(2040)  #receive the text

        if text.find('PING') != -1:                      
            self.irc.send('PONG ' + text.split() [1] + '\r\n') 

        return text</pre><p>Now that we have the network connectivity class, we can use it as an instance.  We will keep our (ro)bot simple for explanatory purposes. The bot will reply &#8220;Hello!&#8221; if it gets the message &#8220;hello&#8221; in the channel it resides.</p><pre class="crayon-plain-tag">from irc import *
import os
import random

channel = "#testit"
server = "irc.freenode.net"
nickname = "reddity"

irc = IRC()
irc.connect(server, channel, nickname)


while 1:
    text = irc.get_text()
    print text
 
    if "PRIVMSG" in text and channel in text and "hello" in text:
        irc.send(channel, "Hello!")</pre><p>Save it as bot.py and run with <em>python bot.py</em>. Connect with a traditional irc client (mirc,hexchat,irsii) to the the channel and observe the experiment has worked! You can now extend it with any cool features you can imagine.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;linkname=Building%20an%20IRC%20%28ro%29bot" title="Facebook" rel="nofollow" target="_blank"></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;linkname=Building%20an%20IRC%20%28ro%29bot" title="Twitter" rel="nofollow" target="_blank"></a><a class="a2a_button_google_plus" href="http://www.addtoany.com/add_to/google_plus?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;linkname=Building%20an%20IRC%20%28ro%29bot" title="Google+" rel="nofollow" target="_blank"></a><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;linkname=Building%20an%20IRC%20%28ro%29bot" title="Reddit" rel="nofollow" target="_blank"></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;linkname=Building%20an%20IRC%20%28ro%29bot" title="StumbleUpon" rel="nofollow" target="_blank"></a><a class="a2a_button_pinterest" href="http://www.addtoany.com/add_to/pinterest?linkurl=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;linkname=Building%20an%20IRC%20%28ro%29bot" title="Pinterest" rel="nofollow" target="_blank"></a><a class="a2a_dd a2a_target addtoany_share_save" href="https://www.addtoany.com/share_save#url=http%3A%2F%2Ftalkera.org%2Fpython%2Fbuilding-an-irc-bot%2F&amp;title=Building%20an%20IRC%20%28ro%29bot" id="wpa2a_2"></a></p>]]></content:encoded>
			<wfw:commentRss>https://talkera.org/python/building-an-irc-bot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
