Chat messages were upgraded to the new post method, too
This commit is contained in:
parent
bbef34d125
commit
a7173adc22
@ -1182,67 +1182,14 @@ class chatBuffer(baseBuffer):
|
||||
wx.Bell()
|
||||
return
|
||||
self.tab.text.SetValue("")
|
||||
call_threaded(self._send_message, text=text)
|
||||
|
||||
def upload_attachments(self, attachments):
|
||||
""" Upload attachments to VK before posting them.
|
||||
Returns attachments formatted as string, as required by VK API.
|
||||
"""
|
||||
local_attachments = ""
|
||||
uploader = upload.VkUpload(self.session.vk.session_object)
|
||||
for i in attachments:
|
||||
if i["from"] == "online":
|
||||
local_attachments += "{0}{1}_{2},".format(i["type"], i["owner_id"], i["id"])
|
||||
elif i["from"] == "local" and i["type"] == "photo":
|
||||
photos = i["file"]
|
||||
description = i["description"]
|
||||
r = uploader.photo_messages(photos)
|
||||
id = r[0]["id"]
|
||||
owner_id = r[0]["owner_id"]
|
||||
local_attachments += "photo{0}_{1},".format(owner_id, id)
|
||||
elif i["from"] == "local" and i["type"] == "audio":
|
||||
audio = i["file"]
|
||||
title = "untitled"
|
||||
artist = "unnamed"
|
||||
if "artist" in i:
|
||||
artist = i["artist"]
|
||||
if "title" in i:
|
||||
title = i["title"]
|
||||
r = uploader.audio(audio, title=title, artist=artist)
|
||||
id = r["id"]
|
||||
owner_id = r["owner_id"]
|
||||
local_attachments += "audio{0}_{1},".format(owner_id, id)
|
||||
elif i["from"] == "local" and i["type"] == "voice_message":
|
||||
r = uploader.audio_message(i["file"], peer_id=self.kwargs["peer_id"])
|
||||
id = r["audio_message"]["id"]
|
||||
owner_id = r["audio_message"]["owner_id"]
|
||||
local_attachments += "audio_message{0}_{1},".format(owner_id, id)
|
||||
elif i["from"] == "local" and i["type"] == "document":
|
||||
document = i["file"]
|
||||
title = i["title"]
|
||||
r = uploader.document(document, title=title, message_peer_id=self.kwargs["peer_id"])
|
||||
id = r["doc"]["id"]
|
||||
owner_id = r["doc"]["owner_id"]
|
||||
local_attachments += "doc{0}_{1},".format(owner_id, id)
|
||||
return local_attachments
|
||||
|
||||
def _send_message(self, text, attachments=[]):
|
||||
if hasattr(self, "attachments_to_be_sent") and type(self.attachments_to_be_sent) == list:
|
||||
self.attachments_to_be_sent = self.upload_attachments(self.attachments_to_be_sent)
|
||||
try:
|
||||
# Let's take care about the random_id attribute.
|
||||
# This should be unique per message and should be changed right after the message has been sent.
|
||||
# If the message is tried to be sent twice this random_id should be the same for both copies.
|
||||
# At the moment we just calculate len(text)_user_id, hope that will work.
|
||||
random_id = random.randint(0, 100000)
|
||||
if hasattr(self, "attachments_to_be_sent"):
|
||||
response = self.session.vk.client.messages.send(peer_id=self.kwargs["peer_id"], message=text, attachment=self.attachments_to_be_sent, random_id=random_id)
|
||||
post_arguments = dict(random_id = random.randint(0, 100000), peer_id=self.kwargs["peer_id"])
|
||||
if len(text) > 0:
|
||||
post_arguments.update(message=text)
|
||||
if hasattr(self, "attachments_to_be_sent") and len(self.attachments_to_be_sent) > 0:
|
||||
attachments = self.attachments_to_be_sent[::]
|
||||
else:
|
||||
response = self.session.vk.client.messages.send(peer_id=self.kwargs["peer_id"], message=text, random_id=random_id)
|
||||
except ValueError as ex:
|
||||
if ex.code == 9:
|
||||
output.speak(_("You have been sending a message that is already sent. Try to update the buffer if you can't see the new message in the history."))
|
||||
finally:
|
||||
attachments = []
|
||||
call_threaded(pub.sendMessage, "post", parent_endpoint="messages", child_endpoint="send", attachments_list=attachments, post_arguments=post_arguments)
|
||||
if hasattr(self, "attachments_to_be_sent"):
|
||||
del self.attachments_to_be_sent
|
||||
|
||||
|
@ -313,8 +313,9 @@ class vkSession(object):
|
||||
This function should be capable of uploading all attachments before posting, and send a special event in case the post has failed,
|
||||
So the program can recreate the post and show it back to the user."""
|
||||
attachments = ""
|
||||
print(attachments_list)
|
||||
if len(attachments_list) > 0:
|
||||
attachments = self.upload_attachments(attachments_list)
|
||||
attachments = self.upload_attachments(attachments_list, post_arguments.get("peer_id"))
|
||||
# VK generally defines all kind of messages under "text", "message" or "body" so let's try with all of those
|
||||
possible_message_keys = ["text", "message", "body"]
|
||||
for i in possible_message_keys:
|
||||
@ -328,16 +329,20 @@ class vkSession(object):
|
||||
post_arguments[i] = post_arguments[i].replace(urls[0], "")
|
||||
# After modifying everything, let's update the post arguments if needed.
|
||||
if len(attachments) > 0:
|
||||
if parent_endpoint == "messages":
|
||||
post_arguments.update(attachment=attachments)
|
||||
else:
|
||||
post_arguments.update(attachments=attachments)
|
||||
# Determines the correct functions to call here.
|
||||
parent_endpoint = getattr(self.vk.client, parent_endpoint)
|
||||
endpoint = getattr(parent_endpoint, child_endpoint)
|
||||
post = endpoint(**post_arguments)
|
||||
print(post)
|
||||
|
||||
def upload_attachments(self, attachments):
|
||||
def upload_attachments(self, attachments, peer_id=None):
|
||||
""" Upload attachments to VK before posting them.
|
||||
Returns attachments formatted as string, as required by VK API."""
|
||||
Returns attachments formatted as string, as required by VK API.
|
||||
@ peer_id int: if this value is passed, let's assume attachments will be send in private messages.
|
||||
"""
|
||||
# To do: Check the caption and description fields for this kind of attachments.
|
||||
local_attachments = ""
|
||||
uploader = upload.VkUpload(self.vk.session_object)
|
||||
@ -347,7 +352,10 @@ class vkSession(object):
|
||||
elif i["from"] == "local" and i["type"] == "photo":
|
||||
photos = i["file"]
|
||||
description = i["description"]
|
||||
if peer_id == None:
|
||||
r = uploader.photo_wall(photos, caption=description)
|
||||
else:
|
||||
r = uploader.photo_messages(photos)
|
||||
id = r[0]["id"]
|
||||
owner_id = r[0]["owner_id"]
|
||||
local_attachments += "photo{0}_{1},".format(owner_id, id)
|
||||
@ -363,10 +371,18 @@ class vkSession(object):
|
||||
id = r["id"]
|
||||
owner_id = r["owner_id"]
|
||||
local_attachments += "audio{0}_{1},".format(owner_id, id)
|
||||
elif i["from"] == "local" and i["type"] == "voice_message":
|
||||
r = uploader.audio_message(i["file"], peer_id=peer_id)
|
||||
id = r["audio_message"]["id"]
|
||||
owner_id = r["audio_message"]["owner_id"]
|
||||
local_attachments += "audio_message{0}_{1},".format(owner_id, id)
|
||||
elif i["from"] == "local" and i["type"] == "document":
|
||||
document = i["file"]
|
||||
title = i["title"]
|
||||
if peer_id == None:
|
||||
r = uploader.document(document, title=title, to_wall=True)
|
||||
else:
|
||||
r = uploader.document(document, title=title, message_peer_id=peer_id)
|
||||
id = r["doc"]["id"]
|
||||
owner_id = r["doc"]["owner_id"]
|
||||
local_attachments += "doc{0}_{1},".format(owner_id, id)
|
||||
|
Loading…
Reference in New Issue
Block a user