📜 ⬆️ ⬇️

ICQ Python bot

Hi % habrauser% ! This is my first habrotopik. In which I will tell you how to make an ICQ bot on pythone. The bot will sit in the network and send, at the user's request, a list of the latest articles of the habr.
To implement the bot, we will use the Twisted library. Of course, there are already specialized libraries for working with ICQ on python. Such as py-icq or nanoicq . But since I was already familiar with Twisted, the choice fell on him.

In order not to reinvent the wheel, take the lion's part of the program from the example :

# UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  1. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  2. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  3. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  4. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  5. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  6. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
  7. # UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .
# UIN UIN = "" # PASS = "" # host = ( "login.icq.com" , 5238) # / icqMode = 1 # Status message AMSG = "I'm here +)" * This source code was highlighted with Source Code Highlighter .


Here the initial part of us and ends. Download the necessary libraries.
')
  1. from twisted.words.protocols import oscar
  2. from twisted.internet import protocol, reactor
  3. import feedparser
  4. import urllib2 # Please do not bother with this. For what it is, I'll tell you later
* This source code was highlighted with Source Code Highlighter .


Feedparser we need to pull off and otparsit rss tape habr. Now the bot itself. Here, almost all of the example, with the exception of minor changes ...

  1. # Class Bot
  2. class Bot (oscar.BOSConnection):
  3. capabilities = [oscar.CAP_CHAT]
  4. def initDone (self):
  5. print "Connect" , UIN, "to server" , host [0], host [1]
  6. self.requestSelfInfo (). addCallback (self.gotSelfInfo)
  7. self.requestSSI (). addCallback (self.gotBuddyList)
  8. self.setAway (AMSG)
  9. def gotSelfInfo (self, user):
  10. print user .__ dict__
  11. self.name = user.name
  12. def gotBuddyList (self, l):
  13. print l
  14. self.activateSSI ()
  15. self.setProfile ( "" "ICQBot" "" )
  16. self.setIdleTime (0)
  17. self.clientReady ()
  18. def gotAway (self, away, user):
  19. if away:
  20. print "User" , user, ":" , away
* This source code was highlighted with Source Code Highlighter .


This is where we have the tastiest part. Inbound processing:

  1. def receiveMessage (self, user, multiparts, flags):
  2. print "\ n <From:" , user.name
  3. print "<Message:" , multiparts [0] [0] .decode ( 'cp1251' )
  4. command = multiparts [0] [0] .lower (). split ( '' ) # split the message by spaces
  5. PREF_C = "!" # teams will start with us!
* This source code was highlighted with Source Code Highlighter .


We will have the first team! Habr, but there are some minor exceptions. If the command syntax! Habr n, then the last n posts will be displayed. If the syntax command is! Habr n1 n2, then from n1 to n2 posts (my little whim). Of course, the output is limited to 20 posts, because In their xml page it is exactly 20.

  1. if command [0] == (PREF_C + "habr" ):
  2. rss = feedparser.parse ( 'http://habrahabr.ru/rss' ) # pick up the xml page and parsim
  3. feeds = [0.20] # This is for additional functionality here)
  4. if len (command) == 2:
  5. try :
  6. feeds [1] = int (command [1])
  7. except:
  8. feeds [1] = 0
  9. if feeds [1] <1: feeds [1] = 1
  10. if feeds [1]> 20: feeds [1] = 20
  11. elif len (command) == 3:
  12. try :
  13. feeds [0] = int (command [1]) - 1
  14. feeds [1] = int (command [2])
  15. except:
  16. feeds [0] = 0
  17. feeds [1] = 19
  18. if feeds [0] <0: feeds [0] = 0
  19. if feeds [0]> 19: feeds [0] = 19
  20. if feeds [1] <1: feeds [1] = 1
  21. if feeds [1]> 20: feeds [1] = 20
  22. mes = ''
  23. for feed in range (feeds [0], feeds [1]):
  24. mes + = rss.entries [feed] .title + '\ n'
  25. self.sendMessage (user.name, mes)
* This source code was highlighted with Source Code Highlighter .


The command! Habrn n will display the contents of the description field of the n field.

  1. elif command [0] == (PREF_C + "habrn" ):
  2. rss = feedparser.parse ( 'http://habrahabr.ru/rss' )
  3. try :
  4. feedn = int (command [1]) - 1
  5. except:
  6. feedn = 0
  7. if feedn <0: feedn = 0
  8. if feedn> 19: feedn = 19
  9. self.sendMessage (user.name, rss.entries [feedn] .title + '\ n' + rss.entries [feedn] .description)
* This source code was highlighted with Source Code Highlighter .


Here I want to mention import urllib2, which I entered at the beginning. Sometimes I need to know the ip of my home server in order to log in via ssh. And of course, I would not want anyone else to know his address.

  1. elif command [0] == (PREF_C + "ip" ) and user.name == '368576236' :
  2. file_s = urllib2.urlopen (urllib2.Request ( 'http://api.wipmania.com/' ))
  3. response = file_s.read ()
  4. self.sendMessage (user.name, response.split ( "<br>" ) [0])
  5. f.close ()
* This source code was highlighted with Source Code Highlighter .


If you look at http://api.wipmania.com/ , then you can find a pretty simple html page which will have our external ip.

Well, the final part:

  1. class BotAuth (oscar.OscarAuthenticator):
  2. BOSClass = Bot
  3. protocol.ClientCreator (reactor, BotAuth, UIN, PASS, icq = icqMode) .connectTCP (* host)
  4. reactor.run ()
* This source code was highlighted with Source Code Highlighter .

That's all. I want to say thanks to the person who helped get the ip for the last command.

Here is the whole code:

  1. # - * - coding: utf-8 - * -
  2. # ICQ bot
  3. #Uin
  4. UIN = "123456"
  5. PASS = "pass"
  6. # Server
  7. host = ( "login.icq.com" , 5238)
  8. icqMode = 1
  9. # Status message
  10. AMSG = "I'm here +)"
  11. from twisted.words.protocols import oscar
  12. from twisted.internet import protocol, reactor
  13. import feedparser
  14. import urllib2
  15. # Class Bot
  16. class Bot (oscar.BOSConnection):
  17. capabilities = [oscar.CAP_CHAT]
  18. def initDone (self):
  19. print "Connect" , UIN, "to server" , host [0], host [1]
  20. self.requestSelfInfo (). addCallback (self.gotSelfInfo)
  21. self.requestSSI (). addCallback (self.gotBuddyList)
  22. self.setAway (AMSG)
  23. def gotSelfInfo (self, user):
  24. print user .__ dict__
  25. self.name = user.name
  26. def gotBuddyList (self, l):
  27. print l
  28. self.activateSSI ()
  29. self.setProfile ( "" "ICQBot" "" )
  30. self.setIdleTime (0)
  31. self.clientReady ()
  32. def gotAway (self, away, user):
  33. if away:
  34. print "User" , user, ":" , away
  35. def receiveMessage (self, user, multiparts, flags):
  36. print "\ n <From:" , user.name
  37. print "<Message:" , multiparts [0] [0] .decode ( 'cp1251' )
  38. command = multiparts [0] [0] .lower (). split ( '' )
  39. PREF_C = "!"
  40. if command [0] == (PREF_C + "habr" ):
  41. rss = feedparser.parse ( 'http://habrahabr.ru/rss' )
  42. feeds = [0.20]
  43. if len (command) == 2:
  44. try :
  45. feeds [1] = int (command [1])
  46. except:
  47. feeds [1] = 0
  48. if feeds [1] <1: feeds [1] = 1
  49. if feeds [1]> 20: feeds [1] = 20
  50. elif len (command) == 3:
  51. try :
  52. feeds [0] = int (command [1]) - 1
  53. feeds [1] = int (command [2])
  54. except:
  55. feeds [0] = 0
  56. feeds [1] = 19
  57. if feeds [0] <0: feeds [0] = 0
  58. if feeds [0]> 19: feeds [0] = 19
  59. if feeds [1] <1: feeds [1] = 1
  60. if feeds [1]> 20: feeds [1] = 20
  61. mes = ''
  62. for feed in range (feeds [0], feeds [1]):
  63. mes + = rss.entries [feed] .title + '\ n'
  64. self.sendMessage (user.name, mes)
  65. elif command [0] == (PREF_C + "habrn" ):
  66. rss = feedparser.parse ( 'http://habrahabr.ru/rss' )
  67. try :
  68. feedn = int (command [1]) - 1
  69. except:
  70. feedn = 0
  71. if feedn <0: feedn = 0
  72. if feedn> 19: feedn = 19
  73. self.sendMessage (user.name, rss.entries [feedn] .title + '\ n' + rss.entries [feedn] .description)
  74. elif command [0] == (PREF_C + "ip" ) and user.name == '123456' :
  75. file_s = urllib2.urlopen (urllib2.Request ( 'http://api.wipmania.com/' ))
  76. response = file_s.read ()
  77. self.sendMessage (user.name, response.split ( " <br> " ) [0])
  78. f.close ()
  79. class BotAuth (oscar.OscarAuthenticator):
  80. BOSClass = Bot
  81. protocol.ClientCreator (reactor, BotAuth, UIN, PASS, icq = icqMode) .connectTCP (* host)
  82. reactor.run ()
* This source code was highlighted with Source Code Highlighter .


Thanks for attention!

Source: https://habr.com/ru/post/111263/


All Articles