From 64243c20b2a7e4424371e404229e9ad7bf9a4378 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 5 May 2017 20:12:17 +0200 Subject: [PATCH] Fixed tests, and improved send() --- fbchat/client.py | 33 ++++++++++++++++++++++++--------- tests.py | 14 ++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index c8aca48..ffa77cc 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -451,6 +451,8 @@ class Client(object): :param like: size of the like sticker you want to send :param image_id: id for the image to send, gotten from the UploadURL :param add_user_ids: a list of user ids to add to a chat + + returns a list of message ids of the sent message(s) """ if self.is_def_recipient_set: @@ -521,11 +523,10 @@ class Client(object): data["sticker_id"] = sticker r = self._post(SendURL, data) - - if r.ok: - log.info('Message sent.') - else: - log.info('Message not sent.') + + if not r.ok: + log.warning('Error when sending message: Got {} response'.format(r.status_code)) + return False if isinstance(r._content, str) is False: r._content = r._content.decode(facebookEncoding) @@ -534,10 +535,18 @@ class Client(object): # 'errorDescription' is in the users own language! log.warning('Error #{} when sending message: {}'.format(j['error'], j['errorDescription'])) return False + + message_ids = [] + try: + message_ids += [action['message_id'] for action in j['payload']['actions'] if 'message_id' in action] + except KeyError as e: + log.warning('Error when sending message: No message ids could be found') + return False + log.info('Message sent.') log.debug("Sending {}".format(r)) log.debug("With data {}".format(data)) - return True + return message_ids def sendRemoteImage(self, recipient_id=None, message=None, is_user=True, image=''): @@ -826,10 +835,10 @@ class Client(object): fbid = m['delta']['messageMetadata']['actorFbId'] self.on_message_new(mid, fbid, message, m, recipient_id, thread_type) elif m['type'] in ['jewel_requests_add']: - from_id = m['from'] - self.on_friend_request(from_id) + from_id = m['from'] + self.on_friend_request(from_id) else: - log.debug("Unknown type {}".format(m)) + self.on_unknown_type(m) except Exception as e: # ex_type, ex, tb = sys.exc_info() self.on_message_error(sys.exc_info(), m) @@ -1021,3 +1030,9 @@ class Client(object): def on_qprimer(self, timestamp): pass + + + def on_unknown_type(self, m): + """subclass Client and override this method to add custom behavior on event""" + log.debug("Unknown type {}".format(m)) + diff --git a/tests.py b/tests.py index f2d1e0b..533e0d2 100644 --- a/tests.py +++ b/tests.py @@ -14,17 +14,19 @@ fbchat.log.setLevel(100) """ Tests for fbchat +~~~~~~~~~~~~~~~~ To use these tests, put: - email - password - a group_uid -- a user_uid whom will be kicked from that group and then added again +- a user_uid (the user will be kicked from the group and then added again) (seperated these by a newline) in a file called `tests.data`, or type them manually in the terminal prompts Please remember to test both python v. 2.7 and python v. 3.6! If you've made any changes to the 2FA functionality, test it with a 2FA enabled account +If you only want to execute specific tests, pass the function names in the commandline """ @@ -56,21 +58,21 @@ class TestFbchat(unittest.TestCase): def test_getAllUsers(self): users = client.getAllUsers() - self.assertTrue(len(users) > 0) + self.assertGreater(len(users), 0) def test_getUsers(self): users = client.getUsers("Mark Zuckerberg") - self.assertTrue(len(users) > 0) + self.assertGreater(len(users), 0) u = users[0] # Test if values are set correctly - self.assertTrue(isinstance(u.uid, int)) + self.assertIsInstance(u.uid, int) self.assertEquals(u.type, 'user') self.assertEquals(u.photo[:4], 'http') self.assertEquals(u.url[:4], 'http') self.assertEquals(u.name, 'Mark Zuckerberg') - self.assertTrue(u.score > 0) + self.assertGreater(u.score, 0) def test_send_likes(self): self.assertTrue(client.send(client.uid, like='s')) @@ -147,7 +149,7 @@ if __name__ == '__main__': try: with open(path.join(path.dirname(__file__), 'tests.data'), 'r') as f: content = f.readlines() - content = [x.strip() for x in content] + content = [x.strip() for x in content if len(x.strip()) != 0] email = content[0] password = content[1] group_uid = content[2]