mukulhase / WebWhatsapp-Wrapper

An API for sending and receiving messages over web.whatsapp [Working as of 18th May 2018]
https://webwhatsapi.readthedocs.io/en/latest/
MIT License
2.03k stars 797 forks source link

get_unread #545

Open gabrieligbastos opened 5 years ago

gabrieligbastos commented 5 years ago

I've already saw #328 and #86 and #243 and #385 but not working, was not my problem =(

import os, sys, time, json, datetime, base64
import requests
from webwhatsapi import WhatsAPIDriver
from webwhatsapi.objects.message import Message

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 'Saved session'
botId = None

while botId is None:
    try:
    botId = driver.driver.execute_script("return Store.Conn.me.toString()")
    print("Bot started ", botId)
    except:
        print 'did not find connection.me, will try again'
    time.sleep(2)

warned = False
#trying to download old messages
print 'Make sure get all messages downloaded'
for chat in driver.get_all_chats():
   chat.load_earlier_messages()
   print 'Loaded earlier messages for chat', chat.name

for contact in driver.get_unread(False, False, True):

Then, I'm getting:

for contact in driver.get_unread(False, False, True):
  File "/usr/local/lib/python2.7/site-packages/webwhatsapi/__init__.py", line 354, in get_unread
    messages.sort(key=lambda message: message.timestamp)
  File "/usr/local/lib/python2.7/site-packages/webwhatsapi/__init__.py", line 354, in <lambda>
    messages.sort(key=lambda message: message.timestamp)
AttributeError: 'NoneType' object has no attribute 'timestamp'

Any clue why the messages are NoneType and then no timestamp attribute?

Operating System

Amazon Linux AMI release 2018.03
Linux ip-172-31-37-55 4.14.77-70.59.amzn1.x86_64 #1 SMP Mon Nov 12 22:02:45 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Python Version Python 2.7.15

Selenium Version Firefox v61.0.2 (64-bit)

Everything running inside docker container. Already tried to pull again all code from github , built it and still now working =(

gabrieligbastos commented 5 years ago

The bug seems to be that I was asking for Non_NotificationMessages.. But flag unreads one...

If I had a chat with only one unread message that was notificaiton one, (somebody added me to a group i.e.) it was crashing in this, because he filtered for non-notificaiton, but forced to fetch one because of unread flags, i imagine this was the problem..

maybe should treat this in the get_unread method, i'm doing workaround here at program side

Arlington1985 commented 5 years ago

I had the same issue, it's how I fixed it:
Replaced messages = [factory_message(message, self) for message in raw_message_group['messages']] with messages = list(filter(None.__ne__,[factory_message(message, self) for message in raw_message_group['messages']])) or if you didn't convert it still to python 3: messages = list(filter(None,[factory_message(message, self) for message in raw_message_group['messages']])) Basically it filters out all the None object from the array list @mukulhase please check if it's ok I can create merge request