dtalley11

bot.py

Feb 11th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import socket
  2. import re
  3. import urllib2
  4. import bitly
  5. from bs4 import BeautifulSoup
  6.  
  7. api = bitly.Api(login='BITLY LOGIN', apikey='BITLY KEY') # bitly information
  8. server = "irc.messwithus.com"   # Server
  9. channel = "#chantobother"       # Channel
  10. botnick = "NICK"                # Your bots nick
  11. pref = "!"                      # Command Prefix
  12. port = 6667                     # Port used to connect with
  13.  
  14. def commands(nick,channel,message):
  15.    if message.find("http")!=-1: #when http is found
  16.       find_urls(nick,channel,message)
  17.  
  18. def version(nick):
  19.    if message.find("VERSION")!=-1:
  20.       ircsock.send('NOTICE : '+nick+' IRC BOT \n')
  21.  
  22. def ping(): # This is our first function! It will respond to server Pings.
  23.   ircsock.send("PONG :Pong\n")
  24.  
  25. def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
  26.   ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
  27.  
  28. def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
  29.   ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
  30.  
  31. def joinchan(chan): # This function is used to join channels.
  32.   ircsock.send("JOIN "+ chan +"\n")
  33.  
  34. def find_urls(nick,channel,message):
  35.     """ Extract all URL's from a string & return as a list """
  36.  
  37.     url_list = re.findall("(?P<url>https?://[^\s]+)",message) #look for url
  38.     short=api.shorten(url_list)                               #send url to api
  39.     site = ''.join(url_list)                                  #join list of urls from chat
  40.     shortstr = ''.join(short)                                 #Join list of urls from bitly
  41.     content = urllib2.urlopen(site).read()                    #Read the site associated with the URL
  42.     soup = BeautifulSoup(content)
  43.     titl = soup.title.string                                  #Set titl as what was in the title tag
  44.     ircsock.send('PRIVMSG %s :%s : %s\r\n' % (channel,shortstr,titl)) #say short url and title
  45.  
  46. ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  47. ircsock.connect((server, port)) # Here we connect to the server using port 6667
  48. ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :Hey\n") # user authentication
  49. ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
  50.  
  51. joinchan(channel) # Join the channel using the functions we previously defined
  52.  
  53. while 1: # Be careful with these! It might send you to an infinite loop
  54.   ircmsg = ircsock.recv(2048) # receive data from the server
  55.   ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
  56.   print(ircmsg) # Here we print what's coming from the server
  57.   if ircmsg.find(' PRIVMSG ')!=-1:
  58.        nick=ircmsg.split('!')[0][1:]
  59.        channel=ircmsg.split(' PRIVMSG ')[-1].split(' :')[0]
  60.        commands(nick,channel,ircmsg)
  61.   if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
  62.     hello()
  63.   if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
  64.     ping()
Advertisement
Add Comment
Please, Sign In to add comment