Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from glob import glob
- import threading
- import websocket
- import logging
- import random
- import time
- import json
- import ssl
- class HackChatBot(threading.Thread):
- def __init__(self, botname, chat_room):
- self.botname = botname
- self.chat_room = chat_room
- self.users = {}
- self.cmd_syntax = "!"
- self.ws = None
- threading.Thread.__init__(self)
- def onlineSet(self, data):
- for user in data['nicks']:
- self.users[user] = "stuff"
- def onlineRemove(self, data):
- del self.users[data['nick']]
- print "User left "+ data['nick']
- def onlineAdd(self, data):
- self.users[data['nick']] = "stuff"
- print "User joined: "+data['nick']
- def handleInfo(self, data):
- pass
- def getBetween(self, data, first, last):
- return data.split(first)[1].split(last)[0]
- # websockets handlers
- def ping(self):
- self.sendJSON({"cmd":"ping"})
- threading.Timer(30.0, self.ping).start()
- def sendJSON(self, data):
- self.ws.send(json.dumps(data))
- def sendMessage(self, msg):
- self.ws.send(json.dumps({"cmd":"chat", "text":msg}))
- def on_error(self, wbsk, error):
- print error
- def on_close(self, wbsk):
- print "closed"
- def on_open(self, wbsk):
- self.ws.send(json.dumps({"cmd": "join", "channel": self.chat_room, "nick": self.botname}))
- self.ping()
- def on_message(self, wbsk, message):
- data = json.loads(message)
- cmd = data['cmd']
- print data['nick'] + ' : ' + data['text']
- def mandar(self):
- while True:
- mens = raw_input('>>')
- self.sendMessage(mens)
- def run(self):
- self.ws = websocket.WebSocketApp("wss://hack.chat/chat-ws", on_message = self.on_message, on_error = self.on_error, on_close = self.on_close)
- self.ws.on_open = self.on_open
- self.ws.run_forever(sslopt={"cert_reqs":ssl.CERT_NONE})
- bot = HackChatBot("teste", "dasoe02032")
- bot.start()
- bot.mandar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement