Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1.  
  2. from glob import glob
  3. import threading
  4. import websocket
  5. import logging
  6. import random
  7. import time
  8. import json
  9. import ssl
  10.  
  11.  
  12.  
  13.  
  14. class HackChatBot(threading.Thread):
  15.     def __init__(self, botname, chat_room):
  16.  
  17.         self.botname = botname
  18.         self.chat_room = chat_room
  19.         self.users = {}
  20.         self.cmd_syntax = "!"
  21.         self.ws = None
  22.         threading.Thread.__init__(self)
  23.  
  24.  
  25.     def onlineSet(self, data):
  26.         for user in data['nicks']:
  27.             self.users[user] = "stuff"
  28.  
  29.     def onlineRemove(self, data):
  30.         del self.users[data['nick']]
  31.         print "User left "+ data['nick']
  32.  
  33.     def onlineAdd(self, data):
  34.         self.users[data['nick']] = "stuff"
  35.         print "User joined: "+data['nick']
  36.  
  37.     def handleInfo(self, data):
  38.         pass
  39.  
  40.     def getBetween(self, data, first, last):
  41.         return data.split(first)[1].split(last)[0]
  42.  
  43. # websockets handlers
  44.     def ping(self):
  45.         self.sendJSON({"cmd":"ping"})
  46.         threading.Timer(30.0, self.ping).start()
  47.  
  48.     def sendJSON(self, data):
  49.         self.ws.send(json.dumps(data))
  50.  
  51.     def sendMessage(self, msg):
  52.         self.ws.send(json.dumps({"cmd":"chat", "text":msg}))
  53.  
  54.     def on_error(self, wbsk, error):
  55.         print error
  56.  
  57.     def on_close(self, wbsk):
  58.         print "closed"
  59.  
  60.     def on_open(self, wbsk):
  61.         self.ws.send(json.dumps({"cmd": "join", "channel": self.chat_room, "nick": self.botname}))
  62.         self.ping()
  63.  
  64.     def on_message(self, wbsk, message):
  65.         data = json.loads(message)
  66.         cmd = data['cmd']
  67.         print data['nick'] + ' : ' + data['text']
  68.  
  69.     def mandar(self):
  70.         while True:
  71.             mens = raw_input('>>')
  72.             self.sendMessage(mens)
  73.  
  74.  
  75.     def run(self):
  76.         self.ws = websocket.WebSocketApp("wss://hack.chat/chat-ws", on_message = self.on_message, on_error = self.on_error, on_close = self.on_close)
  77.         self.ws.on_open = self.on_open
  78.         self.ws.run_forever(sslopt={"cert_reqs":ssl.CERT_NONE})
  79.  
  80. bot = HackChatBot("teste", "dasoe02032")
  81. bot.start()
  82. bot.mandar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement