From e4b0647d8d63d6378e05b07db3ac2857a0c14d81 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 6 Feb 2017 13:14:47 +0100 Subject: [PATCH 1/2] Added error handling when sending messages --- fbchat/client.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fbchat/client.py b/fbchat/client.py index aabf36b..37a7a38 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -413,10 +413,21 @@ class Client(object): data["sticker_id"] = sticker r = self._post(SendURL, data) + + if not r.ok: + return False + + if isinstance(r._content, str) is False: + r._content = r._content.decode(facebookEncoding) + j = get_json(r._content) + if 'error' in j: + # 'errorDescription' is in the users own language! + log.warning('Error #{} when sending message: {}'.format(j['error'], j['errorDescription'])) + return False log.debug("Sending {}".format(r)) log.debug("With data {}".format(data)) - return r.ok + return True def sendRemoteImage(self, recipient_id, message=None, message_type='user', image=''): """Send an image from a URL From 433f99198b47a5476d20eeb75e0f33a1e68379ab Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 6 Feb 2017 13:44:30 +0100 Subject: [PATCH 2/2] Improved login reliability Sometimes Facebook tries to show the user a "Save Device" dialog. --- fbchat/client.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fbchat/client.py b/fbchat/client.py index aabf36b..2dd918c 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -45,6 +45,7 @@ ConnectURL ="https://www.facebook.com/ajax/add_friend/action.php?dpr=1" RemoveUserURL="https://www.facebook.com/chat/remove_participants/" LogoutURL ="https://www.facebook.com/logout.php" AllUsersURL ="https://www.facebook.com/chat/user_info_all" +SaveDeviceURL="https://m.facebook.com/login/save-device/cancel/" facebookEncoding = 'UTF-8' # Log settings @@ -181,6 +182,9 @@ class Client(object): payload=self._generatePayload(query) return self._session.post(url, headers=self._header, data=payload, timeout=timeout) + def _cleanGet(self, url, query=None, timeout=30): + return self._session.get(url, headers=self._header, params=query, timeout=timeout) + def _cleanPost(self, url, query=None, timeout=30): self.req_counter += 1 return self._session.post(url, headers=self._header, data=query, timeout=timeout) @@ -201,6 +205,10 @@ class Client(object): data['login'] = 'Log In' r = self._cleanPost(LoginURL, data) + + # Sometimes Facebook tries to show the user a "Save Device" dialog + if 'save-device' in r.url: + r = self._cleanGet(SaveDeviceURL) if 'home' in r.url: self.client_id = hex(int(random()*2147483648))[2:]