Skip to content

Instantly share code, notes, and snippets.

@justinwoo
Last active December 10, 2015 09:38
Show Gist options
  • Save justinwoo/4415402 to your computer and use it in GitHub Desktop.
Save justinwoo/4415402 to your computer and use it in GitHub Desktop.
A stupid IRC bot made in Python that just echoes back what is said. To be developed for actual applications later. Also hangs waiting for server responses, so... yeah. Not very smart.
#! /usr/bin/env python
# Justin Woo <moomoowoo@gmail.com>
# Usage:
# usage: chatbot.py [server] [nick] [channel]
# optional:
# -h, --help help
# required:
# [server] server to connect to
# [nickname] nickname to use
# [channel] channel to join
import argparse, sys, os
import socket
def main(args):
server = args.server
nickname = args.nickname
channel = args.channel
ident = nickname
port = 6667
s = socket.socket()
s.connect((server,port))
s.send('NICK ' + nickname + '\n')
s.send('USER ' + ident + ' ' + server + ' bla :' + 'james' + '\n')
while 1:
line = s.recv(500)
if line != '':
print line.rstrip()
if line.find('elcome') != -1:
outmsg = 'JOIN ' + channel + '\n'
print 'Me: ' + outmsg
s.send(outmsg)
if line.find('PING') != -1:
line = line.rstrip()
line = line.split(' ')
outmsg = 'PONG ' + line[1] + '\n'
print 'Me: ' + outmsg
s.send(outmsg)
elif line.find('PRIVMSG') != -1:
outmsg = parsemsg(line)
if outmsg != '':
s.send(outmsg)
def parsemsg(line):
#:nick!username@host PRIVMSG channel/nick :Message
line = line.rstrip()
line = line.split(' ')
print line
sender = line[0].split('!')[1:]
msg = line[3:]
channel = line[2]
outmsg = 'PRIVMSG ' + channel + ' :You said: ' + ' '.join(msg) + '\n'
return outmsg
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('server', help = 'server to connect to')
parser.add_argument('nickname', help = 'nickname to set')
parser.add_argument('channel', help = 'channel to join')
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment