From d4446280c7361c75365c55f01dc35e1be5f62227 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 9 Dec 2018 15:27:01 +0100 Subject: [PATCH] Detecting when someone unsends a message --- fbchat/client.py | 25 ++++++++++++++++++++++++- fbchat/graphql.py | 14 ++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 795b280..dfa9160 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -2387,10 +2387,20 @@ class Client(object): thread_id, thread_type = getThreadIdAndThreadType(i) for l in i['messageLiveLocations']: mid = l["messageId"] - author_id = l["senderId"] + author_id = str(l["senderId"]) location = graphql_to_live_location(l) self.onLiveLocation(mid=mid, location=location, author_id=author_id, thread_id=thread_id, thread_type=thread_type, ts=ts, msg=m) + + # Message deletion + elif d.get('deltaRecallMessageData'): + i = d['deltaRecallMessageData'] + thread_id, thread_type = getThreadIdAndThreadType(i) + mid = i['messageID'] + ts = i['deletionTimestamp'] + author_id = str(i['senderID']) + self.onMessageDeleted(mid=mid, author_id=author_id, thread_id=thread_id, thread_type=thread_type, + ts=ts, msg=m) # New message elif delta.get("class") == "NewMessage": @@ -2798,6 +2808,19 @@ class Client(object): """ log.info("Marked messages as seen in threads {} at {}s".format([(x[0], x[1].name) for x in threads], seen_ts/1000)) + def onMessageDeleted(self, mid=None, author_id=None, thread_id=None, thread_type=None, ts=None, msg=None): + """ + Called when the client is listening, and someone unsends (deleted for everyone) a message + + :param mid: ID of the deleted message + :param author_id: The ID of the person who deleted the message + :param thread_id: Thread ID that the action was sent to. See :ref:`intro_threads` + :param thread_type: Type of thread that the action was sent to. See :ref:`intro_threads` + :param ts: A timestamp of the action + :param msg: A full set of the data recieved + :type thread_type: models.ThreadType + """ + log.info("{} unsended the message {} in {} ({}) at {}s".format(author_id, repr(mid), thread_id, thread_type.name, ts/1000)) def onPeopleAdded(self, mid=None, added_ids=None, author_id=None, thread_id=None, ts=None, msg=None): """ diff --git a/fbchat/graphql.py b/fbchat/graphql.py index b6c1ae2..0d6c5d2 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -141,9 +141,10 @@ def graphql_to_extensible_attachment(a): latitude=float(latitude), longitude=float(longitude), ) - rtn.image_url = story['media']['image']['uri'] - rtn.image_width = story['media']['image']['width'] - rtn.image_height = story['media']['image']['height'] + if story['media']: + rtn.image_url = story['media']['image']['uri'] + rtn.image_width = story['media']['image']['width'] + rtn.image_height = story['media']['image']['height'] rtn.url = story['url'] return rtn elif _type == 'MessageLiveLocation': @@ -155,9 +156,10 @@ def graphql_to_extensible_attachment(a): expiration_time=story['target']['expiration_time'] if story['target'].get('expiration_time') else None, is_expired=story['target']['is_expired'], ) - rtn.image_url = story['media']['image']['uri'] - rtn.image_width = story['media']['image']['width'] - rtn.image_height = story['media']['image']['height'] + if story['media']: + rtn.image_url = story['media']['image']['uri'] + rtn.image_width = story['media']['image']['width'] + rtn.image_height = story['media']['image']['height'] rtn.url = story['url'] return rtn elif _type in ['ExternalUrl', 'Story']: