class WhatsAppAgent(object):
    def __init__(self):
        self.driver = WhatsAPIDriver(client=WhatsAppAgentConstants.BROWSER)
        logger.info("Waiting for successful QR Code scan...")
        self.driver.wait_for_login()
        logger.info("Logged in!")

    def get_all_chats(self):
        return self.driver.get_all_chats()

    def get_all_msgs_in_chat(self, chat=None):
        if chat is None:
            return []
        return self.driver.get_all_messages_in_chat(chat)

    def get_msg_text(self, message=None):
        if message is None:
            return ""
        return message.safe_content

    def get_unread_msgs(self):
        for contact in self.driver.get_unread():
            for message in contact.messages:
                if isinstance(message, Message):
                    whatsAppMsg = WhatsAppAgentMessage(
                        chatId=contact.chat.id,
                        chatName=contact.chat.name,
                        senderId="",
                        senderName=message.sender.get_safe_name(),
                        msgType="",
                        msgData=message.safe_content,
                        msgTime=message.timestamp)

                    logger.debug("New message: %s",
                                 whatsAppMsg.readable_format())

    def run(self):
        while True:
            time.sleep(WhatsAppAgentConstants.NEW_MSG_POLL)
            self.get_unread_msgs()
    def run(self, profile_path="/data/firefox_cache"):
        """
        Faz a coleta dos metadados de grupos de Whatsapp de acordo
        com os parâmetros fornecidos na criação do objeto de coleta.

        Parâmetros
        ------------
            profile_path : str
                Caminho para um profile alternativo do navegador
                utilizado na coleta.
        """

        today = datetime.date.today().strftime('%Y-%m-%d')
        all_groups_filename = '/data/all_grupos_%s.json' % (today)
        with open(all_groups_filename, 'w') as json_file:
            print('Collecting metadata for groups at %s' % (today))

        if not os.path.exists(profile_path):
            os.makedirs(profile_path)

        driver = WhatsAPIDriver(loadstyles=True,
                                profile=profile_path,
                                client="remote",
                                command_executor=os.environ["SELENIUM"])

        try:
            print("Waiting for WhatsApp Web Login")
            driver.wait_for_login()
            print("Saving session")
            driver.save_firefox_profile(remove_old=False)
            print("Bot started")

            pathlib.Path("/data/grupos").mkdir(parents=True, exist_ok=True)

            print('>>>>>>>>>>> Loading chat ids')
            chats = driver.get_all_chats()

            for chat in (chats):
                # Does not collect direct messages, only group chats
                if not chat._js_obj['isGroup']:
                    continue

                gid = chat.id
                gid = gid.split('@')[0]
                s_name = self._process_string(chat.name)

                # Skip group if it is on blacklist (can be name or groupID)
                if (s_name in self.group_blacklist
                        or gid in self.group_blacklist):
                    continue

                group = dict()

                _id = chat.id
                creator = _id.split('-')[0]
                timestamp = _id.split('-')[-1].split('@')[0]
                date = convert_data_from_timestamp(float(timestamp))
                str_date = date.strftime('%Y-%m-%d %H:%M:%S')
                name = chat.name.strip().replace('\t', ' ')

                kind = chat._js_obj["kind"]

                participants = list()
                for member in driver.group_get_participants(_id):
                    user = dict()
                    user['name'] = member.verified_name
                    user['short_name'] = member.short_name
                    user['nome_formatado'] = member.formatted_name
                    user['number'] = member.id
                    user['isBusiness'] = member.is_business
                    user['profile_pic'] = member.profile_pic
                    participants.append(user)

                group['group_id'] = _id
                group['creator'] = creator
                group['kind'] = kind
                group['creation'] = dict()
                group['creation']['creation_date'] = str_date
                group['creation']['creation_timestamp'] = timestamp
                group['title'] = name
                group['members'] = participants

                path = '/data/grupos/'
                filename = '%sgrupos_%s.json' % (path,
                                                 _id.split('@')[0].strip())
                print(group)
                with open(filename, 'w') as json_file:
                    json.dump(group, json_file)
                    print('', file=json_file)
                with open(all_groups_filename, 'a') as json_file:
                    json.dump(group, json_file)
                    print('', file=json_file)

            driver.close()
        except Exception as e:
            print(e)
            driver.close()
            raise Exception(e)
Example #3
0
from webwhatsapi import WhatsAPIDriver
import os
import time
path_to_geckodriver = os.path.join(os.getcwd(), "geckodriver")
driver = WhatsAPIDriver(extra_params={"executable_path": path_to_geckodriver})
# print(driver.get_unread())
time.sleep(7)
print(driver.get_all_chats())
Example #4
0
from webwhatsapi import WhatsAPIDriver

