{"id":487,"date":"2016-04-04T19:00:00","date_gmt":"2016-04-04T19:00:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=487"},"modified":"2025-02-25T14:31:23","modified_gmt":"2025-02-25T14:31:23","slug":"irc-bot-using-python-2","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/irc-bot-using-python-2","title":{"rendered":"Create an IRC Bot Using Python 2"},"content":{"rendered":"\n<p>If you&#8217;re unfamiliar with IRC (Internet Relay Chat) it is an application layer protocol used to facilitate chat with users all over the globe and has been around since the late 1980s and is still widely used to this day.<\/p>\n\n\n\n<p>There are several different IRC networks, so which one you chose to use comes down to your own personal preference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"why-create-an-irc-bot\">Why Create an IRC Bot?<\/h2>\n\n\n\n<p>Simply put,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/IRC_bot\" target=\"_blank\" rel=\"noreferrer noopener\">IRC bots<\/a>&nbsp;in general are simple and fun to create. They allow you to extend the functionality of a channel or even create fun interactive games for your channel. Basically, the sky is the limit in terms of possibilities for bot functionality, and when mixed with the ease of Python you can create a complex bot in very few lines of code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"how-do-i-connect-to-the-irc-server\">How Do I Connect to the IRC Server?<\/h2>\n\n\n\n<p>The process for connecting to an IRC server is straight forward*.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Connect to XXXX server on port XXXX (typically 6667)<\/li>\n\n\n\n<li>Send the USER command<\/li>\n\n\n\n<li>Send the NICK command<\/li>\n\n\n\n<li>Respond to any PING commands<\/li>\n<\/ol>\n\n\n\n<p><em>* &#8211; It&#8217;s important to note that some IRC servers differ in how you connect or the format of messages received, be sure to look up the reference information for the host you normally connect to.<\/em><\/p>\n\n\n\n<p>Before connecting to the IRC server we&#8217;ll setup the variables for server, botnick, the channel to join on connect and the boolean flags for&nbsp;<em>sentUser<\/em>&nbsp;and&nbsp;<em>sentNick<\/em>. The&nbsp;<em>sentUser<\/em>&nbsp;and&nbsp;<em>sentNick<\/em>&nbsp;boolean flags are used to determine whether or not those commands have been previously sent to the server during connection.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server = \"ix.undernet.org\"\nchannel = \"#usa\"\nbotnick = \"uberbot\" + str(random.randint(1, 10000))\nsentUser = False\nsentNick = False<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Connect to XXXX server on port XXXX<\/h3>\n\n\n\n<p>To connect to a server a simple socket is created and then connected to by calling the connect method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nprint \"\\nConnecting to:\" + server\nirc.connect((server, 6667))<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Send the USER command<\/h3>\n\n\n\n<p>Next, register (and something authenticate) a user session with a short username and a human-readable realname. The command format looks like this.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">USER (username) (hostname) (unused) (realname)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if sentUser == False:\n   irc.send(\"USER \" + botnick + \" \" + botnick + \" \" + botnick + \" :This is a fun bot\\n\")\n   sentUser = True\n   continue<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Send the NICK command<\/h3>\n\n\n\n<p>After the USER command, send the NICK command to specify your chat nickname. The command format looks like this.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">NICK (nickname)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if sentUser and sentNick == False:\n\u00a0\u00a0\u00a0irc.send(\"NICK \"\u00a0+ botnick + \"\\n\")\n\u00a0\u00a0\u00a0sentNick = True\n\u00a0\u00a0\u00a0continue<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Respond to any PING commands<\/h3>\n\n\n\n<p>IRC servers regularly send the PING command to connected clients to determine if they are still connected. To show you are connected each PING must be answered with a PONG and the supplied PING token. The PING command uses the following format.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PING (token)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if text.find(\"PING\") != -1:\n\u00a0\u00a0\u00a0irc.send(\"PONG \" + text.split()[1] + \"\\n\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"how-does-my-bot-accept-commands\">How Does My Bot Accept Commands?<\/h2>\n\n\n\n<p>To have your bot accept commands, you&#8217;ll need to parse the input that comes from the buffer and then respond accordingly. Since IRC is text based the messages coming from the server are all easy to parse using regex or even a simple text find. The snippet below searches the message text from a channel or private message containing &#8220;!host&#8221;. If found, the bot responds with the OS version information.&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if text.find(\":!host\") != -1:\nirc.send(\"PRIVMSG \" + channel + \" :\" + str(platform.platform()) + \"\\n\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"can-i-just-have-the-codez\">Can I Just Have the Codez?<\/h2>\n\n\n\n<p>The source code below is the full listing of code used for this post.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import platform\nimport random\nimport socket\nimport sys\n\nreload(sys)\nsys.setdefaultencoding('utf8')\n\nserver = \"ix.undernet.org\"\nchannel = \"#usa\"\nbotnick = \"uberbot\" + str(random.randint(1, 10000))\nsentUser = False\nsentNick = False\n\nirc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nprint \"\\nConnecting to:\" + server\nirc.connect((server, 6667))\n\ntry:\n   while 1:\n      text = irc.recv(2048)\n      if len(text) > 0:\n         print text\n      else:\n         continue\n\n      if text.find(\"PING\") != -1:\n         irc.send(\"PONG \" + text.split()[1] + \"\\n\")\n\n      if sentUser == False:\n         irc.send(\"USER \" + botnick + \" \" + botnick + \" \" + botnick + \" :This is a fun bot\\n\")\n         sentUser = True\n         continue\n\n      if sentUser and sentNick == False:\n         irc.send(\"NICK \" + botnick + \"\\n\")\n         sentNick = True\n         continue\n\n      if text.find(\"255 \" + botnick) != -1:\n         irc.send(\"JOIN \" + channel + \"\\n\")\n\n      if text.find(\":!host\") != -1:\n         irc.send(\"PRIVMSG \" + channel + \" :\" + str(platform.platform()) + \"\\n\")\n\nexcept KeyboardInterrupt:\n   irc.send(\"QUIT :I have to go for now!\\n\")\n   print \"\\n\"\n   sys.exit()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"where-can-i-learn-more\">Where Can I Learn More?<\/h2>\n\n\n\n<p>The links below are great references for learning about IRC, IRC bots and the IRC protocol.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet_Relay_Chat\" target=\"_blank\" rel=\"noreferrer noopener\">Internet Relay Chat<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/IRC_bot\" target=\"_blank\" rel=\"noreferrer noopener\">IRC Bots<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/tools.ietf.org\/html\/rfc2812\" target=\"_blank\" rel=\"noreferrer noopener\">IRC Protocol RFC<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/plus.google.com\/114841922674980098581?rel=author\" rel=\"noopener\">By Torrey Betts<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re unfamiliar with IRC (Internet Relay Chat) it is an application layer protocol used to facilitate chat with users all over the globe and has been around since the late 1980s and is still widely used to this day. <\/p>\n","protected":false},"author":108,"featured_media":2370,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":2,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":2127,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/487\/revisions\/2127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2370"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}