From 33013bc5f51572594ab9421b79bbed119c1f54b9 Mon Sep 17 00:00:00 2001 From: Daniel Dayley Date: Mon, 17 Apr 2023 16:03:48 -0600 Subject: [PATCH] Now downloading full attachments Cleaned up logging Fixed timestamp typo --- bin/bluebubbles_bot | 18 +++++++++++++++--- persona/persona.py | 5 +++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/bin/bluebubbles_bot b/bin/bluebubbles_bot index 9ca5335..e9f6fd0 100644 --- a/bin/bluebubbles_bot +++ b/bin/bluebubbles_bot @@ -102,13 +102,24 @@ if __name__ == '__main__': payload.update({'attachments':attachments}) url = os.environ['BB_SERVER_URL'].rstrip('/') + '/api/v1/message/attachment' requests.post(url,params=params,json=payload) + + def get_full_attachments(message: persona.Message) -> persona.Message : + """Given a message with basic attachment descriptions, fetch the full attachment payloads from BlueBubbles""" + for attachment in message.attachments : + params = {'password': os.environ['BB_SERVER_PASSWORD']} + url = os.environ['BB_SERVER_URL'].rstrip('/') + '/api/v1/attachment/' + attachment.data + # log.debug(requests.get(url,params=params).text) + url = os.environ['BB_SERVER_URL'].rstrip('/') + '/api/v1/attachment/' + attachment.data + '/download' + content = requests.get(url,params=params).content + attachment.data = content + return message # Create persona instance current_persona = persona.Persona() # Create a fastAPI instance @bot.post('/message') async def message(content: dict): - # print(content) + log.debug(content) if content['type'] == 'new-message' : message = content['data'] # Determine sender and receiver @@ -121,9 +132,9 @@ if __name__ == '__main__': # Resolve attachments attachments = [] for attachment in message['attachments'] : - attachments.append(persona.Attachment(mime_type=message['attachments'][attachment]['mimeType'],data=message['attachments'][attachment]['guid'])) + attachments.append(persona.Attachment(mime_type=attachment['mimeType'],data=attachment['guid'])) # Get the date sent - date_sent = datetime.datetime.fromtimestamp(message['dateCreated']/100) + date_sent = datetime.datetime.fromtimestamp(message['dateCreated']/1000) # Get any effects or subjects subject = '' effect_id = '' @@ -142,6 +153,7 @@ if __name__ == '__main__': attachments=attachments, meta={'subject': subject, 'effectId': effect_id} ) + persona_message = get_full_attachments(persona_message) responses = current_persona.receive_message(persona_message) for response in responses : prompt = persona_message diff --git a/persona/persona.py b/persona/persona.py index abc5e21..f9dd9ba 100644 --- a/persona/persona.py +++ b/persona/persona.py @@ -115,6 +115,7 @@ class Persona : should_respond = skill.match_intent(message) if should_respond : response = skill.respond(message=message) - self.log.info(f'Responding to \'{message.text}\' with \'{response.text}\'') - generated_messages.append(response) + if response : + self.log.info(f'Responding to \'{message.text}\' with \'{response.text}\'') + generated_messages.append(response) return generated_messages