Skip to main content

Receiving Inline Images

When a user pastes an image into the Teams compose box, the image does not arrive inside activity.text. Teams sends it as an attachment on the inbound message activity.

Inline image attachments are distinct from files attached through the compose box. They do not use the file.download.info content type and are not surfaced by ctx.files.

Understand the attachment shape

An inbound message with an inline image typically contains two related attachments:

{
"attachments": [
{
"contentType": "image/png",
"contentUrl": "https://.../v3/attachments/{id}/views/original"
},
{
"contentType": "text/html",
"content": "<p><img src=\"https://.../objects/{id}/views/imgo\" ...></p>"
}
]
}

Use the image/* attachment as the canonical source of the image. Its contentUrl points to the downloadable image bytes.

The text/html attachment preserves layout context, including where the image appeared among the message's text and other content. Do not use the HTML attachment's <img src> URL to download the image.

Download the image

The image attachment's contentUrl is authenticated rather than public. Fetch it with the app's authenticated HTTP client:

import base64

from microsoft_teams.api import MessageActivity
from microsoft_teams.apps import ActivityContext


@app.on_message
async def handle_message(ctx: ActivityContext[MessageActivity]) -> None:
image = next(
(
attachment
for attachment in (ctx.activity.attachments or [])
if attachment.content_type
and attachment.content_type.startswith("image/")
and attachment.content_url
),
None,
)

if image and image.content_url:
response = await app.api.http.get(image.content_url)
# The response content contains the image body as bytes.
image_bytes = response.content
image_base64 = base64.b64encode(image_bytes).decode("ascii")

# Pass image_bytes or image_base64 to your image-processing or model client.

The example buffers the complete image in memory. Keep the byte representation when your downstream API accepts it, or convert it to base64 when required by an image-processing or model API.