Fixed createGroup
implementation
This commit is contained in:
@@ -1028,24 +1028,25 @@ class Client(object):
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def _doSendRequest(self, data):
|
def _doSendRequest(self, data, get_thread_id=False):
|
||||||
"""Sends the data to `SendURL`, and returns the message ID or None on failure"""
|
"""Sends the data to `SendURL`, and returns the message ID or None on failure"""
|
||||||
j = self._post(self.req_url.SEND, data, fix_request=True, as_json=True)
|
j = self._post(self.req_url.SEND, data, fix_request=True, as_json=True)
|
||||||
|
|
||||||
try:
|
|
||||||
message_ids = [action['message_id'] 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))
|
|
||||||
message_id = message_ids[0]
|
|
||||||
except (KeyError, IndexError, TypeError) as e:
|
|
||||||
raise FBchatException('Error when sending message: No message IDs could be found: {}'.format(j))
|
|
||||||
|
|
||||||
# update JS token if received in response
|
# update JS token if received in response
|
||||||
fb_dtsg = get_jsmods_require(j, 2)
|
fb_dtsg = get_jsmods_require(j, 2)
|
||||||
if fb_dtsg is not None:
|
if fb_dtsg is not None:
|
||||||
self.payloadDefault['fb_dtsg'] = fb_dtsg
|
self.payloadDefault['fb_dtsg'] = fb_dtsg
|
||||||
|
|
||||||
return message_id
|
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))
|
||||||
|
|
||||||
def send(self, message, thread_id=None, thread_type=ThreadType.USER):
|
def send(self, message, thread_id=None, thread_type=ThreadType.USER):
|
||||||
"""
|
"""
|
||||||
@@ -1184,23 +1185,27 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
return self.sendLocalFiles(file_paths=[image_path], message=message, thread_id=thread_id, thread_type=thread_type)
|
return self.sendLocalFiles(file_paths=[image_path], message=message, thread_id=thread_id, thread_type=thread_type)
|
||||||
|
|
||||||
def createGroup(self, message, person_ids=None):
|
def createGroup(self, message, user_ids):
|
||||||
"""Creates a group with the given ids
|
|
||||||
:param person_ids: A list of people to create the group with.
|
|
||||||
:return: Returns error if couldn't create group, returns True when the group created.
|
|
||||||
"""
|
"""
|
||||||
payload = {
|
Creates a group with the given ids
|
||||||
"send" : "send",
|
|
||||||
"body": message,
|
:param message: The initial message
|
||||||
"ids" : person_ids
|
:param user_ids: A list of users to create the group with.
|
||||||
}
|
:return: ID of the new group
|
||||||
r = self._post(self.req_url.CREATE_GROUP, payload)
|
:raises: FBchatException if request failed
|
||||||
if "send_success" in r.url:
|
"""
|
||||||
log.debug("The group was created successfully!")
|
data = self._getSendData(message=self._oldMessage(message))
|
||||||
return True
|
|
||||||
else:
|
if len(user_ids) < 2:
|
||||||
log.warning("Error while creating group")
|
raise FBchatUserError("Error when creating group: Not enough participants")
|
||||||
return False
|
|
||||||
|
for i, user_id in enumerate(user_ids + [self.uid]):
|
||||||
|
data['specific_to_list[{}]'.format(i)] = 'fbid:{}'.format(user_id)
|
||||||
|
|
||||||
|
message_id, thread_id = self._doSendRequest(data, get_thread_id=True)
|
||||||
|
if not thread_id:
|
||||||
|
raise FBchatException("Error when creating group: No thread_id could be found")
|
||||||
|
return thread_id
|
||||||
|
|
||||||
def addUsersToGroup(self, user_ids, thread_id=None):
|
def addUsersToGroup(self, user_ids, thread_id=None):
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user