@@ -3089,6 +3089,36 @@ def quoted_reply_attachment(
30893089 return attachment , marker
30903090
30913091
3092+ def inline_image_payload (message , paths , content_type ):
3093+ """Build Graph hostedContents only from separately supplied image files."""
3094+ if not paths and content_type != "html-inline" :
3095+ return {}
3096+ if not paths or content_type != "html-inline" :
3097+ raise BackendError ("Inline images require an HTML image message" )
3098+ hosted = []
3099+ remaining = 4 * 1024 * 1024
3100+ for index , file in enumerate (paths , 1 ):
3101+ if f'src="../hostedContents/{ index } /$value"' not in message :
3102+ raise BackendError ("Inline image reference missing from message" )
3103+ path = Path (file )
3104+ if not path .is_file ():
3105+ raise BackendError ("Inline image file is missing" )
3106+ with path .open ("rb" ) as image :
3107+ data = image .read (remaining + 1 )
3108+ if len (data ) > remaining :
3109+ raise BackendError ("Inline images exceed 4 MiB; resize or attach as files" )
3110+ remaining -= len (data )
3111+ mime = next ((mime for prefix , mime in (
3112+ (b"\x89 PNG\r \n \x1a \n " , "image/png" ), (b"\xff \xd8 \xff " , "image/jpeg" ),
3113+ (b"GIF87a" , "image/gif" ), (b"GIF89a" , "image/gif" ))
3114+ if data .startswith (prefix )), None )
3115+ if not mime :
3116+ raise BackendError ("Unsupported inline image; choose a PNG, JPEG or GIF" )
3117+ hosted .append ({"@microsoft.graph.temporaryId" : str (index ), "contentType" : mime ,
3118+ "contentBytes" : base64 .b64encode (data ).decode ("ascii" )})
3119+ return {"hostedContents" : hosted }
3120+
3121+
30923122def send_message (
30933123 chat_id : str ,
30943124 message : str ,
@@ -3100,10 +3130,12 @@ def send_message(
31003130 reply_sender_id : str = "" ,
31013131 reply_sender_name : str = "" ,
31023132 reply_preview : str = "" ,
3133+ inline_images : list [str ] | None = None ,
31033134) -> dict [str , Any ]:
31043135 """Send MESSAGE to CHAT_ID, optionally with a native quote attachment."""
31053136 if not message .strip () and not attachment_paths :
31063137 raise BackendError ("Message and attachment list are empty" )
3138+ images = inline_image_payload (message , inline_images , content_type )
31073139 attachments , attachment_tags = uploaded_reference_attachments (
31083140 attachment_paths or [], access_token
31093141 )
@@ -3115,11 +3147,11 @@ def send_message(
31153147 attachment_tags .insert (0 , marker )
31163148 body , mentions = outgoing_body (
31173149 message ,
3118- content_type = content_type ,
3150+ content_type = "html" if images else content_type ,
31193151 mention_specs = mention_specs or [],
31203152 attachment_tags = attachment_tags ,
31213153 )
3122- payload : dict [str , Any ] = {"body" : body }
3154+ payload : dict [str , Any ] = {"body" : body , ** images }
31233155 if attachments :
31243156 payload ["attachments" ] = attachments
31253157 if mentions :
@@ -3142,6 +3174,7 @@ def send_channel_message(
31423174 attachment_paths : list [str ] | None = None ,
31433175 mention_specs : list [str ] | None = None ,
31443176 content_type : str = "text" ,
3177+ inline_images : list [str ] | None = None ,
31453178) -> dict [str , Any ]:
31463179 """Send a channel root post or a reply."""
31473180 if not message .strip () and not attachment_paths :
@@ -3150,16 +3183,17 @@ def send_channel_message(
31503183 f"/teams/{ quoted_id (team_id )} /channels/{ quoted_id (channel_id )} /messages"
31513184 )
31523185 path = f"{ base } /{ quoted_id (reply_to_id )} /replies" if reply_to_id else base
3186+ images = inline_image_payload (message , inline_images , content_type )
31533187 attachments , tags = uploaded_reference_attachments (
31543188 attachment_paths or [], access_token
31553189 )
31563190 body , mentions = outgoing_body (
31573191 message ,
3158- content_type = content_type ,
3192+ content_type = "html" if images else content_type ,
31593193 mention_specs = mention_specs or [],
31603194 attachment_tags = tags ,
31613195 )
3162- payload : dict [str , Any ] = {"body" : body }
3196+ payload : dict [str , Any ] = {"body" : body , ** images }
31633197 if attachments :
31643198 payload ["attachments" ] = attachments
31653199 if mentions :
@@ -3990,6 +4024,7 @@ def execute(
39904024 str (option (args , "--replySenderId" , required = False ) or "" ),
39914025 str (option (args , "--replySenderName" , required = False ) or "" ),
39924026 str (option (args , "--replyPreview" , required = False ) or "" ),
4027+ inline_images = options (args , "--inlineImage" ),
39934028 )
39944029 with TeamsCache () as cache :
39954030 cache .upsert_messages ("chat" , str (chat_id ), [result ])
@@ -4087,6 +4122,7 @@ def execute(
40874122 access_token ,
40884123 reply_to_id = option (args , "--replyToId" , required = False ),
40894124 attachment_paths = options (args , "--attachment" ),
4125+ inline_images = options (args , "--inlineImage" ),
40904126 mention_specs = options (args , "--mention" ),
40914127 content_type = option (args , "--contentType" , required = False ) or "text" ,
40924128 )
0 commit comments