apod - fix broken image metadata and error response / logging on room avatar event errors

There was a parameter off by one error. Propably introduced with the
room_send wrapper.

While testing the avatar feature I noticed that nothing happens if the bot is not at least a moderator in
the room. So I decided to send that info to the room and log the error
message as a warning.

The explanation was not altered by the apod module.
Turns out it was broken that day at the api level. See https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&hd=true&date=2022-09-13

fixes #216
This commit is contained in:
Frank Becker 2022-09-15 21:24:46 +02:00
parent 7aef0934ab
commit 69a7aae939
2 changed files with 17 additions and 24 deletions

37
bot.py
View File

@ -22,7 +22,9 @@ from io import BytesIO
from PIL import Image
import requests
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LoginError, RoomMemberEvent, RoomVisibility, RoomPreset, RoomCreateError, RoomResolveAliasResponse, UploadError, UploadResponse, SyncError
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LoginError, RoomMemberEvent, \
RoomVisibility, RoomPreset, RoomCreateError, RoomResolveAliasResponse, UploadError, UploadResponse, SyncError, \
RoomPutStateError
from modules.common.exceptions import CommandRequiresAdmin, CommandRequiresOwner, UploadFailed
@ -258,7 +260,7 @@ class Bot:
"msgtype": "m.image",
"info": {
"thumbnail_info": None,
"thumbnail_url": None,
"thumbnail_url": url,
},
}
@ -271,37 +273,28 @@ class Bot:
if size:
msg["info"]["size"] = size
self.logger.debug(f"send image room message: {msg}")
return await self.room_send(room.room_id, event, 'm.room.message', msg)
async def set_room_avatar(self, room, uri, mimetype=None, width=None, height=None, size=None):
async def set_room_avatar(self, room, uri):
"""
:param room: A MatrixRoom the image should be send to
:param room: A MatrixRoom the image should be send as room avatar event
:param uri: A MXC-Uri https://matrix.org/docs/spec/client_server/r0.6.0#mxc-uri
:param mimetype: The mimetype of the image
:param width: Width in pixel of the image
:param height: Height in pixel of the image
:param size: Size in bytes of the image
:return:
"""
msg = {
"url": uri,
"info": {
"thumbnail_info": None,
"thumbnail_url": None,
},
"url": uri
}
if mimetype:
msg["info"]["mimetype"] = mimetype
if width:
msg["info"]["w"] = width
if height:
msg["info"]["h"] = height
if size:
msg["info"]["size"] = size
result = await self.client.room_put_state(room.room_id, 'm.room.avatar', msg)
return await self.client.room_put_state(room.room_id, 'm.room.avatar', msg)
if isinstance(result, RoomPutStateError):
self.logger.warning(f"can't set room avatar. {result.message}")
await self.send_text(room, f"sorry. can't set room avatar. I need at least be a moderator")
return result
async def send_msg(self, mxid, roomname, message):
"""

View File

@ -107,10 +107,10 @@ class MatrixModule(BotModule):
matrix_uri, mimetype, w, h, size = await bot.upload_image(apod.hdurl)
except (UploadFailed, TypeError, ValueError):
await bot.send_text(room, f"Something went wrong uploading {apod.hdurl}.")
await bot.send_image(room, matrix_uri, apod.hdurl, mimetype, w, h, size)
await bot.send_image(room, matrix_uri, apod.hdurl, None, mimetype, w, h, size)
await bot.send_text(room, f"{apod.explanation}")
if matrix_uri and set_room_avatar:
await bot.set_room_avatar(room, matrix_uri, mimetype, w, h, size)
await bot.set_room_avatar(room, matrix_uri, None, mimetype, w, h, size)
async def send_unknown_mediatype(self, room, bot, apod):
self.logger.debug(f"unknown media_type: {apod.media_type}. sending raw information")