BotBuilder-schema contains the serialized data sent across the wire between user and bot when using Bot Framework
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
File attachment and media management models supporting upload, download, and display of documents, images, audio, and video content across Bot Framework channels.
The primary model for file attachments that can be sent with activities.
class Attachment(Model):
def __init__(self, *, content_type: str = None, content_url: str = None,
content = None, name: str = None, thumbnail_url: str = None, **kwargs): ...Properties:
content_type - MIME type of the content (e.g., "image/jpeg", "application/pdf")content_url - URL where the content can be downloadedcontent - Inline content object (for cards and structured data)name - Display name for the attachmentthumbnail_url - URL for thumbnail/preview imagefrom botbuilder.schema import Attachment, Activity, ActivityTypes
# File attachment with URL
file_attachment = Attachment(
content_type="application/pdf",
content_url="https://example.com/document.pdf",
name="User Manual.pdf"
)
# Image attachment with thumbnail
image_attachment = Attachment(
content_type="image/jpeg",
content_url="https://example.com/photo.jpg",
name="Photo.jpg",
thumbnail_url="https://example.com/photo-thumb.jpg"
)
# Send attachments in message
message = Activity(
type=ActivityTypes.message,
text="Here are the files you requested:",
attachments=[file_attachment, image_attachment]
)Model for uploading attachment content, typically used with file upload APIs.
class AttachmentData(Model):
def __init__(self, *, type: str = None, name: str = None,
original_base64: str = None, thumbnail_base64: str = None, **kwargs): ...import base64
from botbuilder.schema import AttachmentData
# Prepare file for upload
with open("document.pdf", "rb") as file:
file_data = base64.b64encode(file.read()).decode('utf-8')
attachment_data = AttachmentData(
type="application/pdf",
name="document.pdf",
original_base64=file_data
)Information about an attachment, including available views and formats.
class AttachmentInfo(Model):
def __init__(self, *, name: str = None, type: str = None,
views: List[AttachmentView] = None, **kwargs): ...
class AttachmentView(Model):
def __init__(self, *, view_id: str = None, size: int = None, **kwargs): ...from botbuilder.schema import AttachmentInfo, AttachmentView
attachment_info = AttachmentInfo(
name="presentation.pptx",
type="application/vnd.openxmlformats-officedocument.presentationml.presentation",
views=[
AttachmentView(view_id="original", size=2048576),
AttachmentView(view_id="thumbnail", size=15324)
]
)URL references for media content with optional profiles for different quality/formats.
class MediaUrl(Model):
def __init__(self, *, url: str = None, profile: str = None, **kwargs): ...from botbuilder.schema import MediaUrl
# Multiple quality options for video
media_urls = [
MediaUrl(url="https://example.com/video-hd.mp4", profile="high"),
MediaUrl(url="https://example.com/video-sd.mp4", profile="standard"),
MediaUrl(url="https://example.com/video-low.mp4", profile="low")
]Thumbnail image references with alt text for accessibility.
class ThumbnailUrl(Model):
def __init__(self, *, url: str = None, alt: str = None, **kwargs): ...from botbuilder.schema import ThumbnailUrl
thumbnail = ThumbnailUrl(
url="https://example.com/video-thumb.jpg",
alt="Video thumbnail showing the main topic"
)Images:
image/jpeg - JPEG imagesimage/png - PNG imagesimage/gif - GIF imagesimage/svg+xml - SVG vector graphicsDocuments:
application/pdf - PDF documentsapplication/msword - Word documents (.doc)application/vnd.openxmlformats-officedocument.wordprocessingml.document - Word documents (.docx)application/vnd.ms-excel - Excel spreadsheets (.xls)application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - Excel spreadsheets (.xlsx)Media:
audio/mpeg - MP3 audioaudio/wav - WAV audiovideo/mp4 - MP4 videovideo/quicktime - QuickTime videoArchives:
application/zip - ZIP archivesapplication/x-rar-compressed - RAR archivesfrom botbuilder.schema import Attachment
# Different content type examples
attachments = [
Attachment(
content_type="image/png",
content_url="https://example.com/chart.png",
name="Sales Chart.png"
),
Attachment(
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
content_url="https://example.com/report.docx",
name="Monthly Report.docx"
),
Attachment(
content_type="audio/mpeg",
content_url="https://example.com/recording.mp3",
name="Meeting Recording.mp3"
)
]from botbuilder.schema import Activity, ActivityTypes, Attachment
async def share_file(file_url: str, file_name: str, content_type: str):
attachment = Attachment(
content_type=content_type,
content_url=file_url,
name=file_name
)
return Activity(
type=ActivityTypes.message,
text=f"Here's the file: {file_name}",
attachments=[attachment]
)from botbuilder.schema import Activity, ActivityTypes, AttachmentLayoutTypes
def create_file_gallery(file_list):
attachments = []
for file_info in file_list:
attachment = Attachment(
content_type=file_info['content_type'],
content_url=file_info['url'],
name=file_info['name'],
thumbnail_url=file_info.get('thumbnail_url')
)
attachments.append(attachment)
return Activity(
type=ActivityTypes.message,
text="Here are your files:",
attachments=attachments,
attachment_layout=AttachmentLayoutTypes.list
)Different Bot Framework channels have varying levels of attachment support:
Full Support: Direct Line, Web Chat, Emulator
Partial Support: Teams, Slack
Limited Support: SMS, Email
from botbuilder.schema import Activity, ActivityTypes, Attachment
def create_accessible_attachment(url: str, name: str, content_type: str, description: str):
attachment = Attachment(
content_type=content_type,
content_url=url,
name=name
)
return Activity(
type=ActivityTypes.message,
text=f"{description}\n\nFile: {name}", # Fallback text
attachments=[attachment]
)from botbuilder.schema import Activity, ActivityTypes
def handle_attachment_error(error_message: str):
return Activity(
type=ActivityTypes.message,
text=f"Sorry, there was an issue with the file attachment: {error_message}. Please try uploading the file again or contact support if the problem persists."
)Attachments can be combined with rich cards for enhanced presentation:
from botbuilder.schema import (
Activity, ActivityTypes, Attachment, HeroCard,
CardImage, CardAction, AttachmentLayoutTypes
)
def create_file_card(file_url: str, file_name: str, description: str, thumbnail_url: str = None):
# Create hero card for file presentation
hero_card = HeroCard(
title=file_name,
text=description,
images=[CardImage(url=thumbnail_url)] if thumbnail_url else None,
buttons=[
CardAction(type="openUrl", title="Download", value=file_url),
CardAction(type="openUrl", title="Preview", value=f"{file_url}?preview=true")
]
)
card_attachment = Attachment(
content_type="application/vnd.microsoft.card.hero",
content=hero_card
)
return Activity(
type=ActivityTypes.message,
attachments=[card_attachment]
)Install with Tessl CLI
npx tessl i tessl/pypi-botbuilder-schema