From 5b965e63f87ebdcd7eb32e6b286c7ef551a240a7 Mon Sep 17 00:00:00 2001 From: kapi2289 Date: Tue, 3 Jul 2018 23:13:47 +0200 Subject: [PATCH 1/4] Update client.py --- fbchat/client.py | 57 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 79adb39..7bd90f9 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -1080,6 +1080,50 @@ class Client(object): } j = self._post(self.req_url.REMOVE_USER, data, fix_request=True, as_json=True) + + def changeThreadImage(self, image_id, thread_id=None, thread_type=ThreadType.USER): + + thread_id, thread_type = self._getThread(thread_id, thread_type) + + if thread_type == ThreadType.GROUP: + data = { + 'thread_image_id': image_id, + 'thread_id': thread_id + } + + j = self._post(self.req_url.THREAD_IMAGE, data, fix_request=True, as_json=True) + + def changeThreadImageRemote(self, image_url, thread_id=None, thread_type=ThreadType.USER): + + thread_id, thread_type = self._getThread(thread_id, thread_type) + + if thread_type == ThreadType.GROUP: + mimetype = guess_type(image_url)[0] + is_gif = (mimetype == 'image/gif') + remote_image = requests.get(image_url).content + image_id = self._uploadImage(image_url, remote_image, mimetype) + data = { + 'thread_image_id': image_id, + 'thread_id': thread_id + } + + j = self._post(self.req_url.THREAD_IMAGE, data, fix_request=True, as_json=True) + + def changeThreadImageLocal(self, image_path, thread_id=None, thread_type=ThreadType.USER): + + thread_id, thread_type = self._getThread(thread_id, thread_type) + + if thread_type == ThreadType.GROUP: + + mimetype = guess_type(image_path)[0] + is_gif = (mimetype == 'image/gif') + image_id = self._uploadImage(image_path, open(image_path, 'rb'), mimetype) + data = { + 'thread_image_id': image_id, + 'thread_id': thread_id + } + + j = self._post(self.req_url.THREAD_IMAGE, data, fix_request=True, as_json=True) def changeThreadTitle(self, title, thread_id=None, thread_type=ThreadType.USER): """ @@ -1094,18 +1138,17 @@ class Client(object): """ thread_id, thread_type = self._getThread(thread_id, thread_type) - + if thread_type == ThreadType.USER: # The thread is a user, so we change the user's nickname return self.changeNickname(title, thread_id, thread_id=thread_id, thread_type=thread_type) else: - data = self._getSendData(thread_id=thread_id, thread_type=thread_type) + data = { + 'thread_name': title, + 'thread_id': thread_id, + } - data['action_type'] = 'ma-type:log-message' - data['log_message_data[name]'] = title - data['log_message_type'] = 'log:thread-name' - - return self._doSendRequest(data) + j = self._post(self.req_url.THREAD_NAME, data, fix_request=True, as_json=True) def changeNickname(self, nickname, user_id, thread_id=None, thread_type=ThreadType.USER): """ From e6fdc56d2505e714c7779bcad16ef6699bfd5729 Mon Sep 17 00:00:00 2001 From: kapi2289 Date: Tue, 3 Jul 2018 23:14:48 +0200 Subject: [PATCH 2/4] Update utils.py --- fbchat/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fbchat/utils.py b/fbchat/utils.py index 2b31228..d33a70f 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -116,6 +116,8 @@ class ReqUrl(object): THREAD_COLOR = "https://www.facebook.com/messaging/save_thread_color/?source=thread_settings&dpr=1" THREAD_NICKNAME = "https://www.facebook.com/messaging/save_thread_nickname/?source=thread_settings&dpr=1" THREAD_EMOJI = "https://www.facebook.com/messaging/save_thread_emoji/?source=thread_settings&dpr=1" + THREAD_IMAGE = "https://www.facebook.com/messaging/set_thread_image/?dpr=1" + THREAD_NAME = "https://www.facebook.com/messaging/set_thread_name/?dpr=1" MESSAGE_REACTION = "https://www.facebook.com/webgraphql/mutation" TYPING = "https://www.facebook.com/ajax/messaging/typ.php" GRAPHQL = "https://www.facebook.com/api/graphqlbatch/" From 714e783e0d25c04d94811fbe08cbf6314f0f60d6 Mon Sep 17 00:00:00 2001 From: kapi2289 Date: Sat, 7 Jul 2018 22:39:02 +0200 Subject: [PATCH 3/4] Update client.py --- fbchat/client.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 7bd90f9..81fa70c 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -1085,7 +1085,9 @@ class Client(object): thread_id, thread_type = self._getThread(thread_id, thread_type) - if thread_type == ThreadType.GROUP: + if thread_type != ThreadType.GROUP: + raise FBchatUserError('Can only change the image of group threads') + else: data = { 'thread_image_id': image_id, 'thread_id': thread_id @@ -1097,33 +1099,28 @@ class Client(object): thread_id, thread_type = self._getThread(thread_id, thread_type) - if thread_type == ThreadType.GROUP: + if thread_type != ThreadType.GROUP: + raise FBchatUserError('Can only change the image of group threads') + else: mimetype = guess_type(image_url)[0] is_gif = (mimetype == 'image/gif') remote_image = requests.get(image_url).content image_id = self._uploadImage(image_url, remote_image, mimetype) - data = { - 'thread_image_id': image_id, - 'thread_id': thread_id - } - j = self._post(self.req_url.THREAD_IMAGE, data, fix_request=True, as_json=True) + self.changeThreadImage(image_id, thread_id, thread_type) def changeThreadImageLocal(self, image_path, thread_id=None, thread_type=ThreadType.USER): thread_id, thread_type = self._getThread(thread_id, thread_type) - if thread_type == ThreadType.GROUP: - + if thread_type != ThreadType.GROUP: + raise FBchatUserError('Can only change the image of group threads') + else: mimetype = guess_type(image_path)[0] is_gif = (mimetype == 'image/gif') image_id = self._uploadImage(image_path, open(image_path, 'rb'), mimetype) - data = { - 'thread_image_id': image_id, - 'thread_id': thread_id - } - j = self._post(self.req_url.THREAD_IMAGE, data, fix_request=True, as_json=True) + self.changeThreadImage(image_id, thread_id, thread_type) def changeThreadTitle(self, title, thread_id=None, thread_type=ThreadType.USER): """ From 5af01bb8ff24f0b24440ec6b91b2448179d3c357 Mon Sep 17 00:00:00 2001 From: kapi2289 Date: Sun, 8 Jul 2018 14:37:44 +0200 Subject: [PATCH 4/4] Added documentation --- fbchat/client.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/fbchat/client.py b/fbchat/client.py index 81fa70c..5b5dbe1 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -1082,6 +1082,15 @@ class Client(object): j = self._post(self.req_url.REMOVE_USER, data, fix_request=True, as_json=True) def changeThreadImage(self, image_id, thread_id=None, thread_type=ThreadType.USER): + """ + Changes a thread image from an image id + + :param image_id: ID of uploaded image + :param thread_id User/Group ID to change image. See :ref:`intro_threads` + :param thread_type: See :ref:`intro_threads` + :type thread_type: models.ThreadType + :raises: FBchatException if request failed + """ thread_id, thread_type = self._getThread(thread_id, thread_type) @@ -1096,6 +1105,15 @@ class Client(object): j = self._post(self.req_url.THREAD_IMAGE, data, fix_request=True, as_json=True) def changeThreadImageRemote(self, image_url, thread_id=None, thread_type=ThreadType.USER): + """ + Changes a thread image from a URL + + :param image_url: URL of an image to upload and change + :param thread_id: User/Group ID to change image. See :ref:`intro_threads` + :param thread_type: See :ref:`intro_threads` + :type thread_type: models.ThreadType + :raises: FBchatException if request failed + """ thread_id, thread_type = self._getThread(thread_id, thread_type) @@ -1110,6 +1128,15 @@ class Client(object): self.changeThreadImage(image_id, thread_id, thread_type) def changeThreadImageLocal(self, image_path, thread_id=None, thread_type=ThreadType.USER): + """ + Changes a thread image from a local path + + :param image_path: Path of an image to upload and change + :param thread_id: User/Group ID to change image. See :ref:`intro_threads` + :param thread_type: See :ref:`intro_threads` + :type thread_type: models.ThreadType + :raises: FBchatException if request failed + """ thread_id, thread_type = self._getThread(thread_id, thread_type)