def run(): print("Environment", os.environ) try: os.environ["SELENIUM"] except KeyError: print("Please set the environment variable SELENIUM to Selenium URL") sys.exit(1) driver = WhatsAPIDriver(client='remote', command_executor=os.environ["SELENIUM"]) print("Waiting for QR") driver.wait_for_login() print("Bot started") driver.subscribe_new_messages(NewMessageObserver()) print("Waiting for new messages...") """ Locks the main thread while the subscription in running """ while True: time.sleep(60)
print("Environment", os.environ) try: os.environ["SELENIUM"] except KeyError: print("Please set the environment variable SELENIUM to Selenium URL") sys.exit(1) ##Save session on "/firefox_cache/localStorage.json". ##Create the directory "/firefox_cache", it's on .gitignore ##The "app" directory is internal to docker, it corresponds to the root of the project. ##The profile parameter requires a directory not a file. profiledir = os.path.join(".", "firefox_cache") if not os.path.exists(profiledir): os.makedirs(profiledir) driver = WhatsAPIDriver(profile=profiledir, client='remote', command_executor=os.environ["SELENIUM"]) print("Waiting for QR") driver.wait_for_login() print("Saving session") driver.save_firefox_profile(remove_old=False) print("Bot started") while True: time.sleep(3) print('Checking for more messages, status', driver.get_status()) for contact in driver.get_unread(): for message in contact.messages: print(json.dumps(message.get_js_obj(), indent=4)) print('class', message.__class__.__name__) print('message', message)
from src import WhatsAPIDriver from src.objects.message import Message driver = WhatsAPIDriver(loadstyles=False) print("Waiting for QR") driver.wait_for_login() print("Bot started") try: phone_safe = "1122333333333" # Phone number with country code phone_whatsapp = "{}@c.us".format(phone_safe) # WhatsApp Chat ID image_path = "image.png" # Local path file caption = "Testing a media sender!" # The caption sent under the image, optional driver.send_media( image_path, phone_whatsapp, caption) # Expected send_media(self, path, chatid, caption) print("Media file was successfully sent to {}".format(phone_safe)) except: print("Error while trying to send the midia file.")
def process_command(command): print_and_log("Processing command: {cmd}".format(cmd=command)) if command.lower() == '#status': send_message_to_master("I am still alive") elif command.lower() == '#quit': quit() elif command.lower() == '#ping': send_message_to_master("The counter is now {ping}".format(ping=pinger)) elif command.lower() == '#uptime': uptime = datetime.datetime.now() - start_time send_message_to_master("Up since {start}, hence for a total time of {upt} by now".format(start=start_time,upt=uptime)) else: send_message_to_master("I am sorry but I can't understand '{cmd}'".format(cmd=command)) # Main driver = WhatsAPIDriver() print("Waiting for QR") driver.wait_for_login() print("Bot started") start_time = datetime.datetime.now() try: while True: time.sleep(3) # Checks for new messages every 3 secs. pinger = pinger +1 if ((pinger%600) == 0): # Notification every 30 min. (600 * 3 sec = 1800 sec) pinger=0 send_message_to_master("Resetting counter to {pingcount}. Driver status is '{status}'".format(pingcount=pinger, status=driver.get_status())) print('Checking for more messages, status. Pinger={pingcount}'.format(pingcount=pinger), driver.get_status())
import time from src import WhatsAPIDriver from src.objects.message import Message driver = WhatsAPIDriver(client='chrome', profile='./arquivos/usuario') print("esperando leitura do codigo QR") while not driver.wait_for_login(): time.sleep(3) print("Bot inciado") while True: time.sleep(1) for contact in driver.get_unread(): for message in contact.messages: if isinstance(message, Message): tipoMsg = message.type # tipo de mensagem: imagem, video, texto, Sticker, etc... idChat = message.chat_id # id do chat que mandou a mensagem. ex: [email protected] idSender = message.sender # id de quem mandou a mensagem. se for mensagem de um grupo, o idChat será diferente do idSender if tipoMsg == "image": print(f'recebi uma imagem de {idChat}') nomeArquivo = message.save_media("./arquivos/imagens", force_download=True) # as fotos recebidas são salvas na pasta imagens driver.send_image_as_sticker(f'./arquivos/imagens/{nomeArquivo}', idChat) # a foto recebida é enviada como figurinha elif tipoMsg == "chat": print(f'recebi uma mensagem de {idChat}') contact.chat.send_message('ola, sou um Robô, me mande uma foto e veja oq acontece :)')
import time from src import WhatsAPIDriver from src.objects.message import Message driver = WhatsAPIDriver() print("Waiting for QR") driver.wait_for_login() print("Bot started") while True: time.sleep(3) print('Checking for more messages') for contact in driver.get_unread(): for message in contact.messages: if isinstance(message, Message): # Currently works for text messages only. contact.chat.send_message(message.content)
elif command.lower() == 'quit': quit() elif command.lower() == 'ping': send_message_to_master("The counter is now {ping}".format(ping=pinger)) elif command.lower() == 'uptime': uptime = datetime.datetime.now() - start_time send_message_to_master( "Up since {start}, hence for a total time of {upt} by now".format( start=start_time, upt=uptime)) else: send_message_to_master( "I am sorry but I can't understand '{cmd}'".format(cmd=command)) # Main driver = WhatsAPIDriver() print("Waiting for QR") driver.wait_for_login() print("Bot started") start_time = datetime.datetime.now() try: while True: time.sleep(3) # Checks for new messages every 3 secs. pinger = pinger + 1 if ((pinger % 600) == 0): # Notification every 30 min. (600 * 3 sec = 1800 sec) pinger = 0 send_message_to_master(
import time from src import WhatsAPIDriver from src.objects.message import Message driver = WhatsAPIDriver() print("Waiting for QR") while not driver.wait_for_login(): time.sleep(3) print("Bot started") while True: time.sleep(3) print('Checking for more messages') for contact in driver.get_unread(): for message in contact.messages: if isinstance(message, Message): # Currently works for text messages only. contact.chat.send_message(message.content)