def on_message_received(self, new_messages): for message in new_messages: if message.type == 'chat': write_log( 'Socket-Info', "New message '{}' received from number {}".format( message.type, message.sender.id)) socketIO.emit('newMessage', { 'chat': message.sender.id, 'message': message.content }) else: write_log( 'Socket-Info', "New message of type '{}' received from number {}".format( message.type, message.sender.id)) if message.type == 'image': content = str(message.save_media(pathSource, True)) socketIO.emit( 'newMessage', { 'chat': message.sender.id, 'message': content, 'type': 'img', 'caption': message.caption }) elif message.type == 'video': content = str(message.save_media(pathSource, True)) socketIO.emit( 'newMessage', { 'chat': message.sender.id, 'message': content, 'type': 'video', 'caption': message.caption }) elif message.type == 'document': content = str(message.save_media(pathSource, True)) socketIO.emit( 'newMessage', { 'chat': message.sender.id, 'message': content, 'type': 'file', 'caption': message.caption }) elif message.type == 'audio' or message.type == 'ptt': content = str(message.save_media(pathSource, True)) os.rename(content, content + '.ogg') socketIO.emit( 'newMessage', { 'chat': message.sender.id, 'message': content + '.ogg', 'type': 'ogg', 'caption': message.caption }) else: socketIO.emit( 'newMessage', { 'chat': message.sender.id, 'message': 'Contenido No soportado' })
def on_connect(): global awaitLogin, driver, wsp write_log('Socket-Info', 'Connection successfuly whit server') try: wsp['status'] = driver.is_logged_in() if 'active' else 'desactiva' socketIO.emit("Auth", wsp) except: socketIO.emit("Auth", wsp)
def on_closeSession(): global awaitLogin, driver, wsp #driver.unsubscribe_new_messages(NewMessageObserver()) driver.close() awaitLogin = None driver = None wsp['status'] = 'desactiva' socketIO.emit('updateAcount', wsp) write_log('Socket-Info', 'session close')
def on_messagesOld(): write_log('Socket-Info', 'session despertar') for chat in driver.get_chats_whit_messages_not_read(): if chat.get('isGroup'): write_log('Socket-Info', 'Grupo eliminado ' + chat.get('id')) driver.leave_group(chat.get('id')) else: driver.chat_load_earlier_messages(chat.get('id')) for message in driver.get_all_messages_in_chat( chat.get('id'), True): time.sleep(.5) on_messageOld(message)
def on_screenShot(*args): global driver if driver != None: write_log('Socket-Info', 'go to screen') name = uuid4().hex + '.png' if os.path.exists(name): os.remove(name) driver.screenshot(name) write_log('Socket-Info', 'saving screen') shutil.move('./' + name, pathSource + name) socketIO.emit('sendScreen', { 'idSend': args[0], 'file': str(pathSource + name) }) else: socketIO.emit('sendScreen', { 'idSend': args[0], 'status': 'Error in connection' })
def getOldMessages(): try: chats = {} write_log('Socket-Info','Get oldMessage') for chat in driver.get_chats_whit_messages(): print("CHAT NUEVO") print(chat.get('id')) print("*****@*****.**") if chat.get('isGroup') != True: ############################################ #### SE RETIRARA DEL CODIGO ESTA LINEA, #### #### TRABA LA EJECUCIÓN DEL PROGRAMA #### #### GAMA 2019-04-30 #### ############################################ #driver.chat_load_earlier_messages(chat.get('id')) if chat.get('id') not in "*****@*****.**": chats[str(chat.get('id'))] = [] _messages = driver.get_all_messages_in_chat(chat.get('id'),True) for message in _messages: chatId = message._js_obj.get('chat').get('id').get('_serialized') sendByMy = True if driver.get_phone_number() == message.sender.id else False body = {'chat':chatId,'message':'','type':False,'caption':False,'sendBy':sendByMy} if message.type == 'image': body['message'] = str(message.save_media(pathSource,True)) body['type'] = 'image' body['caption'] = message.caption elif message.type == 'video': body['message'] = str(message.save_media(pathSource,True)) body['type'] = 'video' body['caption'] = message.caption elif message.type == 'document': body['message'] = str(message.save_media(pathSource,True)) body['type'] = 'file' body['caption'] = message.caption elif message.type == 'audio' or message.type == 'ptt': content = str(message.save_media(pathSource,True)) os.rename(content, content+'.ogg') body['message'] = content body['type'] = 'ogg' elif message.type == 'chat': body['message'] = message.content else : body['message'] = 'No soportado' chats[chatId].append(body) else: write_log('Socket-Info','Message of group') ############################################## #### SE RETIRARA DEL CODIGO ESTA FUNCION, #### #### TRABA LA EJECUCIÓN DEL PROGRAMA #### #### GAMA 2019-04-30 #### ############################################## #outGroup = Thread(target=exitGroup,args=(chat.get('id'),)) #outGroup.start() socketIO.emit('oldMessages',chats) except Exception as e: write_log('Socket-Error',traceback.format_exc()) errorSend(traceback.format_exc())
def loopStatus(): try: global wsp while driver != None: time.sleep(60) write_log('Socket-Info','Send account info') if driver.is_logged_in(): _wsp = {} _wsp['whatsAppJoin'] = driver.is_logged_in() _wsp['bateryLevel'] = driver.get_battery_level() _wsp['numero'] = driver.get_phone_number() _wsp['accountDown'] = False socketIO.emit('change',_wsp) else: _wsp = {} _wsp['whatsAppJoin'] = driver.is_logged_in() _wsp['bateryLevel'] = driver.get_battery_level() _wsp['numero'] = driver.get_phone_number() _wsp['accountDown'] = False socketIO.emit('change',_wsp) except Exception as e: write_log('Socket-Error',traceback.format_exc()) errorSend(traceback.format_exc())
def on_waitLogin(): global awaitLogin, driver, wsp if awaitLogin == None: awaitLogin = True driver.wait_for_login() wsp['status'] = 'active' wsp['numero'] = str(driver.get_phone_number()) socketIO.emit('updateAcount', wsp) write_log('Socket-Info', 'session start') on_sendStatus() write_log('Socket-Info', 'session recovery') on_messagesOld() write_log('Socket-Info', 'session subscribe') driver.subscribe_new_messages(NewMessageObserver())
def on_getQr(*args): write_log('Socket-Info', 'give the qr') global driver if driver == None: driver = WhatsAPIDriver(profile=profiledir, client='remote', command_executor=selemiunIP) if driver.is_logged_in(): write_log('Socket-Info', 'session started') socketIO.emit('sendQr', {'idSend': args[0], 'status': 'Session okay'}) else: write_log('Socket-Info', 'go to qr') name = uuid4().hex + '.png' if os.path.exists(name): os.remove(name) driver.get_qr(name) write_log('Socket-Info', 'saving qr') shutil.move('./' + name, pathSource + name) socketIO.emit('sendQr', { 'idSend': args[0], 'file': str(pathSource + name) }) on_waitLogin()
def on_welcome(): global driver, wsp write_log('Socket-Info', 'Server login success') if driver != None: wsp['status'] = 'active' socketIO.emit('updateAcount', wsp)
def on_reconnect(): global awaitLogin, driver, wsp write_log('Socket-Info', 'Reconnect whit server') socketIO.emit("Auth", wsp)
def on_messageOld(message): sendByMy = True if driver.get_phone_number( ) == message.sender.id else False chatId = message._js_obj.get('chat').get('id').get('_serialized') if message.type == 'chat': write_log( 'Socket-Info', "New message '{}' received from number {}".format( message.type, message.sender.id)) socketIO.emit('newMessage', { 'chat': chatId, 'message': message.content, 'sendBy': sendByMy }) else: write_log( 'Socket-Info', "New message of type '{}' received from number {}".format( message.type, message.sender.id)) if message.type == 'image': content = str(message.save_media(pathSource, True)) socketIO.emit( 'newMessage', { 'chat': chatId, 'message': content, 'type': 'img', 'caption': message.caption, 'sendBy': sendByMy }) elif message.type == 'video': content = str(message.save_media(pathSource, True)) socketIO.emit( 'newMessage', { 'chat': chatId, 'message': content, 'type': 'video', 'caption': message.caption, 'sendBy': sendByMy }) elif message.type == 'document': content = str(message.save_media(pathSource, True)) socketIO.emit( 'newMessage', { 'chat': chatId, 'message': content, 'type': 'file', 'caption': message.caption, 'sendBy': sendByMy }) elif message.type == 'audio' or message.type == 'ptt': content = str(message.save_media(pathSource, True)) os.rename(content, content + '.ogg') socketIO.emit( 'newMessage', { 'chat': chatId, 'message': content + '.ogg', 'type': 'ogg', 'caption': message.caption, 'sendBy': sendByMy }) else: socketIO.emit('newMessage', { 'chat': chatId, 'message': 'Contenido No soportado' })
'newMessage', { 'chat': message.sender.id, 'message': content + '.ogg', 'type': 'ogg', 'caption': message.caption }) else: socketIO.emit( 'newMessage', { 'chat': message.sender.id, 'message': 'Contenido No soportado' }) ##### SOCKET CREDENTIALS ##### write_log('Socket-Info', 'Connect whit server') socketIO = SocketIO(ip, 3000, LoggingNamespace) wsp = {'status': 'desactiva', 'chats': None, 'token': token} ##### SOCKET LISSENER ##### socketIO.on('connect', on_connect) socketIO.on('reconnect', on_reconnect) socketIO.on('welcome', on_welcome) socketIO.on('getQr', on_getQr) socketIO.on('getScreenShot', on_screenShot) socketIO.on('updateInfo', on_updateInfo) socketIO.on('closeSession', on_closeSession) socketIO.on('sendText', on_sendText) socketIO.on('getStatus', on_sendStatus) socketIO.wait()