driver = WhatsAPIDriver(username="******")
print('get_unread: ',
      driver.get_unread(include_me=True, include_notifications=True))
print('get_contacts: ', driver.get_contacts())
# print('save_firefox_profile: ', driver.save_firefox_profile())
getChatFrom = driver.get_chat_from_phone_number(628999871008)
print('get_chat_from_phone_number: ', getChatFrom)
# print('get_all_messages_in_chat: ', driver.get_all_messages_in_chat(getChatFrom.id, include_me=True, include_notifications=True))
print(driver.get_qr())
print('get_all_chats: ', driver.get_all_chats())
print(driver.wait_for_login())
# print('get_safe_name', driver.get_safe_name())
print('get_unread: ', driver.get_unread())
# print('view_unread: ', driver.view_unread())
Example #5
0
    def run(self, profile_path="/data/firefox_cache"):
        """
        Faz a coleta das mensagens de grupos de Whatsapp de acordo
        com os parâmetros fornecidos na criação do objeto de coleta.

        Parâmetros
        ------------
            profile_path : str
                Caminho para um profile alternativo do navegador
                utilizado na coleta.
        """
        if not os.path.exists(profile_path):
            os.makedirs(profile_path)
        driver = WhatsAPIDriver(loadstyles=True, profile=profile_path,
                                client="remote",
                                command_executor=os.environ["SELENIUM"])

        pathlib.Path("/data/mensagens").mkdir(parents=True, exist_ok=True)
        pathlib.Path("/data/image").mkdir(parents=True, exist_ok=True)
        pathlib.Path("/data/audio").mkdir(parents=True, exist_ok=True)
        pathlib.Path("/data/video").mkdir(parents=True, exist_ok=True)
        pathlib.Path("/data/mensagens_grupo").mkdir(parents=True, exist_ok=True)
        pathlib.Path("/data/notificacoes").mkdir(parents=True, exist_ok=True)
        pathlib.Path("/data/mids").mkdir(parents=True, exist_ok=True)

        min_date = self.start_date
        max_date = self.end_date
        include_notf = self.collect_notifications
        looping = True
        if (self.collection_mode == 'period') and (min_date < '2020-01-01'):
            raise Exception("Can't start collection without a start and end"
                            " date.")

        while looping:

            if self.collection_mode == 'continuous':
                looping = True
            else:
                looping = False

            try:
                print("Waiting for WhatsApp Web Login")
                driver.wait_for_login()
                print("Saving session")
                driver.save_firefox_profile(remove_old=False)
                print("Bot started")

                print('>>>>>>>>>>> Loading previous saved Messages')
                messagesID = self._get_load_messages()
                notificationsID = self._get_load_notifications()

                today_date = datetime.date.today().strftime("%Y-%m-%d")
                date_format = "%Y-%m-%d"
                file_name = "/data/mensagens/mensagens_" + today_date + ".json"
                start_date = min_date

                print('>>>>>>>>>>>>Getting Groups Messages...', end=' ')
                chats = driver.get_all_chats()
                count = 0
                all_chats = list(chats)

                print(' DONE! %d chats loaded!' % (len(all_chats)))
                random.shuffle(all_chats)

                for chat in (all_chats):
                    # Does not collect direct messages, only group chats
                    if not chat._js_obj['isGroup']:
                        continue

                    gid = chat.id
                    gid = gid.split('@')[0]
                    s_name = self._process_string(chat.name)

                    # Skip group if it is on blacklist (can be name or groupID)
                    if (s_name in self.group_blacklist or
                            gid in self.group_blacklist):
                        continue

                    # PRINT CHAT INFORMATION
                    members = chat._js_obj['groupMetadata']['participants']
                    timestamp = gid.split('-')[-1]
                    date = convert_data_from_timestamp(float(timestamp))
                    str_date = date.strftime('%Y-%m-%d %H:%M:%S')

                    chat_print = "<Group chat - {name}: {id}, {participants} " \
                        "participants - at {time}!!>".format(
                            name=s_name, id=gid, participants=len(members),
                            time=str_date)
                    print('>>>>>Loading messages from', chat_print)

                    if gid not in messagesID:
                        messagesID[gid] = dict()
                        messagesID[gid]['messages'] = set()
                        messagesID[gid]['date'] = '2000-01-01'

                    # PROCESS PREVIOUS LOADED MESSAGES ID AND LAST DATE
                    if self.collection_mode == 'continuous':
                        if messagesID[gid]['date'] > max_date:
                            continue
                        if messagesID[gid]['date'] > min_date:
                            start_date = messagesID[gid]['date']
                            till_date = datetime.datetime.strptime(start_date,
                                                                   date_format)
                        else:
                            start_date = min_date
                            till_date = datetime.datetime.strptime(start_date,
                                                                   date_format)

                        # LOAD MESSAGES FROM WHATSAPP SINCE MIN_DATE
                        messages = chat.load_earlier_messages_till(till_date)
                        messages = driver.get_all_message_ids_in_chat(
                            chat, include_notifications=include_notf)

                    elif self.collection_mode == 'period':
                        till_date = datetime.datetime.strptime(start_date,
                                                               date_format)
                        # LOAD MESSAGES FROM WHATSAPP SINCE MIN_DATE
                        messages = chat.load_earlier_messages_till(till_date)
                        messages = driver.get_all_message_ids_in_chat(
                            chat, include_notifications=include_notf)

                    elif self.collection_mode == 'unread':
                        # LOAD UNREAD MESSAGES FROM WHATSAPP
                        messages = chat.get_unread_messages(
                            include_me=False,
                            include_notifications=include_notf)

                    print('>>>>>Total messages %d' % (len(messages)))
                    count += 1

                    for msg in messages:
                        count += 1
                        gid = gid.split('@')[0]
                        mid = msg

                        if self._is_notification(mid):
                            if gid not in notificationsID.keys():
                                notificationsID[gid] = set()
                            if mid.strip() in notificationsID[gid]:
                                continue
                            j = driver.get_message_by_id(mid)
                            self._save_notification_(j, gid)
                            continue

                        if mid.strip() in messagesID[gid]['messages']:
                            print('Message: %d >>> %s from %s was CHECKED' %
                                  (count, mid, gid))
                            continue

                        else:
                            try:
                                j = driver.get_message_by_id(mid)
                            except Exception as e:
                                print('Error getting a message >>', e)
                                continue
                            if not j:
                                continue

                        sender = j.sender.id
                        sender = sender.replace(' ', '').strip()
                        sender = sender.split('@')[0]
                        if (sender in self.user_blacklist or
                                '+' + sender in self.user_blacklist):
                            continue

                        try:
                            date = self._get_date_from_message(j)
                        except Exception:
                            continue

                        if (date > max_date) and (self.collection_mode == 'period'):
                            break
                        if (date < start_date):
                            continue

                        # Update day
                        if today_date != date:
                            today_date = date
                            file_name = "/data/mensagens/mensagens_" + today_date + ".json"

                        if self.collect_images:
                            try:
                                self._get_image_from_message(j)
                            except Exception as ei:
                                print('!!!!Error getting image!!!! ', ei)

                        if self.collect_videos:
                            try:
                                self._get_video_from_message(j)
                            except Exception as ev:
                                print('!!!!Error getting video!!!! ', ev)

                        if self.collect_audios:
                            try:
                                self._get_audio_from_message(j)
                            except Exception as ea:
                                print('!!!!Error getting audio!!!! ', ea)

                        if self.collect_messages:
                            self._save_message(j, s_name, gid, mid, file_name)

                driver.close()
            except Exception as e:
                print(e)
                driver.close()
                raise Exception(e)
            if looping:
                print('Waiting code to start again...')
                time.sleep(3600)
Example #6
0
from webwhatsapi import WhatsAPIDriver
#  https://stackoverflow.com/questions/29846087/microsoft-visual-c-14-0-is-required-unable-to-find-vcvarsall-bat#comment-91095944
# need virtualstudio setuptools for webwhatsapi
import os

# driver = WhatsAPIDriver(client="chrome", chrome_options=[])
path_to_driver = os.path.join(os.getcwd(), "geckodriver.exe")
driver = WhatsAPIDriver(exe)
chats = driver.get_all_chats()
print(chats)
# Load config
with open("config.yaml", "r") as configFile:
    config = yaml.load(configFile)

# Connect to Slack
sc = SlackClient(config['slack_token'])

# Connect WhatsApp
driver = WhatsAPIDriver()
print("Waiting for login")
driver.wait_for_login()

# Find our chat
group_chat = None
for chat in driver.get_all_chats():
    if isinstance(chat, GroupChat) and chat.name == config['group_name']:
        group_chat = chat
        break


def whatsapp():
    last_message = None

    while True
        messages = driver.get_all_messages_in_chat(group_chat, True)

        new_messages = []
        for index, message in enumerate(messages):
            if last_message is None or message.timestamp > last_message.timestamp:
                new_messages = messages[index:]