From 4d13cd2c0b06a22ef10ec2f79d598a7d0be200b0 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sat, 27 Jul 2019 11:32:52 +0200 Subject: [PATCH] Move body of Client._doSendRequest to State --- fbchat/_client.py | 29 +++++------------------------ fbchat/_state.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index abc3e51..849a144 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -1056,30 +1056,11 @@ class Client(object): def _doSendRequest(self, data, get_thread_id=False): """Send the data to `SendURL`, and returns the message ID or None on failure.""" - j = self._post("/messaging/send/", data) - - # update JS token if received in response - fb_dtsg = get_jsmods_require(j, 2) - if fb_dtsg is not None: - self._state.fb_dtsg = fb_dtsg - - try: - message_ids = [ - (action["message_id"], action["thread_fbid"]) - for action in j["payload"]["actions"] - if "message_id" in action - ] - if len(message_ids) != 1: - log.warning("Got multiple message ids' back: {}".format(message_ids)) - if get_thread_id: - return message_ids[0] - else: - return message_ids[0][0] - except (KeyError, IndexError, TypeError) as e: - raise FBchatException( - "Error when sending message: " - "No message IDs could be found: {}".format(j) - ) + mid, thread_id = self._state._do_send_request(data) + if get_thread_id: + return mid, thread_id + else: + return mid def send(self, message, thread_id=None, thread_type=ThreadType.USER): """Send message to a thread. diff --git a/fbchat/_state.py b/fbchat/_state.py index 78942e4..ab5a389 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -297,3 +297,26 @@ class State(object): (data[_util.mimetype_to_key(data["filetype"])], data["filetype"]) for data in j["metadata"] ] + + def _do_send_request(self, data): + j = self._post("/messaging/send/", data) + + # update JS token if received in response + fb_dtsg = _util.get_jsmods_require(j, 2) + if fb_dtsg is not None: + self.fb_dtsg = fb_dtsg + + try: + message_ids = [ + (action["message_id"], action["thread_fbid"]) + for action in j["payload"]["actions"] + if "message_id" in action + ] + if len(message_ids) != 1: + log.warning("Got multiple message ids' back: {}".format(message_ids)) + return message_ids[0] + except (KeyError, IndexError, TypeError) as e: + raise _exception.FBchatException( + "Error when sending message: " + "No message IDs could be found: {}".format(j) + )