def process_request(self, path, query_string, content):
"""default handler for incoming request
path should contain a conversation id e.g. http://localhost/XXXXXXXXXXX/
content is a valid json string with keys:
echo text string
image
base64encoded base64-encoded image data
filename optional filename (else determined automatically via imghdr)
"""
# parse incoming data
payload = json.loads(content)
path = path.split("/")
conversation_id = path[1]
if not conversation_id:
logger.error("{}: conversation id must be provided as part of path".format(self.sinkname))
return
text = None
if "echo" in payload:
text = payload["echo"]
image_data = None
image_filename = None
if "image" in payload:
if "base64encoded" in payload["image"]:
image_raw = base64.b64decode(payload["image"]["base64encoded"])
image_data = io.BytesIO(image_raw)
if "filename" in payload["image"]:
image_filename = payload["image"]["filename"]
else:
image_type = imghdr.what('ignore', image_raw)
image_filename = str(int(time.time())) + "." + image_type
logger.info("automatic image filename: {}".format(image_filename))
if not text and not image_data:
logger.error("{}: nothing to send".format(self.sinkname))
return
await self.send_data(conversation_id, text, image_data=image_data, image_filename=image_filename)
评论列表
文章目录