Hello Rajen Jani,
I understand you are trying to send an HTML-formatted message in a one-on-one Teams chat using Microsoft Graph Python SDK.
Initially, I also faced the same issue where the HTML tags did not render properly and the message appeared as plain text when running your code, where content_type
was passed as a string:
chat_request_body = ChatMessage( body = ItemBody( content="<h2>This is a test.</h2>", content_type="BodyType.Html", ),
Teams chat message:
The issue occurs because content_type
must be set using the BodyType enum, not by passing a plain text value. Updating your code like this should fix it:
import asyncio from azure.identity import InteractiveBrowserCredential from msgraph import GraphServiceClient from msgraph.generated.models.chat_message import ChatMessage from msgraph.generated.models.item_body import ItemBody from msgraph.generated.models.body_type import BodyType # <-- Import BodyType enum async def main(): credential = InteractiveBrowserCredential( client_id="appId", tenant_id="tennatId", redirect_uri="http://localhost:8400" ) client = GraphServiceClient(credentials=credential, scopes=["https://graph.microsoft.com/.default"]) chat_request_body = ChatMessage( body=ItemBody( content="<h2>This is a test.</h2>", content_type=BodyType.Html ) ) chat_id = "chatID" response = await client.chats.by_chat_id(chat_id).messages.post(chat_request_body) print(response) asyncio.run(main())
After making this change, the message is sent successfully, and Teams displays the heading properly in bold and larger font.
Let me know if you have any other questions or need any further assistance.
Hope this helps!
If this answer was helpful, please click "Accept the answer" and mark Yes
, as this can help other community members.
If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.